blob: 70b3a13c090c028598b92b97095c3e9fbbe2493a [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>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
238
239#else
240#error "Check if your OS uses bitfields for fd_sets"
241#endif
242
Willy Tarreau80587432006-12-24 17:47:20 +0100243void init_proto_http()
244{
Willy Tarreau42250582007-04-01 01:30:43 +0200245 int i;
246 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100247 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200248
Willy Tarreau80587432006-12-24 17:47:20 +0100249 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
250 if (!http_err_msgs[msg]) {
251 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
252 abort();
253 }
254
255 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
256 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
257 }
Willy Tarreau42250582007-04-01 01:30:43 +0200258
259 /* initialize the log header encoding map : '{|}"#' should be encoded with
260 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
261 * URL encoding only requires '"', '#' to be encoded as well as non-
262 * printable characters above.
263 */
264 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
265 memset(url_encode_map, 0, sizeof(url_encode_map));
266 for (i = 0; i < 32; i++) {
267 FD_SET(i, hdr_encode_map);
268 FD_SET(i, url_encode_map);
269 }
270 for (i = 127; i < 256; i++) {
271 FD_SET(i, hdr_encode_map);
272 FD_SET(i, url_encode_map);
273 }
274
275 tmp = "\"#{|}";
276 while (*tmp) {
277 FD_SET(*tmp, hdr_encode_map);
278 tmp++;
279 }
280
281 tmp = "\"#";
282 while (*tmp) {
283 FD_SET(*tmp, url_encode_map);
284 tmp++;
285 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200286
287 /* memory allocations */
288 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100289 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100290}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau53b6c742006-12-17 13:37:46 +0100292/*
293 * We have 26 list of methods (1 per first letter), each of which can have
294 * up to 3 entries (2 valid, 1 null).
295 */
296struct http_method_desc {
297 http_meth_t meth;
298 int len;
299 const char text[8];
300};
301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100302const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100303 ['C' - 'A'] = {
304 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
305 },
306 ['D' - 'A'] = {
307 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
308 },
309 ['G' - 'A'] = {
310 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
311 },
312 ['H' - 'A'] = {
313 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
314 },
315 ['P' - 'A'] = {
316 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
317 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
318 },
319 ['T' - 'A'] = {
320 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
321 },
322 /* rest is empty like this :
323 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
324 */
325};
326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100327/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100329 * RFC2616 for those chars.
330 */
331
332const char http_is_spht[256] = {
333 [' '] = 1, ['\t'] = 1,
334};
335
336const char http_is_crlf[256] = {
337 ['\r'] = 1, ['\n'] = 1,
338};
339
340const char http_is_lws[256] = {
341 [' '] = 1, ['\t'] = 1,
342 ['\r'] = 1, ['\n'] = 1,
343};
344
345const char http_is_sep[256] = {
346 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
347 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
348 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
349 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
350 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
351};
352
353const char http_is_ctl[256] = {
354 [0 ... 31] = 1,
355 [127] = 1,
356};
357
358/*
359 * A token is any ASCII char that is neither a separator nor a CTL char.
360 * Do not overwrite values in assignment since gcc-2.95 will not handle
361 * them correctly. Instead, define every non-CTL char's status.
362 */
363const char http_is_token[256] = {
364 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
365 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
366 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
367 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
369 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
370 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
371 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
372 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
373 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
374 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
375 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
376 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
377 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
378 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
379 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
380 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
381 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
382 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
383 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
384 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
385 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
386 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
387 ['|'] = 1, ['}'] = 0, ['~'] = 1,
388};
389
390
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100391/*
392 * An http ver_token is any ASCII which can be found in an HTTP version,
393 * which includes 'H', 'T', 'P', '/', '.' and any digit.
394 */
395const char http_is_ver_token[256] = {
396 ['.'] = 1, ['/'] = 1,
397 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
398 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
399 ['H'] = 1, ['P'] = 1, ['T'] = 1,
400};
401
402
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100403/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100404 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
405 */
406#if defined(DEBUG_FSM)
407static void http_silent_debug(int line, struct session *s)
408{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100409 chunk_printf(&trash,
410 "[%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",
411 line,
412 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
413 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);
414 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100415
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416 chunk_printf(&trash,
417 " %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",
418 line,
419 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
420 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);
421 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100422}
423#else
424#define http_silent_debug(l,s) do { } while (0)
425#endif
426
427/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100428 * Adds a header and its CRLF at the tail of the message's buffer, just before
429 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100430 * The header is also automatically added to the index <hdr_idx>, and the end
431 * of headers is automatically adjusted. The number of bytes added is returned
432 * on success, otherwise <0 is returned indicating an error.
433 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100434int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435{
436 int bytes, len;
437
438 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200439 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440 if (!bytes)
441 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100442 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
444}
445
446/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100447 * Adds a header and its CRLF at the tail of the message's buffer, just before
448 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100449 * the buffer is only opened and the space reserved, but nothing is copied.
450 * The header is also automatically added to the index <hdr_idx>, and the end
451 * of headers is automatically adjusted. The number of bytes added is returned
452 * on success, otherwise <0 is returned indicating an error.
453 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100454int http_header_add_tail2(struct http_msg *msg,
455 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456{
457 int bytes;
458
Willy Tarreau9b28e032012-10-12 23:49:43 +0200459 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100460 if (!bytes)
461 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100462 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
466/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100467 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
468 * If so, returns the position of the first non-space character relative to
469 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
470 * to return a pointer to the place after the first space. Returns 0 if the
471 * header name does not match. Checks are case-insensitive.
472 */
473int http_header_match2(const char *hdr, const char *end,
474 const char *name, int len)
475{
476 const char *val;
477
478 if (hdr + len >= end)
479 return 0;
480 if (hdr[len] != ':')
481 return 0;
482 if (strncasecmp(hdr, name, len) != 0)
483 return 0;
484 val = hdr + len + 1;
485 while (val < end && HTTP_IS_SPHT(*val))
486 val++;
487 if ((val >= end) && (len + 2 <= end - hdr))
488 return len + 2; /* we may replace starting from second space */
489 return val - hdr;
490}
491
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200492/* Find the first or next occurrence of header <name> in message buffer <sol>
493 * using headers index <idx>, and return it in the <ctx> structure. This
494 * structure holds everything necessary to use the header and find next
495 * occurrence. If its <idx> member is 0, the header is searched from the
496 * beginning. Otherwise, the next occurrence is returned. The function returns
497 * 1 when it finds a value, and 0 when there is no more. It is very similar to
498 * http_find_header2() except that it is designed to work with full-line headers
499 * whose comma is not a delimiter but is part of the syntax. As a special case,
500 * if ctx->val is NULL when searching for a new values of a header, the current
501 * header is rescanned. This allows rescanning after a header deletion.
502 */
503int http_find_full_header2(const char *name, int len,
504 char *sol, struct hdr_idx *idx,
505 struct hdr_ctx *ctx)
506{
507 char *eol, *sov;
508 int cur_idx, old_idx;
509
510 cur_idx = ctx->idx;
511 if (cur_idx) {
512 /* We have previously returned a header, let's search another one */
513 sol = ctx->line;
514 eol = sol + idx->v[cur_idx].len;
515 goto next_hdr;
516 }
517
518 /* first request for this header */
519 sol += hdr_idx_first_pos(idx);
520 old_idx = 0;
521 cur_idx = hdr_idx_first_idx(idx);
522 while (cur_idx) {
523 eol = sol + idx->v[cur_idx].len;
524
525 if (len == 0) {
526 /* No argument was passed, we want any header.
527 * To achieve this, we simply build a fake request. */
528 while (sol + len < eol && sol[len] != ':')
529 len++;
530 name = sol;
531 }
532
533 if ((len < eol - sol) &&
534 (sol[len] == ':') &&
535 (strncasecmp(sol, name, len) == 0)) {
536 ctx->del = len;
537 sov = sol + len + 1;
538 while (sov < eol && http_is_lws[(unsigned char)*sov])
539 sov++;
540
541 ctx->line = sol;
542 ctx->prev = old_idx;
543 ctx->idx = cur_idx;
544 ctx->val = sov - sol;
545 ctx->tws = 0;
546 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
547 eol--;
548 ctx->tws++;
549 }
550 ctx->vlen = eol - sov;
551 return 1;
552 }
553 next_hdr:
554 sol = eol + idx->v[cur_idx].cr + 1;
555 old_idx = cur_idx;
556 cur_idx = idx->v[cur_idx].next;
557 }
558 return 0;
559}
560
Willy Tarreau68085d82010-01-18 14:54:04 +0100561/* Find the end of the header value contained between <s> and <e>. See RFC2616,
562 * par 2.2 for more information. Note that it requires a valid header to return
563 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100565char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200566{
567 int quoted, qdpair;
568
569 quoted = qdpair = 0;
570 for (; s < e; s++) {
571 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200572 else if (quoted) {
573 if (*s == '\\') qdpair = 1;
574 else if (*s == '"') quoted = 0;
575 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 else if (*s == '"') quoted = 1;
577 else if (*s == ',') return s;
578 }
579 return s;
580}
581
582/* Find the first or next occurrence of header <name> in message buffer <sol>
583 * using headers index <idx>, and return it in the <ctx> structure. This
584 * structure holds everything necessary to use the header and find next
585 * occurrence. If its <idx> member is 0, the header is searched from the
586 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100587 * 1 when it finds a value, and 0 when there is no more. It is designed to work
588 * with headers defined as comma-separated lists. As a special case, if ctx->val
589 * is NULL when searching for a new values of a header, the current header is
590 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
592int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 struct hdr_ctx *ctx)
595{
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 char *eol, *sov;
597 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200598
Willy Tarreau68085d82010-01-18 14:54:04 +0100599 cur_idx = ctx->idx;
600 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 /* We have previously returned a value, let's search
602 * another one on the same line.
603 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200605 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 eol = sol + idx->v[cur_idx].len;
608
609 if (sov >= eol)
610 /* no more values in this header */
611 goto next_hdr;
612
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* values remaining for this header, skip the comma but save it
614 * for later use (eg: for header deletion).
615 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 sov++;
617 while (sov < eol && http_is_lws[(unsigned char)*sov])
618 sov++;
619
620 goto return_hdr;
621 }
622
623 /* first request for this header */
624 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627 while (cur_idx) {
628 eol = sol + idx->v[cur_idx].len;
629
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200630 if (len == 0) {
631 /* No argument was passed, we want any header.
632 * To achieve this, we simply build a fake request. */
633 while (sol + len < eol && sol[len] != ':')
634 len++;
635 name = sol;
636 }
637
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 if ((len < eol - sol) &&
639 (sol[len] == ':') &&
640 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 sov = sol + len + 1;
643 while (sov < eol && http_is_lws[(unsigned char)*sov])
644 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100645
Willy Tarreau33a7e692007-06-10 19:45:56 +0200646 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100647 ctx->prev = old_idx;
648 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200649 ctx->idx = cur_idx;
650 ctx->val = sov - sol;
651
652 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200654 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200655 eol--;
656 ctx->tws++;
657 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 ctx->vlen = eol - sov;
659 return 1;
660 }
661 next_hdr:
662 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200664 cur_idx = idx->v[cur_idx].next;
665 }
666 return 0;
667}
668
669int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 struct hdr_ctx *ctx)
672{
673 return http_find_header2(name, strlen(name), sol, idx, ctx);
674}
675
Willy Tarreau68085d82010-01-18 14:54:04 +0100676/* Remove one value of a header. This only works on a <ctx> returned by one of
677 * the http_find_header functions. The value is removed, as well as surrounding
678 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100679 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * message <msg>. The new index is returned. If it is zero, it means there is
681 * no more header, so any processing may stop. The ctx is always left in a form
682 * that can be handled by http_find_header2() to find next occurrence.
683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100685{
686 int cur_idx = ctx->idx;
687 char *sol = ctx->line;
688 struct hdr_idx_elem *hdr;
689 int delta, skip_comma;
690
691 if (!cur_idx)
692 return 0;
693
694 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200697 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 http_msg_move_end(msg, delta);
699 idx->used--;
700 hdr->len = 0; /* unused entry */
701 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100702 if (idx->tail == ctx->idx)
703 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100704 ctx->idx = ctx->prev; /* walk back to the end of previous header */
705 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
706 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200707 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 return ctx->idx;
709 }
710
711 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200712 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
713 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 */
715
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200716 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200717 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200718 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 NULL, 0);
720 hdr->len += delta;
721 http_msg_move_end(msg, delta);
722 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725}
726
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727/* This function handles a server error at the stream interface level. The
728 * stream interface is assumed to be already in a closed state. An optional
729 * message is copied into the input buffer, and an HTTP status code stored.
730 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100731 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733static void http_server_error(struct session *t, struct stream_interface *si,
734 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200736 channel_auto_read(si->ob);
737 channel_abort(si->ob);
738 channel_auto_close(si->ob);
739 channel_erase(si->ob);
740 channel_auto_close(si->ib);
741 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100742 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100743 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200744 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 }
746 if (!(t->flags & SN_ERR_MASK))
747 t->flags |= err;
748 if (!(t->flags & SN_FINST_MASK))
749 t->flags |= finst;
750}
751
Willy Tarreau80587432006-12-24 17:47:20 +0100752/* This function returns the appropriate error location for the given session
753 * and message.
754 */
755
Willy Tarreau783f2582012-09-04 12:19:04 +0200756struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100757{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200758 if (s->be->errmsg[msgnum].str)
759 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100760 else if (s->fe->errmsg[msgnum].str)
761 return &s->fe->errmsg[msgnum];
762 else
763 return &http_err_chunks[msgnum];
764}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765
Willy Tarreau53b6c742006-12-17 13:37:46 +0100766/*
767 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
768 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
769 */
770static http_meth_t find_http_meth(const char *str, const int len)
771{
772 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100773 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100774
775 m = ((unsigned)*str - 'A');
776
777 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100778 for (h = http_methods[m]; h->len > 0; h++) {
779 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100780 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100781 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100783 };
784 return HTTP_METH_OTHER;
785 }
786 return HTTP_METH_NONE;
787
788}
789
Willy Tarreau21d2af32008-02-14 20:25:24 +0100790/* Parse the URI from the given transaction (which is assumed to be in request
791 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
792 * It is returned otherwise.
793 */
794static char *
795http_get_path(struct http_txn *txn)
796{
797 char *ptr, *end;
798
Willy Tarreau9b28e032012-10-12 23:49:43 +0200799 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100800 end = ptr + txn->req.sl.rq.u_l;
801
802 if (ptr >= end)
803 return NULL;
804
805 /* RFC2616, par. 5.1.2 :
806 * Request-URI = "*" | absuri | abspath | authority
807 */
808
809 if (*ptr == '*')
810 return NULL;
811
812 if (isalpha((unsigned char)*ptr)) {
813 /* this is a scheme as described by RFC3986, par. 3.1 */
814 ptr++;
815 while (ptr < end &&
816 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
817 ptr++;
818 /* skip '://' */
819 if (ptr == end || *ptr++ != ':')
820 return NULL;
821 if (ptr == end || *ptr++ != '/')
822 return NULL;
823 if (ptr == end || *ptr++ != '/')
824 return NULL;
825 }
826 /* skip [user[:passwd]@]host[:[port]] */
827
828 while (ptr < end && *ptr != '/')
829 ptr++;
830
831 if (ptr == end)
832 return NULL;
833
834 /* OK, we got the '/' ! */
835 return ptr;
836}
837
Willy Tarreau71241ab2012-12-27 11:30:54 +0100838/* Returns a 302 for a redirectable request that reaches a server working in
839 * in redirect mode. This may only be called just after the stream interface
840 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
841 * follow normal proxy processing. NOTE: this function is designed to support
842 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100844void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845{
846 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200849 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850
851 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100852 trash.len = strlen(HTTP_302);
853 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100855 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100856
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100858 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859 return;
860
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100861 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100862 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100863 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
864 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100865 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100866
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200867 /* 3: add the request URI. Since it was already forwarded, we need
868 * to temporarily rewind the buffer.
869 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200871 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200872
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200874 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 +0200875
Willy Tarreau9b28e032012-10-12 23:49:43 +0200876 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200877
Willy Tarreauefb453c2008-10-26 20:49:47 +0100878 if (!path)
879 return;
880
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100881 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100882 return;
883
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100884 memcpy(trash.str + trash.len, path, len);
885 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100886
887 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100888 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
889 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100890 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100891 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
892 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100893 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100894
895 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200896 si_shutr(si);
897 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100898 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 si->state = SI_ST_CLO;
900
901 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200902 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100903
904 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100905 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906}
907
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100908/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 * that the server side is closed. Note that err_type is actually a
910 * bitmask, where almost only aborts may be cumulated with other
911 * values. We consider that aborted operations are more important
912 * than timeouts or errors due to the fact that nobody else in the
913 * logs might explain incomplete retries. All others should avoid
914 * being cumulated. It should normally not be possible to have multiple
915 * aborts at once, but just in case, the first one in sequence is reported.
916 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100917void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920
921 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100922 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200923 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200926 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100928 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200929 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200932 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200935 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100937 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200938 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100940 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200941 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942}
943
Willy Tarreau42250582007-04-01 01:30:43 +0200944extern const char sess_term_cond[8];
945extern const char sess_fin_state[8];
946extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200947struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100948struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100949struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100950
Willy Tarreau117f59e2007-03-04 18:17:17 +0100951/*
952 * Capture headers from message starting at <som> according to header list
953 * <cap_hdr>, and fill the <idx> structure appropriately.
954 */
955void capture_headers(char *som, struct hdr_idx *idx,
956 char **cap, struct cap_hdr *cap_hdr)
957{
958 char *eol, *sol, *col, *sov;
959 int cur_idx;
960 struct cap_hdr *h;
961 int len;
962
963 sol = som + hdr_idx_first_pos(idx);
964 cur_idx = hdr_idx_first_idx(idx);
965
966 while (cur_idx) {
967 eol = sol + idx->v[cur_idx].len;
968
969 col = sol;
970 while (col < eol && *col != ':')
971 col++;
972
973 sov = col + 1;
974 while (sov < eol && http_is_lws[(unsigned char)*sov])
975 sov++;
976
977 for (h = cap_hdr; h; h = h->next) {
978 if ((h->namelen == col - sol) &&
979 (strncasecmp(sol, h->name, h->namelen) == 0)) {
980 if (cap[h->index] == NULL)
981 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200982 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100983
984 if (cap[h->index] == NULL) {
985 Alert("HTTP capture : out of memory.\n");
986 continue;
987 }
988
989 len = eol - sov;
990 if (len > h->len)
991 len = h->len;
992
993 memcpy(cap[h->index], sov, len);
994 cap[h->index][len]=0;
995 }
996 }
997 sol = eol + idx->v[cur_idx].cr + 1;
998 cur_idx = idx->v[cur_idx].next;
999 }
1000}
1001
1002
Willy Tarreau42250582007-04-01 01:30:43 +02001003/* either we find an LF at <ptr> or we jump to <bad>.
1004 */
1005#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1006
1007/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1008 * otherwise to <http_msg_ood> with <state> set to <st>.
1009 */
1010#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1011 ptr++; \
1012 if (likely(ptr < end)) \
1013 goto good; \
1014 else { \
1015 state = (st); \
1016 goto http_msg_ood; \
1017 } \
1018 } while (0)
1019
1020
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001022 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1024 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1025 * will give undefined results.
1026 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1027 * and that msg->sol points to the beginning of the response.
1028 * If a complete line is found (which implies that at least one CR or LF is
1029 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1030 * returned indicating an incomplete line (which does not mean that parts have
1031 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1032 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1033 * upon next call.
1034 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001035 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1037 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001040const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001041 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001042 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001043{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001044 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001045
Willy Tarreau8973c702007-01-21 23:58:29 +01001046 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001047 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001048 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001049 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1051
1052 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001053 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001054 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1055 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001056 state = HTTP_MSG_ERROR;
1057 break;
1058
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001060 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001062 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001063 goto http_msg_rpcode;
1064 }
1065 if (likely(HTTP_IS_SPHT(*ptr)))
1066 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1067 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001068 state = HTTP_MSG_ERROR;
1069 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001070
Willy Tarreau8973c702007-01-21 23:58:29 +01001071 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001072 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001073 if (likely(!HTTP_IS_LWS(*ptr)))
1074 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1075
1076 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001077 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001078 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1079 }
1080
1081 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001083 http_msg_rsp_reason:
1084 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 msg->sl.st.r_l = 0;
1087 goto http_msg_rpline_eol;
1088
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001090 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001091 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001093 goto http_msg_rpreason;
1094 }
1095 if (likely(HTTP_IS_SPHT(*ptr)))
1096 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1097 /* so it's a CR/LF, so there is no reason phrase */
1098 goto http_msg_rsp_reason;
1099
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001101 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 if (likely(!HTTP_IS_CRLF(*ptr)))
1103 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 http_msg_rpline_eol:
1106 /* We have seen the end of line. Note that we do not
1107 * necessarily have the \n yet, but at least we know that we
1108 * have EITHER \r OR \n, otherwise the response would not be
1109 * complete. We can then record the response length and return
1110 * to the caller which will be able to register it.
1111 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001112 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 return ptr;
1114
1115#ifdef DEBUG_FULL
1116 default:
1117 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1118 exit(1);
1119#endif
1120 }
1121
1122 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001123 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 if (ret_state)
1125 *ret_state = state;
1126 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001127 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129}
1130
Willy Tarreau8973c702007-01-21 23:58:29 +01001131/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 * This function parses a request line between <ptr> and <end>, starting with
1133 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1134 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1135 * will give undefined results.
1136 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1137 * and that msg->sol points to the beginning of the request.
1138 * If a complete line is found (which implies that at least one CR or LF is
1139 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1140 * returned indicating an incomplete line (which does not mean that parts have
1141 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1142 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1143 * upon next call.
1144 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1147 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001148 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001150const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001151 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001152 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001154 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001155
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(HTTP_IS_TOKEN(*ptr)))
1160 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001167 if (likely(HTTP_IS_CRLF(*ptr))) {
1168 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001169 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 msg->sl.rq.v_l = 0;
1177 goto http_msg_rqline_eol;
1178 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 state = HTTP_MSG_ERROR;
1180 break;
1181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001183 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001185 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 goto http_msg_rquri;
1187 }
1188 if (likely(HTTP_IS_SPHT(*ptr)))
1189 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1190 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1191 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001194 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001195 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001199 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1201 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001203 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001204 /* non-ASCII chars are forbidden unless option
1205 * accept-invalid-http-request is enabled in the frontend.
1206 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 if (msg->err_pos < -1)
1209 goto invalid_char;
1210 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001211 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001212 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1213 }
1214
1215 if (likely(HTTP_IS_CRLF(*ptr))) {
1216 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1217 goto http_msg_req09_uri_e;
1218 }
1219
1220 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001221 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001222 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001223 state = HTTP_MSG_ERROR;
1224 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001227 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001229 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 goto http_msg_rqver;
1231 }
1232 if (likely(HTTP_IS_SPHT(*ptr)))
1233 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1234 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1235 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001239 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001241
1242 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244 http_msg_rqline_eol:
1245 /* We have seen the end of line. Note that we do not
1246 * necessarily have the \n yet, but at least we know that we
1247 * have EITHER \r OR \n, otherwise the request would not be
1248 * complete. We can then record the request length and return
1249 * to the caller which will be able to register it.
1250 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001251 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001252 return ptr;
1253 }
1254
1255 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001256 state = HTTP_MSG_ERROR;
1257 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259#ifdef DEBUG_FULL
1260 default:
1261 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1262 exit(1);
1263#endif
1264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275/*
1276 * Returns the data from Authorization header. Function may be called more
1277 * than once so data is stored in txn->auth_data. When no header is found
1278 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1279 * searching again for something we are unable to find anyway.
1280 */
1281
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001282char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001283
1284int
1285get_http_auth(struct session *s)
1286{
1287
1288 struct http_txn *txn = &s->txn;
1289 struct chunk auth_method;
1290 struct hdr_ctx ctx;
1291 char *h, *p;
1292 int len;
1293
1294#ifdef DEBUG_AUTH
1295 printf("Auth for session %p: %d\n", s, txn->auth.method);
1296#endif
1297
1298 if (txn->auth.method == HTTP_AUTH_WRONG)
1299 return 0;
1300
1301 if (txn->auth.method)
1302 return 1;
1303
1304 txn->auth.method = HTTP_AUTH_WRONG;
1305
1306 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001307
1308 if (txn->flags & TX_USE_PX_CONN) {
1309 h = "Proxy-Authorization";
1310 len = strlen(h);
1311 } else {
1312 h = "Authorization";
1313 len = strlen(h);
1314 }
1315
Willy Tarreau9b28e032012-10-12 23:49:43 +02001316 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 return 0;
1318
1319 h = ctx.line + ctx.val;
1320
1321 p = memchr(h, ' ', ctx.vlen);
1322 if (!p || p == h)
1323 return 0;
1324
1325 chunk_initlen(&auth_method, h, 0, p-h);
1326 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1327
1328 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1329
1330 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001331 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001332
1333 if (len < 0)
1334 return 0;
1335
1336
1337 get_http_auth_buff[len] = '\0';
1338
1339 p = strchr(get_http_auth_buff, ':');
1340
1341 if (!p)
1342 return 0;
1343
1344 txn->auth.user = get_http_auth_buff;
1345 *p = '\0';
1346 txn->auth.pass = p+1;
1347
1348 txn->auth.method = HTTP_AUTH_BASIC;
1349 return 1;
1350 }
1351
1352 return 0;
1353}
1354
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Willy Tarreau8973c702007-01-21 23:58:29 +01001356/*
1357 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001358 * depending on the initial msg->msg_state. The caller is responsible for
1359 * ensuring that the message does not wrap. The function can be preempted
1360 * everywhere when data are missing and recalled at the exact same location
1361 * with no information loss. The message may even be realigned between two
1362 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001363 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001364 * fields. Note that msg->sol will be initialized after completing the first
1365 * state, so that none of the msg pointers has to be initialized prior to the
1366 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001368void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001370 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001372 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001373
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001374 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001375 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001376 ptr = buf->p + msg->next;
1377 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 if (unlikely(ptr >= end))
1380 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 /*
1384 * First, states that are specific to the response only.
1385 * We check them first so that request and headers are
1386 * closer to each other (accessed more often).
1387 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001389 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 /* we have a start of message, but we have to check
1392 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001393 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001395 if (unlikely(ptr != buf->p)) {
1396 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001397 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001398 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001399 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 }
Willy Tarreau26927362012-05-18 23:22:52 +02001401 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001402 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001403 hdr_idx_init(idx);
1404 state = HTTP_MSG_RPVER;
1405 goto http_msg_rpver;
1406 }
1407
1408 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1409 goto http_msg_invalid;
1410
1411 if (unlikely(*ptr == '\n'))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1413 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1414 /* stop here */
1415
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001417 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 EXPECT_LF_HERE(ptr, http_msg_invalid);
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1420 /* stop here */
1421
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 case HTTP_MSG_RPVER_SP:
1425 case HTTP_MSG_RPCODE:
1426 case HTTP_MSG_RPCODE_SP:
1427 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001428 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001429 state, ptr, end,
1430 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 if (unlikely(!ptr))
1432 return;
1433
1434 /* we have a full response and we know that we have either a CR
1435 * or an LF at <ptr>.
1436 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1438
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001439 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (likely(*ptr == '\r'))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1442 goto http_msg_rpline_end;
1443
Willy Tarreau8973c702007-01-21 23:58:29 +01001444 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001445 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* msg->sol must point to the first of CR or LF. */
1447 EXPECT_LF_HERE(ptr, http_msg_invalid);
1448 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1449 /* stop here */
1450
1451 /*
1452 * Second, states that are specific to the request only
1453 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001457 /* we have a start of message, but we have to check
1458 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001459 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001460 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001461 if (likely(ptr != buf->p)) {
1462 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001463 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001464 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 }
Willy Tarreau26927362012-05-18 23:22:52 +02001467 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001468 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 state = HTTP_MSG_RQMETH;
1470 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1474 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (unlikely(*ptr == '\n'))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 EXPECT_LF_HERE(ptr, http_msg_invalid);
1484 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_RQMETH_SP:
1490 case HTTP_MSG_RQURI:
1491 case HTTP_MSG_RQURI_SP:
1492 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001493 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001494 state, ptr, end,
1495 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (unlikely(!ptr))
1497 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 /* we have a full request and we know that we have either a CR
1500 * or an LF at <ptr>.
1501 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001504 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (likely(*ptr == '\r'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001510 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 /* check for HTTP/0.9 request : no version information available.
1512 * msg->sol must point to the first of CR or LF.
1513 */
1514 if (unlikely(msg->sl.rq.v_l == 0))
1515 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 /*
1522 * Common states below
1523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (likely(!HTTP_IS_CRLF(*ptr))) {
1528 goto http_msg_hdr_name;
1529 }
1530
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1533 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 /* assumes msg->sol points to the first char */
1538 if (likely(HTTP_IS_TOKEN(*ptr)))
1539 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001544 if (likely(msg->err_pos < -1) || *ptr == '\n')
1545 goto http_msg_invalid;
1546
1547 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549
1550 /* and we still accept this non-token character */
1551 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001555 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (likely(HTTP_IS_SPHT(*ptr)))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 if (likely(!HTTP_IS_CRLF(*ptr))) {
1563 goto http_msg_hdr_val;
1564 }
1565
1566 if (likely(*ptr == '\r'))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1568 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(HTTP_IS_SPHT(*ptr))) {
1578 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001579 for (; buf->p + msg->sov < ptr; msg->sov++)
1580 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 goto http_msg_hdr_l1_sp;
1582 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001583 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001584 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 goto http_msg_complete_header;
1586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001588 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001589 /* assumes msg->sol points to the first char, and msg->sov
1590 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 */
1592 if (likely(!HTTP_IS_CRLF(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001594
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001595 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 /* Note: we could also copy eol into ->eoh so that we have the
1597 * real header end in case it ends with lots of LWS, but is this
1598 * really needed ?
1599 */
1600 if (likely(*ptr == '\r'))
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1602 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001605 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 EXPECT_LF_HERE(ptr, http_msg_invalid);
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1612 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001613 for (; buf->p + msg->eol < ptr; msg->eol++)
1614 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 goto http_msg_hdr_val;
1616 }
1617 http_msg_complete_header:
1618 /*
1619 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001620 * Assumes msg->sol points to the first char, msg->sov points
1621 * to the first character of the value and msg->eol to the
1622 * first CR or LF so we know how the line ends. We insert last
1623 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 idx, idx->tail) < 0))
1627 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001628
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 if (likely(!HTTP_IS_CRLF(*ptr))) {
1631 goto http_msg_hdr_name;
1632 }
1633
1634 if (likely(*ptr == '\r'))
1635 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1636 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 /* Assumes msg->sol points to the first of either CR or LF */
1641 EXPECT_LF_HERE(ptr, http_msg_invalid);
1642 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001644 msg->eoh = msg->sol;
1645 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001646 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001648
1649 case HTTP_MSG_ERROR:
1650 /* this may only happen if we call http_msg_analyser() twice with an error */
1651 break;
1652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653#ifdef DEBUG_FULL
1654 default:
1655 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1656 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001657#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 }
1659 http_msg_ood:
1660 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001661 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 http_msg_invalid:
1666 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
1670}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001671
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001672/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1673 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1674 * nothing is done and 1 is returned.
1675 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001676static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001677{
1678 int delta;
1679 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001680 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001681
1682 if (msg->sl.rq.v_l != 0)
1683 return 1;
1684
Willy Tarreau9b28e032012-10-12 23:49:43 +02001685 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001686 delta = 0;
1687
1688 if (msg->sl.rq.u_l == 0) {
1689 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001690 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001691 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001692 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001693 }
1694 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001695 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001696 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001698 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001700 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001701 NULL, NULL);
1702 if (unlikely(!cur_end))
1703 return 0;
1704
1705 /* we have a full HTTP/1.0 request now and we know that
1706 * we have either a CR or an LF at <ptr>.
1707 */
1708 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1709 return 1;
1710}
1711
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001713 * and "keep-alive" values. If we already know that some headers may safely
1714 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1716 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001717 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1719 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1720 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001721 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001722 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001723void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001724{
Willy Tarreau5b154472009-12-21 20:11:07 +01001725 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001726 const char *hdr_val = "Connection";
1727 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001728
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730 return;
1731
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection";
1734 hdr_len = 16;
1735 }
1736
Willy Tarreau5b154472009-12-21 20:11:07 +01001737 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001739 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001740 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1741 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001742 if (to_del & 2)
1743 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 else
1745 txn->flags |= TX_CON_KAL_SET;
1746 }
1747 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1748 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001749 if (to_del & 1)
1750 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001751 else
1752 txn->flags |= TX_CON_CLO_SET;
1753 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001754 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1755 txn->flags |= TX_HDR_CONN_UPG;
1756 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001757 }
1758
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001759 txn->flags |= TX_HDR_CONN_PRS;
1760 return;
1761}
Willy Tarreau5b154472009-12-21 20:11:07 +01001762
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001763/* Apply desired changes on the Connection: header. Values may be removed and/or
1764 * added depending on the <wanted> flags, which are exclusively composed of
1765 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1766 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1767 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001768void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769{
1770 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001771 const char *hdr_val = "Connection";
1772 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001773
1774 ctx.idx = 0;
1775
Willy Tarreau88d349d2010-01-25 12:15:43 +01001776
1777 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1778 hdr_val = "Proxy-Connection";
1779 hdr_len = 16;
1780 }
1781
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001782 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001784 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1785 if (wanted & TX_CON_KAL_SET)
1786 txn->flags |= TX_CON_KAL_SET;
1787 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001788 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001789 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1791 if (wanted & TX_CON_CLO_SET)
1792 txn->flags |= TX_CON_CLO_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001795 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001796 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001797
1798 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1799 return;
1800
1801 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1802 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001803 hdr_val = "Connection: close";
1804 hdr_len = 17;
1805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1806 hdr_val = "Proxy-Connection: close";
1807 hdr_len = 23;
1808 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001809 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001810 }
1811
1812 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1813 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 hdr_val = "Connection: keep-alive";
1815 hdr_len = 22;
1816 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1817 hdr_val = "Proxy-Connection: keep-alive";
1818 hdr_len = 28;
1819 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 }
1822 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001823}
1824
Willy Tarreaua458b672012-03-05 11:17:50 +01001825/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001827 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001830 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001831static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001832{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001833 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001835 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001836 const char *end = buf->data + buf->size;
1837 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 unsigned int chunk = 0;
1839
1840 /* The chunk size is in the following form, though we are only
1841 * interested in the size and CRLF :
1842 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1843 */
1844 while (1) {
1845 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001846 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 if (c < 0) /* not a hex digit anymore */
1850 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001851 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001852 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001853 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 chunk = (chunk << 4) + c;
1856 }
1857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001859 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861
1862 while (http_is_spht[(unsigned char)*ptr]) {
1863 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* Up to there, we know that at least one byte is present at *ptr. Check
1870 * for the end of chunk size.
1871 */
1872 while (1) {
1873 if (likely(HTTP_IS_CRLF(*ptr))) {
1874 /* we now have a CR or an LF at ptr */
1875 if (likely(*ptr == '\r')) {
1876 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001877 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001878 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 return 0;
1880 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001885 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 /* done */
1887 break;
1888 }
1889 else if (*ptr == ';') {
1890 /* chunk extension, ends at next CRLF */
1891 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001892 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001893 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
1896 while (!HTTP_IS_CRLF(*ptr)) {
1897 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001899 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 return 0;
1901 }
1902 /* we have a CRLF now, loop above */
1903 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001904 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001906 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001910 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001913 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001915 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001917 msg->chunk_len = chunk;
1918 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001921 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001923 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001924}
1925
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001926/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001927 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 * the trailers is found, it is automatically scheduled to be forwarded,
1929 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1930 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001931 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001932 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001934 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1935 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001936 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001937 * matches the length of trailers already parsed and not forwarded. It is also
1938 * important to note that this function is designed to be able to parse wrapped
1939 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001941static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001943 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001944
Willy Tarreaua458b672012-03-05 11:17:50 +01001945 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001947 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001948 const char *ptr = b_ptr(buf, msg->next);
1949 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001950 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 /* scan current line and stop at LF or CRLF */
1953 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001954 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return 0;
1956
1957 if (*ptr == '\n') {
1958 if (!p1)
1959 p1 = ptr;
1960 p2 = ptr;
1961 break;
1962 }
1963
1964 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001965 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 p1 = ptr;
1970 }
1971
1972 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 if (ptr >= buf->data + buf->size)
1974 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 }
1976
1977 /* after LF; point to beginning of next line */
1978 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (p2 >= buf->data + buf->size)
1980 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001983 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001985
1986 /* schedule this line for forwarding */
1987 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 if (msg->sov >= buf->size)
1989 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001992 /* LF/CRLF at beginning of line => end of trailers at p2.
1993 * Everything was scheduled for forwarding, there's nothing
1994 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001995 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001996 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 msg->msg_state = HTTP_MSG_DONE;
1998 return 1;
1999 }
2000 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 }
2003}
2004
Willy Tarreau54d23df2012-10-25 19:04:45 +02002005/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002007 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002008 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2010 * not enough data are available, the function does not change anything and
2011 * returns zero. If a parse error is encountered, the function returns < 0 and
2012 * does not change anything. Note: this function is designed to parse wrapped
2013 * CRLF at the end of the buffer.
2014 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002015static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002016{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002017 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002019 int bytes;
2020
2021 /* NB: we'll check data availabilty at the end. It's not a
2022 * problem because whatever we match first will be checked
2023 * against the correct length.
2024 */
2025 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002026 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027 if (*ptr == '\r') {
2028 bytes++;
2029 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002030 if (ptr >= buf->data + buf->size)
2031 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032 }
2033
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002034 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002037 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002038 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002040 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041
2042 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002043 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002045 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2046 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002047 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2049 return 1;
2050}
Willy Tarreau5b154472009-12-21 20:11:07 +01002051
William Lallemand82fe75c2012-10-23 10:25:10 +02002052
2053/*
2054 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002055 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002056int select_compression_request_header(struct session *s, struct buffer *req)
2057{
2058 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002059 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002060 struct hdr_ctx ctx;
2061 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002062 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002063
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002064 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2065 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002066 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2067 */
2068 ctx.idx = 0;
2069 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2070 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002071 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2072 (ctx.vlen < 31 ||
2073 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2074 ctx.line[ctx.val + 30] < '6' ||
2075 (ctx.line[ctx.val + 30] == '6' &&
2076 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2077 s->comp_algo = NULL;
2078 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002079 }
2080
William Lallemand82fe75c2012-10-23 10:25:10 +02002081 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002082 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 +02002083 ctx.idx = 0;
2084 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002085 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002086 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2087 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002088
2089 /* remove all occurrences of the header when "compression offload" is set */
2090
2091 if ((s->be->comp && s->be->comp->offload) ||
2092 (s->fe->comp && s->fe->comp->offload)) {
2093 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2094 ctx.idx = 0;
2095 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2096 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2097 }
2098 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002099 return 1;
2100 }
2101 }
2102 }
2103 }
2104
2105 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002106 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2107 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002108 if (comp_algo->add_data == identity_add_data) {
2109 s->comp_algo = comp_algo;
2110 return 1;
2111 }
2112 }
2113 }
2114
2115 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002116 return 0;
2117}
2118
2119/*
2120 * Selects a comression algorithm depending of the server response.
2121 */
2122int select_compression_response_header(struct session *s, struct buffer *res)
2123{
2124 struct http_txn *txn = &s->txn;
2125 struct http_msg *msg = &txn->rsp;
2126 struct hdr_ctx ctx;
2127 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002128
2129 /* no common compression algorithm was found in request header */
2130 if (s->comp_algo == NULL)
2131 goto fail;
2132
2133 /* HTTP < 1.1 should not be compressed */
2134 if (!(msg->flags & HTTP_MSGF_VER_11))
2135 goto fail;
2136
William Lallemandd3002612012-11-26 14:34:47 +01002137 /* 200 only */
2138 if (txn->status != 200)
2139 goto fail;
2140
William Lallemand82fe75c2012-10-23 10:25:10 +02002141 /* Content-Length is null */
2142 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2143 goto fail;
2144
Willy Tarreau667c2a32013-04-09 08:13:58 +02002145 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2146 if (msg->flags & HTTP_MSGF_TE_CHNK)
2147 goto fail;
2148
William Lallemand82fe75c2012-10-23 10:25:10 +02002149 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002150 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002151 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2152 goto fail;
2153
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002154 /* no compression when Cache-Control: no-transform is present in the message */
2155 ctx.idx = 0;
2156 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2157 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2158 goto fail;
2159 }
2160
William Lallemand82fe75c2012-10-23 10:25:10 +02002161 comp_type = NULL;
2162
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002163 /* we don't want to compress multipart content-types, nor content-types that are
2164 * not listed in the "compression type" directive if any. If no content-type was
2165 * found but configuration requires one, we don't compress either. Backend has
2166 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002168 ctx.idx = 0;
2169 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2170 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2171 goto fail;
2172
2173 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2174 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002175 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002176 if (ctx.vlen >= comp_type->name_len &&
2177 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002178 /* this Content-Type should be compressed */
2179 break;
2180 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002181 /* this Content-Type should not be compressed */
2182 if (comp_type == NULL)
2183 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002185 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002186 else { /* no content-type header */
2187 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2188 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002189 }
2190
William Lallemandd85f9172012-11-09 17:05:39 +01002191 /* limit compression rate */
2192 if (global.comp_rate_lim > 0)
2193 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2194 goto fail;
2195
William Lallemand072a2bf2012-11-20 17:01:01 +01002196 /* limit cpu usage */
2197 if (idle_pct < compress_min_idle)
2198 goto fail;
2199
William Lallemand4c49fae2012-11-07 15:00:23 +01002200 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002201 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002202 goto fail;
2203
William Lallemandec3e3892012-11-12 17:02:18 +01002204 s->flags |= SN_COMP_READY;
2205
William Lallemand82fe75c2012-10-23 10:25:10 +02002206 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002207 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002208 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2209 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2210
2211 /* add Transfer-Encoding header */
2212 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2213 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2214
2215 /*
2216 * Add Content-Encoding header when it's not identity encoding.
2217 * RFC 2616 : Identity encoding: This content-coding is used only in the
2218 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2219 * header.
2220 */
2221 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002222 trash.len = 18;
2223 memcpy(trash.str, "Content-Encoding: ", trash.len);
2224 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2225 trash.len += s->comp_algo->name_len;
2226 trash.str[trash.len] = '\0';
2227 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002228 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 return 1;
2230
2231fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002232 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002233 return 0;
2234}
2235
2236
Willy Tarreaud787e662009-07-07 10:14:51 +02002237/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2238 * processing can continue on next analysers, or zero if it either needs more
2239 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2240 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2241 * when it has nothing left to do, and may remove any analyser when it wants to
2242 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002244int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002245{
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 /*
2247 * We will parse the partial (or complete) lines.
2248 * We will check the request syntax, and also join multi-line
2249 * headers. An index of all the lines will be elaborated while
2250 * parsing.
2251 *
2252 * For the parsing, we use a 28 states FSM.
2253 *
2254 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002255 * req->buf->p = beginning of request
2256 * req->buf->p + msg->eoh = end of processed headers / start of current one
2257 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002258 * msg->eol = end of current header or line (LF or CRLF)
2259 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002260 *
2261 * At end of parsing, we may perform a capture of the error (if any), and
2262 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2263 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2264 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002268 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002269 struct http_txn *txn = &s->txn;
2270 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002271 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002273 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 +01002274 now_ms, __FUNCTION__,
2275 s,
2276 req,
2277 req->rex, req->wex,
2278 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002279 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002280 req->analysers);
2281
Willy Tarreau52a0c602009-08-16 22:45:38 +02002282 /* we're speaking HTTP here, so let's speak HTTP to the client */
2283 s->srv_error = http_return_srv_error;
2284
Willy Tarreau83e3af02009-12-28 17:39:57 +01002285 /* There's a protected area at the end of the buffer for rewriting
2286 * purposes. We don't want to start to parse the request if the
2287 * protected area is affected, because we may have to move processed
2288 * data later, which is much more complicated.
2289 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002290 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002291 if (txn->flags & TX_NOT_FIRST) {
2292 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002293 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002294 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002295 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002296 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002297 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002298 return 0;
2299 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002300 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2301 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2302 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
Willy Tarreau065e8332010-01-08 00:30:20 +01002305 /* Note that we have the same problem with the response ; we
2306 * may want to send a redirect, error or anything which requires
2307 * some spare space. So we'll ensure that we have at least
2308 * maxrewrite bytes available in the response buffer before
2309 * processing that one. This will only affect pipelined
2310 * keep-alive requests.
2311 */
2312 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002313 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002314 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2315 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2316 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002317 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002318 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002319 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002320 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002321 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002322 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002323 return 0;
2324 }
2325 }
2326
Willy Tarreau9b28e032012-10-12 23:49:43 +02002327 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002328 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002329 }
2330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* 1: we might have to print this header in debug mode */
2332 if (unlikely((global.mode & MODE_DEBUG) &&
2333 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002334 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336
Willy Tarreau9b28e032012-10-12 23:49:43 +02002337 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002338 /* this is a bit complex : in case of error on the request line,
2339 * we know that rq.l is still zero, so we display only the part
2340 * up to the end of the line (truncated by debug_hdr).
2341 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002342 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 sol += hdr_idx_first_pos(&txn->hdr_idx);
2346 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 while (cur_idx) {
2349 eol = sol + txn->hdr_idx.v[cur_idx].len;
2350 debug_hdr("clihdr", s, sol, eol);
2351 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2352 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002353 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 }
2355
Willy Tarreau58f10d72006-12-04 02:26:12 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 /*
2358 * Now we quickly check if we have found a full valid request.
2359 * If not so, we check the FD and buffer states before leaving.
2360 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002361 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002362 * requests are checked first. When waiting for a second request
2363 * on a keep-alive session, if we encounter and error, close, t/o,
2364 * we note the error in the session flags but don't set any state.
2365 * Since the error will be noted there, it will not be counted by
2366 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002367 * Last, we may increase some tracked counters' http request errors on
2368 * the cases that are deliberately the client's fault. For instance,
2369 * a timeout or connection reset is not counted as an error. However
2370 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372
Willy Tarreau655dce92009-11-08 13:10:58 +01002373 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002374 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002376 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002377 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002378 session_inc_http_req_ctr(s);
2379 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002380 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /* 1: Since we are in header mode, if there's no space
2385 * left for headers, we won't be able to free more
2386 * later, so the session will never terminate. We
2387 * must terminate it now.
2388 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002389 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* FIXME: check if URI is set and return Status
2391 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002392 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002393 session_inc_http_req_ctr(s);
2394 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002395 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002396 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002397 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 goto return_bad_req;
2399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002402 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002403 if (!(s->flags & SN_ERR_MASK))
2404 s->flags |= SN_ERR_CLICL;
2405
Willy Tarreaufcffa692010-01-10 14:21:19 +01002406 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002407 goto failed_keep_alive;
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002410 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002411 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002412 session_inc_http_err_ctr(s);
2413 }
2414
Willy Tarreaudc979f22012-12-04 10:39:01 +01002415 txn->status = 400;
2416 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 msg->msg_state = HTTP_MSG_ERROR;
2418 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002419
Willy Tarreauda7ff642010-06-23 11:44:09 +02002420 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002421 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002422 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002423 if (s->listener->counters)
2424 s->listener->counters->failed_req++;
2425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 if (!(s->flags & SN_FINST_MASK))
2427 s->flags |= SN_FINST_R;
2428 return 0;
2429 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002432 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002433 if (!(s->flags & SN_ERR_MASK))
2434 s->flags |= SN_ERR_CLITO;
2435
Willy Tarreaufcffa692010-01-10 14:21:19 +01002436 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002437 goto failed_keep_alive;
2438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002440 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002441 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002442 session_inc_http_err_ctr(s);
2443 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002445 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 msg->msg_state = HTTP_MSG_ERROR;
2447 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002448
Willy Tarreauda7ff642010-06-23 11:44:09 +02002449 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002450 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002451 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002452 if (s->listener->counters)
2453 s->listener->counters->failed_req++;
2454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 if (!(s->flags & SN_FINST_MASK))
2456 s->flags |= SN_FINST_R;
2457 return 0;
2458 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002461 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002462 if (!(s->flags & SN_ERR_MASK))
2463 s->flags |= SN_ERR_CLICL;
2464
Willy Tarreaufcffa692010-01-10 14:21:19 +01002465 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002466 goto failed_keep_alive;
2467
Willy Tarreau4076a152009-04-02 15:18:36 +02002468 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002469 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002471 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 msg->msg_state = HTTP_MSG_ERROR;
2473 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_err_ctr(s);
2476 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002478 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002479 if (s->listener->counters)
2480 s->listener->counters->failed_req++;
2481
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 if (!(s->flags & SN_FINST_MASK))
2483 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002484 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485 }
2486
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002487 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002488 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2489 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002490#ifdef TCP_QUICKACK
Willy Tarreauf79c8172013-10-21 16:30:56 +02002491 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && (__objt_conn(s->req->prod->end)->flags & CO_FL_CTRL_READY)) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002492 /* We need more data, we have to re-enable quick-ack in case we
2493 * previously disabled it, otherwise we might cause the client
2494 * to delay next data.
2495 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002496 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002497 }
2498#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002499
Willy Tarreaufcffa692010-01-10 14:21:19 +01002500 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2501 /* If the client starts to talk, let's fall back to
2502 * request timeout processing.
2503 */
2504 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002505 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 }
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002509 if (!tick_isset(req->analyse_exp)) {
2510 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2511 (txn->flags & TX_WAIT_NEXT_RQ) &&
2512 tick_isset(s->be->timeout.httpka))
2513 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2514 else
2515 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002517
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 /* we're not ready yet */
2519 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520
2521 failed_keep_alive:
2522 /* Here we process low-level errors for keep-alive requests. In
2523 * short, if the request is not the first one and it experiences
2524 * a timeout, read error or shutdown, we just silently close so
2525 * that the client can try again.
2526 */
2527 txn->status = 0;
2528 msg->msg_state = HTTP_MSG_RQBEFORE;
2529 req->analysers = 0;
2530 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002531 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002532 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002533 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 /* OK now we have a complete HTTP request with indexed headers. Let's
2538 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002539 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002540 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002541 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002542 * byte after the last LF. msg->sov points to the first byte of data.
2543 * msg->eol cannot be trusted because it may have been left uninitialized
2544 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002545 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002546
Willy Tarreauda7ff642010-06-23 11:44:09 +02002547 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002548 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2549
Willy Tarreaub16a5742010-01-10 14:46:16 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ) {
2551 /* kill the pending keep-alive timeout */
2552 txn->flags &= ~TX_WAIT_NEXT_RQ;
2553 req->analyse_exp = TICK_ETERNITY;
2554 }
2555
2556
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 /* Maybe we found in invalid header name while we were configured not
2558 * to block on that, so we have to capture it now.
2559 */
2560 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002561 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /*
2564 * 1: identify the method
2565 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002566 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567
2568 /* we can make use of server redirect on GET and HEAD */
2569 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2570 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * 2: check if the URI matches the monitor_uri.
2574 * We have to do this for every request which gets in, because
2575 * the monitor-uri is defined by the frontend.
2576 */
2577 if (unlikely((s->fe->monitor_uri_len != 0) &&
2578 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002579 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 s->fe->monitor_uri,
2581 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002584 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002588 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002589
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002591 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002592 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 ret = acl_pass(ret);
2595 if (cond->pol == ACL_COND_UNLESS)
2596 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 if (ret) {
2599 /* we fail this request, let's return 503 service unavail */
2600 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002601 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002602 if (!(s->flags & SN_ERR_MASK))
2603 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002605 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002606 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* nothing to fail, let's reply normaly */
2609 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002610 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002611 if (!(s->flags & SN_ERR_MASK))
2612 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_prx_cond;
2614 }
2615
2616 /*
2617 * 3: Maybe we have to copy the original REQURI for the logs ?
2618 * Note: we cannot log anymore if the request has been
2619 * classified as invalid.
2620 */
2621 if (unlikely(s->logs.logwait & LW_REQ)) {
2622 /* we have a complete HTTP request that we must log */
2623 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2624 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 if (urilen >= REQURI_LEN)
2627 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002630
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002631 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 s->do_log(s);
2633 } else {
2634 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
Willy Tarreau06619262006-12-17 08:37:22 +01002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002639 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002640 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002641
Willy Tarreau5b154472009-12-21 20:11:07 +01002642 /* ... and check if the request is HTTP/1.1 or above */
2643 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002644 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2645 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2646 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002647 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002648
2649 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002650 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 +01002651
Willy Tarreau88d349d2010-01-25 12:15:43 +01002652 /* if the frontend has "option http-use-proxy-header", we'll check if
2653 * we have what looks like a proxied connection instead of a connection,
2654 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2655 * Note that this is *not* RFC-compliant, however browsers and proxies
2656 * happen to do that despite being non-standard :-(
2657 * We consider that a request not beginning with either '/' or '*' is
2658 * a proxied connection, which covers both "scheme://location" and
2659 * CONNECT ip:port.
2660 */
2661 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002663 txn->flags |= TX_USE_PX_CONN;
2664
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002665 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002666 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002669 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002670 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002672
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673 /* 6: determine the transfer-length.
2674 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2675 * the presence of a message-body in a REQUEST and its transfer length
2676 * must be determined that way (in order of precedence) :
2677 * 1. The presence of a message-body in a request is signaled by the
2678 * inclusion of a Content-Length or Transfer-Encoding header field
2679 * in the request's header fields. When a request message contains
2680 * both a message-body of non-zero length and a method that does
2681 * not define any semantics for that request message-body, then an
2682 * origin server SHOULD either ignore the message-body or respond
2683 * with an appropriate error message (e.g., 413). A proxy or
2684 * gateway, when presented the same request, SHOULD either forward
2685 * the request inbound with the message- body or ignore the
2686 * message-body when determining a response.
2687 *
2688 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2689 * and the "chunked" transfer-coding (Section 6.2) is used, the
2690 * transfer-length is defined by the use of this transfer-coding.
2691 * If a Transfer-Encoding header field is present and the "chunked"
2692 * transfer-coding is not present, the transfer-length is defined
2693 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002694 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002695 * 3. If a Content-Length header field is present, its decimal value in
2696 * OCTETs represents both the entity-length and the transfer-length.
2697 * If a message is received with both a Transfer-Encoding header
2698 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002699 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002700 * 4. By the server closing the connection. (Closing the connection
2701 * cannot be used to indicate the end of a request body, since that
2702 * would leave no possibility for the server to send back a response.)
2703 *
2704 * Whenever a transfer-coding is applied to a message-body, the set of
2705 * transfer-codings MUST include "chunked", unless the message indicates
2706 * it is terminated by closing the connection. When the "chunked"
2707 * transfer-coding is used, it MUST be the last transfer-coding applied
2708 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002709 */
2710
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002711 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002712 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002713 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002714 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002716 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002717 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2718 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* bad transfer-encoding (chunked followed by something else) */
2720 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002721 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 break;
2723 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002724 }
2725
Willy Tarreau32b47f42009-10-18 20:55:02 +02002726 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002729 signed long long cl;
2730
Willy Tarreauad14f752011-09-02 20:33:27 +02002731 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002732 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002733 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002734 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735
Willy Tarreauad14f752011-09-02 20:33:27 +02002736 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002737 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002738 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002739 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002740
Willy Tarreauad14f752011-09-02 20:33:27 +02002741 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002742 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002743 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002744 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002745
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002746 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002747 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002749 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002750
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002751 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002752 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002753 }
2754
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002755 /* bodyless requests have a known length */
2756 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002757 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002758
Willy Tarreaud787e662009-07-07 10:14:51 +02002759 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002760 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 req->analyse_exp = TICK_ETERNITY;
2762 return 1;
2763
2764 return_bad_req:
2765 /* We centralize bad requests processing here */
2766 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2767 /* we detected a parsing error. We want to archive this request
2768 * in the dedicated proxy area for later troubleshooting.
2769 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002770 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 }
2772
2773 txn->req.msg_state = HTTP_MSG_ERROR;
2774 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002775 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002776
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002777 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002778 if (s->listener->counters)
2779 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002780
2781 return_prx_cond:
2782 if (!(s->flags & SN_ERR_MASK))
2783 s->flags |= SN_ERR_PRXCOND;
2784 if (!(s->flags & SN_FINST_MASK))
2785 s->flags |= SN_FINST_R;
2786
2787 req->analysers = 0;
2788 req->analyse_exp = TICK_ETERNITY;
2789 return 0;
2790}
2791
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002792
Willy Tarreau347a35d2013-11-22 17:51:09 +01002793/* This function prepares an applet to handle the stats. It can deal with the
2794 * "100-continue" expectation, check that admin rules are met for POST requests,
2795 * and program a response message if something was unexpected. It cannot fail
2796 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002797 * analysers nor counters, which are left to the caller. It does not touch
2798 * s->target which is supposed to already point to the stats applet.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002799 */
2800int http_handle_stats(struct session *s, struct channel *req)
2801{
2802 struct stats_admin_rule *stats_admin_rule;
2803 struct stream_interface *si = s->rep->prod;
2804 struct http_txn *txn = &s->txn;
2805 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002806 struct uri_auth *uri_auth = s->be->uri_auth;
2807 const char *uri, *h, *lookup;
2808
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002809 memset(&si->appctx.ctx.stats, 0, sizeof(si->appctx.ctx.stats));
2810 si->appctx.ctx.stats.st_code = STAT_STATUS_INIT;
2811 si->appctx.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002812
2813 uri = msg->chn->buf->p + msg->sl.rq.u;
2814 lookup = uri + uri_auth->uri_len;
2815
2816 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2817 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002818 si->appctx.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002819 break;
2820 }
2821 }
2822
2823 if (uri_auth->refresh) {
2824 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2825 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002826 si->appctx.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002827 break;
2828 }
2829 }
2830 }
2831
2832 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2833 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002834 si->appctx.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002835 break;
2836 }
2837 }
2838
2839 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2840 if (memcmp(h, ";st=", 4) == 0) {
2841 int i;
2842 h += 4;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002843 si->appctx.ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002844 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2845 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002846 si->appctx.ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002847 break;
2848 }
2849 }
2850 break;
2851 }
2852 }
2853
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002854 si->appctx.ctx.stats.scope_str = 0;
2855 si->appctx.ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002856 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2857 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2858 int itx = 0;
2859 const char *h2;
2860 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2861 const char *err;
2862
2863 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2864 h2 = h;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002865 si->appctx.ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002866 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2867 itx++;
2868 h++;
2869 }
2870
2871 if (itx > STAT_SCOPE_TXT_MAXLEN)
2872 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002873 si->appctx.ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002874
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002875 /* scope_txt = search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002876 memcpy(scope_txt, h2, itx);
2877 scope_txt[itx] = '\0';
2878 err = invalid_char(scope_txt);
2879 if (err) {
2880 /* bad char in search text => clear scope */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002881 si->appctx.ctx.stats.scope_str = 0;
2882 si->appctx.ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002883 }
2884 break;
2885 }
2886 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002887
2888 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002889 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002890 int ret = 1;
2891
2892 if (stats_admin_rule->cond) {
2893 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2894 ret = acl_pass(ret);
2895 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2896 ret = !ret;
2897 }
2898
2899 if (ret) {
2900 /* no rule, or the rule matches */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002901 s->rep->prod->appctx.ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002902 break;
2903 }
2904 }
2905
2906 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002907 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002908 if (si->appctx.ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002909 if (msg->msg_state < HTTP_MSG_100_SENT) {
2910 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2911 * send an HTTP/1.1 100 Continue intermediate response.
2912 */
2913 if (msg->flags & HTTP_MSGF_VER_11) {
2914 struct hdr_ctx ctx;
2915 ctx.idx = 0;
2916 /* Expect is allowed in 1.1, look for it */
2917 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2918 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2919 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2920 }
2921 }
2922 msg->msg_state = HTTP_MSG_100_SENT;
2923 s->logs.tv_request = now; /* update the request timer to reflect full request */
2924 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002925 s->rep->prod->appctx.st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002926 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002927 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002928 si->appctx.ctx.stats.st_code = STAT_STATUS_DENY;
2929 s->rep->prod->appctx.st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002930 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002931 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002932 else {
2933 /* So it was another method (GET/HEAD) */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002934 s->rep->prod->appctx.st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002935 }
2936
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002937 s->task->nice = -32; /* small boost for HTTP statistics */
2938 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002939 s->rep->prod->appctx.st1 = s->rep->prod->appctx.st2 = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002940 return 1;
2941}
2942
Lukas Tribus67db8df2013-06-23 17:37:13 +02002943/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2944 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2945 */
2946static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2947{
2948#ifdef IP_TOS
2949 if (from.ss_family == AF_INET)
2950 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2951#endif
2952#ifdef IPV6_TCLASS
2953 if (from.ss_family == AF_INET6) {
2954 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2955 /* v4-mapped addresses need IP_TOS */
2956 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2957 else
2958 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2959 }
2960#endif
2961}
2962
Willy Tarreau20b0de52012-12-24 15:45:22 +01002963/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002964 * transaction <txn>. Returns the first rule that prevents further processing
2965 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2966 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2967 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002968 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002969static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002970http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002971{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002972 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002973 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002974 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002975
Willy Tarreauff011f22011-01-06 17:51:27 +01002976 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002977 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002978 continue;
2979
Willy Tarreau96257ec2012-12-27 10:46:37 +01002980 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002981 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002982 int ret;
2983
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002984 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002985 ret = acl_pass(ret);
2986
Willy Tarreauff011f22011-01-06 17:51:27 +01002987 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002988 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002989
2990 if (!ret) /* condition not matched */
2991 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002992 }
2993
Willy Tarreau20b0de52012-12-24 15:45:22 +01002994
Willy Tarreau96257ec2012-12-27 10:46:37 +01002995 switch (rule->action) {
2996 case HTTP_REQ_ACT_ALLOW:
2997 return NULL; /* "allow" rules are OK */
2998
2999 case HTTP_REQ_ACT_DENY:
3000 txn->flags |= TX_CLDENY;
3001 return rule;
3002
Willy Tarreauccbcc372012-12-27 12:37:57 +01003003 case HTTP_REQ_ACT_TARPIT:
3004 txn->flags |= TX_CLTARPIT;
3005 return rule;
3006
Willy Tarreau96257ec2012-12-27 10:46:37 +01003007 case HTTP_REQ_ACT_AUTH:
3008 return rule;
3009
Willy Tarreau81499eb2012-12-27 12:19:02 +01003010 case HTTP_REQ_ACT_REDIR:
3011 return rule;
3012
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003013 case HTTP_REQ_ACT_SET_NICE:
3014 s->task->nice = rule->arg.nice;
3015 break;
3016
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003017 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003018 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003019 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003020 break;
3021
Willy Tarreau51347ed2013-06-11 19:34:13 +02003022 case HTTP_REQ_ACT_SET_MARK:
3023#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003024 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003025 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003026#endif
3027 break;
3028
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003029 case HTTP_REQ_ACT_SET_LOGL:
3030 s->logs.level = rule->arg.loglevel;
3031 break;
3032
Willy Tarreau96257ec2012-12-27 10:46:37 +01003033 case HTTP_REQ_ACT_SET_HDR:
3034 ctx.idx = 0;
3035 /* remove all occurrences of the header */
3036 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3037 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3038 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003039 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003040 /* now fall through to header addition */
3041
3042 case HTTP_REQ_ACT_ADD_HDR:
3043 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3044 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3045 trash.len = rule->arg.hdr_add.name_len;
3046 trash.str[trash.len++] = ':';
3047 trash.str[trash.len++] = ' ';
3048 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3049 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3050 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003051 }
3052 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003053
3054 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003055 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003056}
3057
Willy Tarreau71241ab2012-12-27 11:30:54 +01003058
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003059/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3060 * transaction <txn>. Returns the first rule that prevents further processing
3061 * of the response (deny, ...) or NULL if it executed all rules or stopped
3062 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3063 * rule.
3064 */
3065static struct http_res_rule *
3066http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3067{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003068 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003069 struct http_res_rule *rule;
3070 struct hdr_ctx ctx;
3071
3072 list_for_each_entry(rule, rules, list) {
3073 if (rule->action >= HTTP_RES_ACT_MAX)
3074 continue;
3075
3076 /* check optional condition */
3077 if (rule->cond) {
3078 int ret;
3079
3080 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3081 ret = acl_pass(ret);
3082
3083 if (rule->cond->pol == ACL_COND_UNLESS)
3084 ret = !ret;
3085
3086 if (!ret) /* condition not matched */
3087 continue;
3088 }
3089
3090
3091 switch (rule->action) {
3092 case HTTP_RES_ACT_ALLOW:
3093 return NULL; /* "allow" rules are OK */
3094
3095 case HTTP_RES_ACT_DENY:
3096 txn->flags |= TX_SVDENY;
3097 return rule;
3098
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003099 case HTTP_RES_ACT_SET_NICE:
3100 s->task->nice = rule->arg.nice;
3101 break;
3102
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003103 case HTTP_RES_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003104 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003105 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003106 break;
3107
Willy Tarreau51347ed2013-06-11 19:34:13 +02003108 case HTTP_RES_ACT_SET_MARK:
3109#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003110 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003111 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003112#endif
3113 break;
3114
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003115 case HTTP_RES_ACT_SET_LOGL:
3116 s->logs.level = rule->arg.loglevel;
3117 break;
3118
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003119 case HTTP_RES_ACT_SET_HDR:
3120 ctx.idx = 0;
3121 /* remove all occurrences of the header */
3122 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3123 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3124 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3125 }
3126 /* now fall through to header addition */
3127
3128 case HTTP_RES_ACT_ADD_HDR:
3129 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3130 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3131 trash.len = rule->arg.hdr_add.name_len;
3132 trash.str[trash.len++] = ':';
3133 trash.str[trash.len++] = ' ';
3134 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3135 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3136 break;
3137 }
3138 }
3139
3140 /* we reached the end of the rules, nothing to report */
3141 return NULL;
3142}
3143
3144
Willy Tarreau71241ab2012-12-27 11:30:54 +01003145/* Perform an HTTP redirect based on the information in <rule>. The function
3146 * returns non-zero on success, or zero in case of a, irrecoverable error such
3147 * as too large a request to build a valid response.
3148 */
3149static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3150{
3151 struct http_msg *msg = &txn->req;
3152 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003153 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003154
3155 /* build redirect message */
3156 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003157 case 308:
3158 msg_fmt = HTTP_308;
3159 break;
3160 case 307:
3161 msg_fmt = HTTP_307;
3162 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163 case 303:
3164 msg_fmt = HTTP_303;
3165 break;
3166 case 301:
3167 msg_fmt = HTTP_301;
3168 break;
3169 case 302:
3170 default:
3171 msg_fmt = HTTP_302;
3172 break;
3173 }
3174
3175 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3176 return 0;
3177
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003178 location = trash.str + trash.len;
3179
Willy Tarreau71241ab2012-12-27 11:30:54 +01003180 switch(rule->type) {
3181 case REDIRECT_TYPE_SCHEME: {
3182 const char *path;
3183 const char *host;
3184 struct hdr_ctx ctx;
3185 int pathlen;
3186 int hostlen;
3187
3188 host = "";
3189 hostlen = 0;
3190 ctx.idx = 0;
3191 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3192 host = ctx.line + ctx.val;
3193 hostlen = ctx.vlen;
3194 }
3195
3196 path = http_get_path(txn);
3197 /* build message using path */
3198 if (path) {
3199 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3200 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3201 int qs = 0;
3202 while (qs < pathlen) {
3203 if (path[qs] == '?') {
3204 pathlen = qs;
3205 break;
3206 }
3207 qs++;
3208 }
3209 }
3210 } else {
3211 path = "/";
3212 pathlen = 1;
3213 }
3214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003215 if (rule->rdr_str) { /* this is an old "redirect" rule */
3216 /* check if we can add scheme + "://" + host + path */
3217 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3218 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003219
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003220 /* add scheme */
3221 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3222 trash.len += rule->rdr_len;
3223 }
3224 else {
3225 /* add scheme with executing log format */
3226 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* check if we can add scheme + "://" + host + path */
3229 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3230 return 0;
3231 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232 /* add "://" */
3233 memcpy(trash.str + trash.len, "://", 3);
3234 trash.len += 3;
3235
3236 /* add host */
3237 memcpy(trash.str + trash.len, host, hostlen);
3238 trash.len += hostlen;
3239
3240 /* add path */
3241 memcpy(trash.str + trash.len, path, pathlen);
3242 trash.len += pathlen;
3243
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003244 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003245 if (trash.len && trash.str[trash.len - 1] != '/' &&
3246 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3247 if (trash.len > trash.size - 5)
3248 return 0;
3249 trash.str[trash.len] = '/';
3250 trash.len++;
3251 }
3252
3253 break;
3254 }
3255 case REDIRECT_TYPE_PREFIX: {
3256 const char *path;
3257 int pathlen;
3258
3259 path = http_get_path(txn);
3260 /* build message using path */
3261 if (path) {
3262 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3263 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3264 int qs = 0;
3265 while (qs < pathlen) {
3266 if (path[qs] == '?') {
3267 pathlen = qs;
3268 break;
3269 }
3270 qs++;
3271 }
3272 }
3273 } else {
3274 path = "/";
3275 pathlen = 1;
3276 }
3277
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003278 if (rule->rdr_str) { /* this is an old "redirect" rule */
3279 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3280 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003281
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003282 /* add prefix. Note that if prefix == "/", we don't want to
3283 * add anything, otherwise it makes it hard for the user to
3284 * configure a self-redirection.
3285 */
3286 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3287 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3288 trash.len += rule->rdr_len;
3289 }
3290 }
3291 else {
3292 /* add prefix with executing log format */
3293 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3294
3295 /* Check length */
3296 if (trash.len + pathlen > trash.size - 4)
3297 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003298 }
3299
3300 /* add path */
3301 memcpy(trash.str + trash.len, path, pathlen);
3302 trash.len += pathlen;
3303
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003304 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003305 if (trash.len && trash.str[trash.len - 1] != '/' &&
3306 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3307 if (trash.len > trash.size - 5)
3308 return 0;
3309 trash.str[trash.len] = '/';
3310 trash.len++;
3311 }
3312
3313 break;
3314 }
3315 case REDIRECT_TYPE_LOCATION:
3316 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003317 if (rule->rdr_str) { /* this is an old "redirect" rule */
3318 if (trash.len + rule->rdr_len > trash.size - 4)
3319 return 0;
3320
3321 /* add location */
3322 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3323 trash.len += rule->rdr_len;
3324 }
3325 else {
3326 /* add location with executing log format */
3327 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003329 /* Check left length */
3330 if (trash.len > trash.size - 4)
3331 return 0;
3332 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 break;
3334 }
3335
3336 if (rule->cookie_len) {
3337 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3338 trash.len += 14;
3339 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3340 trash.len += rule->cookie_len;
3341 memcpy(trash.str + trash.len, "\r\n", 2);
3342 trash.len += 2;
3343 }
3344
3345 /* add end of headers and the keep-alive/close status.
3346 * We may choose to set keep-alive if the Location begins
3347 * with a slash, because the client will come back to the
3348 * same server.
3349 */
3350 txn->status = rule->code;
3351 /* let's log the request time */
3352 s->logs.tv_request = now;
3353
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003354 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003355 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3356 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3357 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3358 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3359 /* keep-alive possible */
3360 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3361 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3362 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3363 trash.len += 30;
3364 } else {
3365 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3366 trash.len += 24;
3367 }
3368 }
3369 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3370 trash.len += 4;
3371 bo_inject(txn->rsp.chn, trash.str, trash.len);
3372 /* "eat" the request */
3373 bi_fast_delete(txn->req.chn->buf, msg->sov);
3374 msg->sov = 0;
3375 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3376 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3377 txn->req.msg_state = HTTP_MSG_CLOSED;
3378 txn->rsp.msg_state = HTTP_MSG_DONE;
3379 } else {
3380 /* keep-alive not possible */
3381 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3382 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3383 trash.len += 29;
3384 } else {
3385 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3386 trash.len += 23;
3387 }
3388 stream_int_retnclose(txn->req.chn->prod, &trash);
3389 txn->req.chn->analysers = 0;
3390 }
3391
3392 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003393 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003394 if (!(s->flags & SN_FINST_MASK))
3395 s->flags |= SN_FINST_R;
3396
3397 return 1;
3398}
3399
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003400/* This stream analyser runs all HTTP request processing which is common to
3401 * frontends and backends, which means blocking ACLs, filters, connection-close,
3402 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003403 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003404 * either needs more data or wants to immediately abort the request (eg: deny,
3405 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003406 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003407int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003408{
Willy Tarreaud787e662009-07-07 10:14:51 +02003409 struct http_txn *txn = &s->txn;
3410 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003411 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003412 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003413 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003414 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003415
Willy Tarreau655dce92009-11-08 13:10:58 +01003416 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003417 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003418 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003419 return 0;
3420 }
3421
Willy Tarreau3a816292009-07-07 10:55:49 +02003422 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003423 req->analyse_exp = TICK_ETERNITY;
3424
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003425 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 +02003426 now_ms, __FUNCTION__,
3427 s,
3428 req,
3429 req->rex, req->wex,
3430 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003431 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003432 req->analysers);
3433
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003434 /* first check whether we have some ACLs set to block this request */
3435 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003436 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003437
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003438 ret = acl_pass(ret);
3439 if (cond->pol == ACL_COND_UNLESS)
3440 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003441
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003442 if (ret) {
3443 txn->status = 403;
3444 /* let's log the request time */
3445 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003446 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003447 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003448 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003449 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003451
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003452 /* just in case we have some per-backend tracking */
3453 session_inc_be_http_req_ctr(s);
3454
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003455 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003457
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003458 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003459 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003460 if (stats_check_uri(s->rep->prod, txn, px)) {
3461 s->target = &http_stats_applet.obj_type;
3462 /* parse the whole stats request and extract the relevant information */
3463 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003465 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003466 }
3467
Willy Tarreau3b44e722013-11-16 10:28:23 +01003468 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3469 * blocked by an http-request rule.
3470 */
3471 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003472 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003473 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003474 }
Willy Tarreau06619262006-12-17 08:37:22 +01003475
Willy Tarreau3b44e722013-11-16 10:28:23 +01003476 /* return a 403 if either rule has blocked */
3477 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003478 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003480 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003481 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003482 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003483 s->fe->fe_counters.denied_req++;
3484 if (s->fe != s->be)
3485 s->be->be_counters.denied_req++;
3486 if (s->listener->counters)
3487 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003488 goto return_prx_cond;
3489 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003490
3491 /* When a connection is tarpitted, we use the tarpit timeout,
3492 * which may be the same as the connect timeout if unspecified.
3493 * If unset, then set it to zero because we really want it to
3494 * eventually expire. We build the tarpit as an analyser.
3495 */
3496 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003497 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003498 /* wipe the request out so that we can drop the connection early
3499 * if the client closes first.
3500 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003501 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003502 req->analysers = 0; /* remove switching rules etc... */
3503 req->analysers |= AN_REQ_HTTP_TARPIT;
3504 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3505 if (!req->analyse_exp)
3506 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003507 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003508 s->fe->fe_counters.denied_req++;
3509 if (s->fe != s->be)
3510 s->be->be_counters.denied_req++;
3511 if (s->listener->counters)
3512 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003513 return 1;
3514 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003515 }
Willy Tarreau06619262006-12-17 08:37:22 +01003516
Willy Tarreau5b154472009-12-21 20:11:07 +01003517 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3518 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003519 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3520 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003521 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3522 * avoid to redo the same work if FE and BE have the same settings (common).
3523 * The method consists in checking if options changed between the two calls
3524 * (implying that either one is non-null, or one of them is non-null and we
3525 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003526 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003527
Willy Tarreaudc008c52010-02-01 16:20:08 +01003528 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3529 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3530 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3531 (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 +01003532 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003533
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003534 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3535 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003536 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003537 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3538 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003539 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003540 tmp = TX_CON_WANT_CLO;
3541
Willy Tarreau5b154472009-12-21 20:11:07 +01003542 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3543 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003544
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003545 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3546 /* parse the Connection header and possibly clean it */
3547 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003548 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003549 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3550 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003551 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003552 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003553 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003554 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003555 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003556
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003557 /* check if client or config asks for explicit close in KAL/SCL */
3558 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3559 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3560 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003561 (!(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 +01003562 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003563 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003564 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003565 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3566 }
Willy Tarreau78599912009-10-17 20:12:21 +02003567
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003568 /* we can be blocked here because the request needs to be authenticated,
3569 * either to pass or to access stats.
3570 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003571 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003572 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003573
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003574 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003575 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003577 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003578 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003579 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003580 /* on 401 we still count one error, because normal browsing
3581 * won't significantly increase the counter but brute force
3582 * attempts will.
3583 */
3584 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003585 goto return_prx_cond;
3586 }
3587
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003588 /* add request headers from the rule sets in the same order */
3589 list_for_each_entry(wl, &px->req_add, list) {
3590 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003591 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003592 ret = acl_pass(ret);
3593 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3594 ret = !ret;
3595 if (!ret)
3596 continue;
3597 }
3598
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003599 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003600 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003601 }
3602
3603 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3604 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3605 goto return_bad_req;
3606 req->analyse_exp = TICK_ETERNITY;
3607 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003608 }
3609
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003610 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003611 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003612 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3613 s->fe->fe_counters.intercepted_req++;
3614
3615 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3616 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3617 if (!(s->flags & SN_FINST_MASK))
3618 s->flags |= SN_FINST_R;
3619
3620 req->analyse_exp = TICK_ETERNITY;
3621 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003622 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003623 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003624
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625 /* check whether we have some ACLs set to redirect this request */
3626 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003627 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003628 int ret;
3629
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003630 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003631 ret = acl_pass(ret);
3632 if (rule->cond->pol == ACL_COND_UNLESS)
3633 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003634 if (!ret)
3635 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003636 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003637 if (!http_apply_redirect_rule(rule, s, txn))
3638 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003639
Willy Tarreau71241ab2012-12-27 11:30:54 +01003640 req->analyse_exp = TICK_ETERNITY;
3641 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003643
Willy Tarreau2be39392010-01-03 17:24:51 +01003644 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3645 * If this happens, then the data will not come immediately, so we must
3646 * send all what we have without waiting. Note that due to the small gain
3647 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003648 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003649 * itself once used.
3650 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003651 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003652
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003653 /* that's OK for us now, let's move on to next analysers */
3654 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003655
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003656 return_bad_req:
3657 /* We centralize bad requests processing here */
3658 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3659 /* we detected a parsing error. We want to archive this request
3660 * in the dedicated proxy area for later troubleshooting.
3661 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003662 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003663 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003664
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 txn->req.msg_state = HTTP_MSG_ERROR;
3666 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003667 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003668
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003669 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003670 if (s->listener->counters)
3671 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 return_prx_cond:
3674 if (!(s->flags & SN_ERR_MASK))
3675 s->flags |= SN_ERR_PRXCOND;
3676 if (!(s->flags & SN_FINST_MASK))
3677 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003678
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003679 req->analysers = 0;
3680 req->analyse_exp = TICK_ETERNITY;
3681 return 0;
3682}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003683
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003684/* This function performs all the processing enabled for the current request.
3685 * It returns 1 if the processing can continue on next analysers, or zero if it
3686 * needs more data, encounters an error, or wants to immediately abort the
3687 * request. It relies on buffers flags, and updates s->req->analysers.
3688 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003689int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003690{
3691 struct http_txn *txn = &s->txn;
3692 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003693 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003694
Willy Tarreau655dce92009-11-08 13:10:58 +01003695 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003696 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003697 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003698 return 0;
3699 }
3700
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003701 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 +02003702 now_ms, __FUNCTION__,
3703 s,
3704 req,
3705 req->rex, req->wex,
3706 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003707 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003708 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003709
William Lallemand82fe75c2012-10-23 10:25:10 +02003710 if (s->fe->comp || s->be->comp)
3711 select_compression_request_header(s, req->buf);
3712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 /*
3714 * Right now, we know that we have processed the entire headers
3715 * and that unwanted requests have been filtered out. We can do
3716 * whatever we want with the remaining request. Also, now we
3717 * may have separate values for ->fe, ->be.
3718 */
Willy Tarreau06619262006-12-17 08:37:22 +01003719
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003721 * If HTTP PROXY is set we simply get remote server address parsing
3722 * incoming request. Note that this requires that a connection is
3723 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003724 */
3725 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003726 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 +01003727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003728
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003730 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003731 * Note that doing so might move headers in the request, but
3732 * the fields will stay coherent and the URI will not move.
3733 * This should only be performed in the backend.
3734 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003735 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003736 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3737 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003738
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003740 * 8: the appsession cookie was looked up very early in 1.2,
3741 * so let's do the same now.
3742 */
3743
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003744 /* It needs to look into the URI unless persistence must be ignored */
3745 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003746 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 +01003747 }
3748
William Lallemanda73203e2012-03-12 12:48:57 +01003749 /* add unique-id if "header-unique-id" is specified */
3750
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003751 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3752 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3753 goto return_bad_req;
3754 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003755 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003756 }
William Lallemanda73203e2012-03-12 12:48:57 +01003757
3758 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003759 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3760 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003761 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003762 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003763 goto return_bad_req;
3764 }
3765
Cyril Bontéb21570a2009-11-29 20:04:48 +01003766 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003767 * 9: add X-Forwarded-For if either the frontend or the backend
3768 * asks for it.
3769 */
3770 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003771 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003772 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003773 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3774 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003775 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003776 /* The header is set to be added only if none is present
3777 * and we found it, so don't do anything.
3778 */
3779 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003780 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003781 /* Add an X-Forwarded-For header unless the source IP is
3782 * in the 'except' network range.
3783 */
3784 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003785 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003786 != s->fe->except_net.s_addr) &&
3787 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003788 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003789 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003790 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003791 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003792 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003793
3794 /* Note: we rely on the backend to get the header name to be used for
3795 * x-forwarded-for, because the header is really meant for the backends.
3796 * However, if the backend did not specify any option, we have to rely
3797 * on the frontend's header name.
3798 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 if (s->be->fwdfor_hdr_len) {
3800 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003801 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003802 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003804 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003805 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003806 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003807
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003808 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003809 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003810 }
3811 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003813 /* FIXME: for the sake of completeness, we should also support
3814 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003815 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003816 int len;
3817 char pn[INET6_ADDRSTRLEN];
3818 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003819 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003820 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003821
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 /* Note: we rely on the backend to get the header name to be used for
3823 * x-forwarded-for, because the header is really meant for the backends.
3824 * However, if the backend did not specify any option, we have to rely
3825 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003826 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003827 if (s->be->fwdfor_hdr_len) {
3828 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003829 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 } else {
3831 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003832 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003833 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003834 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003835
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003836 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003837 goto return_bad_req;
3838 }
3839 }
3840
3841 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003842 * 10: add X-Original-To if either the frontend or the backend
3843 * asks for it.
3844 */
3845 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3846
3847 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003848 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003849 /* Add an X-Original-To header unless the destination IP is
3850 * in the 'except' network range.
3851 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003852 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003853
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003854 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003855 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003856 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003857 != s->fe->except_to.s_addr) &&
3858 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003859 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003860 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003861 int len;
3862 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003863 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003864
3865 /* Note: we rely on the backend to get the header name to be used for
3866 * x-original-to, because the header is really meant for the backends.
3867 * However, if the backend did not specify any option, we have to rely
3868 * on the frontend's header name.
3869 */
3870 if (s->be->orgto_hdr_len) {
3871 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003872 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003873 } else {
3874 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003875 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003876 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003877 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003878
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003879 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003880 goto return_bad_req;
3881 }
3882 }
3883 }
3884
Willy Tarreau50fc7772012-11-11 22:19:57 +01003885 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3886 * If an "Upgrade" token is found, the header is left untouched in order not to have
3887 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3888 * "Upgrade" is present in the Connection header.
3889 */
3890 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3891 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3892 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003893 unsigned int want_flags = 0;
3894
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003895 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003896 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3897 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3898 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003899 want_flags |= TX_CON_CLO_SET;
3900 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003901 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3902 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3903 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003904 want_flags |= TX_CON_KAL_SET;
3905 }
3906
3907 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003908 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003909 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003910
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003911
Willy Tarreau522d6c02009-12-06 18:49:18 +01003912 /* If we have no server assigned yet and we're balancing on url_param
3913 * with a POST request, we may be interested in checking the body for
3914 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 */
3916 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3917 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003918 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003919 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003920 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003921 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003922 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003923
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003924 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003925 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003926#ifdef TCP_QUICKACK
3927 /* We expect some data from the client. Unless we know for sure
3928 * we already have a full request, we have to re-enable quick-ack
3929 * in case we previously disabled it, otherwise we might cause
3930 * the client to delay further data.
3931 */
3932 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003933 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003934 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003935 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003936 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003937#endif
3938 }
Willy Tarreau03945942009-12-22 16:50:27 +01003939
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 /*************************************************************
3941 * OK, that's finished for the headers. We have done what we *
3942 * could. Let's switch to the DATA state. *
3943 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003944 req->analyse_exp = TICK_ETERNITY;
3945 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003946
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003947 /* if the server closes the connection, we want to immediately react
3948 * and close the socket to save packets and syscalls.
3949 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003950 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3951 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003952
Willy Tarreau59234e92008-11-30 23:51:27 +01003953 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003954 /* OK let's go on with the BODY now */
3955 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003956
Willy Tarreau59234e92008-11-30 23:51:27 +01003957 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003958 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003959 /* we detected a parsing error. We want to archive this request
3960 * in the dedicated proxy area for later troubleshooting.
3961 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003962 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003963 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003964
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 txn->req.msg_state = HTTP_MSG_ERROR;
3966 txn->status = 400;
3967 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003968 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003969
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003970 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003971 if (s->listener->counters)
3972 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003973
Willy Tarreau59234e92008-11-30 23:51:27 +01003974 if (!(s->flags & SN_ERR_MASK))
3975 s->flags |= SN_ERR_PRXCOND;
3976 if (!(s->flags & SN_FINST_MASK))
3977 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003978 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003979}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003980
Willy Tarreau60b85b02008-11-30 23:28:40 +01003981/* This function is an analyser which processes the HTTP tarpit. It always
3982 * returns zero, at the beginning because it prevents any other processing
3983 * from occurring, and at the end because it terminates the request.
3984 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003985int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003986{
3987 struct http_txn *txn = &s->txn;
3988
3989 /* This connection is being tarpitted. The CLIENT side has
3990 * already set the connect expiration date to the right
3991 * timeout. We just have to check that the client is still
3992 * there and that the timeout has not expired.
3993 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003994 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003995 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003996 !tick_is_expired(req->analyse_exp, now_ms))
3997 return 0;
3998
3999 /* We will set the queue timer to the time spent, just for
4000 * logging purposes. We fake a 500 server error, so that the
4001 * attacker will not suspect his connection has been tarpitted.
4002 * It will not cause trouble to the logs because we can exclude
4003 * the tarpitted connections by filtering on the 'PT' status flags.
4004 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004005 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4006
4007 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004008 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004009 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004010
4011 req->analysers = 0;
4012 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004013
Willy Tarreau60b85b02008-11-30 23:28:40 +01004014 if (!(s->flags & SN_ERR_MASK))
4015 s->flags |= SN_ERR_PRXCOND;
4016 if (!(s->flags & SN_FINST_MASK))
4017 s->flags |= SN_FINST_T;
4018 return 0;
4019}
4020
Willy Tarreaud34af782008-11-30 23:36:37 +01004021/* This function is an analyser which processes the HTTP request body. It looks
4022 * for parameters to be used for the load balancing algorithm (url_param). It
4023 * must only be called after the standard HTTP request processing has occurred,
4024 * because it expects the request to be parsed. It returns zero if it needs to
4025 * read more data, or 1 once it has completed its analysis.
4026 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004027int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004028{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004029 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004030 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004031 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004032
4033 /* We have to parse the HTTP request body to find any required data.
4034 * "balance url_param check_post" should have been the only way to get
4035 * into this. We were brought here after HTTP header analysis, so all
4036 * related structures are ready.
4037 */
4038
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4040 goto missing_data;
4041
4042 if (msg->msg_state < HTTP_MSG_100_SENT) {
4043 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4044 * send an HTTP/1.1 100 Continue intermediate response.
4045 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004046 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004047 struct hdr_ctx ctx;
4048 ctx.idx = 0;
4049 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004050 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004051 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004052 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004053 }
4054 }
4055 msg->msg_state = HTTP_MSG_100_SENT;
4056 }
4057
4058 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004059 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004060 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004061 * is still null. We must save the body in msg->next because it
4062 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004063 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004064 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004065
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004066 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004067 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4068 else
4069 msg->msg_state = HTTP_MSG_DATA;
4070 }
4071
4072 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004073 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004074 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004075 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004076 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004077 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004078
Willy Tarreau115acb92009-12-26 13:56:06 +01004079 if (!ret)
4080 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004081 else if (ret < 0) {
4082 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004083 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004084 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004085 }
4086
Willy Tarreaud98cf932009-12-27 22:54:55 +01004087 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004088 * We have the first data byte is in msg->sov. We're waiting for at
4089 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004090 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004091
Willy Tarreau124d9912011-03-01 20:30:48 +01004092 if (msg->body_len < limit)
4093 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094
Willy Tarreau9b28e032012-10-12 23:49:43 +02004095 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004096 goto http_end;
4097
4098 missing_data:
4099 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004100 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004101 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004102 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004103 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004104
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004105 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004107 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004108
4109 if (!(s->flags & SN_ERR_MASK))
4110 s->flags |= SN_ERR_CLITO;
4111 if (!(s->flags & SN_FINST_MASK))
4112 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004113 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004114 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115
4116 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004117 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004118 /* Not enough data. We'll re-use the http-request
4119 * timeout here. Ideally, we should set the timeout
4120 * relative to the accept() date. We just set the
4121 * request timeout once at the beginning of the
4122 * request.
4123 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004124 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004125 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004126 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004127 return 0;
4128 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129
4130 http_end:
4131 /* The situation will not evolve, so let's give up on the analysis. */
4132 s->logs.tv_request = now; /* update the request timer to reflect full request */
4133 req->analysers &= ~an_bit;
4134 req->analyse_exp = TICK_ETERNITY;
4135 return 1;
4136
4137 return_bad_req: /* let's centralize all bad requests */
4138 txn->req.msg_state = HTTP_MSG_ERROR;
4139 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141
Willy Tarreau79ebac62010-06-07 13:47:49 +02004142 if (!(s->flags & SN_ERR_MASK))
4143 s->flags |= SN_ERR_PRXCOND;
4144 if (!(s->flags & SN_FINST_MASK))
4145 s->flags |= SN_FINST_R;
4146
Willy Tarreau522d6c02009-12-06 18:49:18 +01004147 return_err_msg:
4148 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004149 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004150 if (s->listener->counters)
4151 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004153}
4154
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004155/* send a server's name with an outgoing request over an established connection.
4156 * Note: this function is designed to be called once the request has been scheduled
4157 * for being forwarded. This is the reason why it rewinds the buffer before
4158 * proceeding.
4159 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004160int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004161
4162 struct hdr_ctx ctx;
4163
Mark Lamourinec2247f02012-01-04 13:02:01 -05004164 char *hdr_name = be->server_id_hdr_name;
4165 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004166 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004167 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004168 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004169
William Lallemandd9e90662012-01-30 17:27:17 +01004170 ctx.idx = 0;
4171
Willy Tarreau9b28e032012-10-12 23:49:43 +02004172 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004173 if (old_o) {
4174 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004175 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004176 }
4177
Willy Tarreau9b28e032012-10-12 23:49:43 +02004178 old_i = chn->buf->i;
4179 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 -05004180 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004181 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004182 }
4183
4184 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004185 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004186 memcpy(hdr_val, hdr_name, hdr_name_len);
4187 hdr_val += hdr_name_len;
4188 *hdr_val++ = ':';
4189 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004190 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4191 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004192
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004193 if (old_o) {
4194 /* If this was a forwarded request, we must readjust the amount of
4195 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004196 * variations. Note that if the request was already scheduled for
4197 * forwarding, it had its req->sol pointing to the body, which
4198 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004199 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004200 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004201 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004202 }
4203
Mark Lamourinec2247f02012-01-04 13:02:01 -05004204 return 0;
4205}
4206
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207/* Terminate current transaction and prepare a new one. This is very tricky
4208 * right now but it works.
4209 */
4210void http_end_txn_clean_session(struct session *s)
4211{
4212 /* FIXME: We need a more portable way of releasing a backend's and a
4213 * server's connections. We need a safer way to reinitialize buffer
4214 * flags. We also need a more accurate method for computing per-request
4215 * data.
4216 */
4217 http_silent_debug(__LINE__, s);
4218
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004219 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004220 si_shutr(s->req->cons);
4221 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222
4223 http_silent_debug(__LINE__, s);
4224
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004225 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004227 if (unlikely(s->srv_conn))
4228 sess_change_server(s, NULL);
4229 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230
4231 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4232 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004233 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234
4235 if (s->txn.status) {
4236 int n;
4237
4238 n = s->txn.status / 100;
4239 if (n < 1 || n > 5)
4240 n = 0;
4241
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004242 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004243 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004244 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004245 s->fe->fe_counters.p.http.comp_rsp++;
4246 }
Willy Tarreau24657792010-02-26 10:30:28 +01004247 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004248 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004249 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004250 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004251 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004252 s->be->be_counters.p.http.comp_rsp++;
4253 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254 }
4255
4256 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004257 s->logs.bytes_in -= s->req->buf->i;
4258 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259
4260 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004261 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 !(s->flags & SN_MONITOR) &&
4263 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4264 s->do_log(s);
4265 }
4266
4267 s->logs.accept_date = date; /* user-visible date for logging */
4268 s->logs.tv_accept = now; /* corrected date for internal use */
4269 tv_zero(&s->logs.tv_request);
4270 s->logs.t_queue = -1;
4271 s->logs.t_connect = -1;
4272 s->logs.t_data = -1;
4273 s->logs.t_close = 0;
4274 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4275 s->logs.srv_queue_size = 0; /* we will get this number soon */
4276
Willy Tarreau9b28e032012-10-12 23:49:43 +02004277 s->logs.bytes_in = s->req->total = s->req->buf->i;
4278 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279
4280 if (s->pend_pos)
4281 pendconn_free(s->pend_pos);
4282
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004283 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 if (s->flags & SN_CURR_SESS) {
4285 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004286 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004288 if (may_dequeue_tasks(objt_server(s->target), s->be))
4289 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290 }
4291
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004292 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004294 /* reinitialize the connection to the server */
Willy Tarreau4bd33a92013-10-14 23:46:46 +02004295 conn_init(s->req->cons->conn);
4296
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004298 s->req->cons->end = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004300 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 s->req->cons->exp = TICK_ETERNITY;
4302 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004303 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4304 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 +02004305 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 +01004306 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004307
4308 if (s->flags & SN_COMP_READY)
4309 s->comp_algo->end(&s->comp_ctx);
4310 s->comp_algo = NULL;
4311 s->flags &= ~SN_COMP_READY;
4312
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313 s->txn.meth = 0;
4314 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004315 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004316 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 s->req->cons->flags |= SI_FL_INDEP_STR;
4318
Willy Tarreau96e31212011-05-30 18:10:30 +02004319 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004320 s->req->flags |= CF_NEVER_WAIT;
4321 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004322 }
4323
Willy Tarreau610ecce2010-01-04 21:15:02 +01004324 /* if the request buffer is not empty, it means we're
4325 * about to process another request, so send pending
4326 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004327 * Just don't do this if the buffer is close to be full,
4328 * because the request will wait for it to flush a little
4329 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004331 if (s->req->buf->i) {
4332 if (s->rep->buf->o &&
4333 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4334 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004335 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004336 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004337
4338 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004339 channel_auto_read(s->req);
4340 channel_auto_close(s->req);
4341 channel_auto_read(s->rep);
4342 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343
Willy Tarreau342b11c2010-11-24 16:22:09 +01004344 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 s->rep->analysers = 0;
4346
4347 http_silent_debug(__LINE__, s);
4348}
4349
4350
4351/* This function updates the request state machine according to the response
4352 * state machine and buffer flags. It returns 1 if it changes anything (flag
4353 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4354 * it is only used to find when a request/response couple is complete. Both
4355 * this function and its equivalent should loop until both return zero. It
4356 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4357 */
4358int http_sync_req_state(struct session *s)
4359{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004360 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004362 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363 unsigned int old_state = txn->req.msg_state;
4364
4365 http_silent_debug(__LINE__, s);
4366 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4367 return 0;
4368
4369 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004370 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004371 * We can shut the read side unless we want to abort_on_close,
4372 * or we have a POST request. The issue with POST requests is
4373 * that some browsers still send a CRLF after the request, and
4374 * this CRLF must be read so that it does not remain in the kernel
4375 * buffers, otherwise a close could cause an RST on some systems
4376 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004377 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004378 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004379 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004380
Willy Tarreau40f151a2012-12-20 12:10:09 +01004381 /* if the server closes the connection, we want to immediately react
4382 * and close the socket to save packets and syscalls.
4383 */
4384 chn->cons->flags |= SI_FL_NOHALF;
4385
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4387 goto wait_other_side;
4388
4389 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4390 /* The server has not finished to respond, so we
4391 * don't want to move in order not to upset it.
4392 */
4393 goto wait_other_side;
4394 }
4395
4396 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4397 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004398 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004399 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004400 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 goto wait_other_side;
4402 }
4403
4404 /* When we get here, it means that both the request and the
4405 * response have finished receiving. Depending on the connection
4406 * mode, we'll have to wait for the last bytes to leave in either
4407 * direction, and sometimes for a close to be effective.
4408 */
4409
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004410 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4411 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004412 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4413 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004414 }
4415 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4416 /* Option forceclose is set, or either side wants to close,
4417 * let's enforce it now that we're not expecting any new
4418 * data to come. The caller knows the session is complete
4419 * once both states are CLOSED.
4420 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004421 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4422 channel_shutr_now(chn);
4423 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004424 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004425 }
4426 else {
4427 /* The last possible modes are keep-alive and tunnel. Since tunnel
4428 * mode does not set the body analyser, we can't reach this place
4429 * in tunnel mode, so we're left with keep-alive only.
4430 * This mode is currently not implemented, we switch to tunnel mode.
4431 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004432 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004433 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004434 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 }
4436
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004437 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004439 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004440
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442 txn->req.msg_state = HTTP_MSG_CLOSING;
4443 goto http_msg_closing;
4444 }
4445 else {
4446 txn->req.msg_state = HTTP_MSG_CLOSED;
4447 goto http_msg_closed;
4448 }
4449 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004450 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 }
4452
4453 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4454 http_msg_closing:
4455 /* nothing else to forward, just waiting for the output buffer
4456 * to be empty and for the shutw_now to take effect.
4457 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004458 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004459 txn->req.msg_state = HTTP_MSG_CLOSED;
4460 goto http_msg_closed;
4461 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004462 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 txn->req.msg_state = HTTP_MSG_ERROR;
4464 goto wait_other_side;
4465 }
4466 }
4467
4468 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4469 http_msg_closed:
4470 goto wait_other_side;
4471 }
4472
4473 wait_other_side:
4474 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004475 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004476}
4477
4478
4479/* This function updates the response state machine according to the request
4480 * state machine and buffer flags. It returns 1 if it changes anything (flag
4481 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4482 * it is only used to find when a request/response couple is complete. Both
4483 * this function and its equivalent should loop until both return zero. It
4484 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4485 */
4486int http_sync_res_state(struct session *s)
4487{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004488 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004490 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 unsigned int old_state = txn->rsp.msg_state;
4492
4493 http_silent_debug(__LINE__, s);
4494 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4495 return 0;
4496
4497 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4498 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004499 * still monitor the server connection for a possible close
4500 * while the request is being uploaded, so we don't disable
4501 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004503 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004504
4505 if (txn->req.msg_state == HTTP_MSG_ERROR)
4506 goto wait_other_side;
4507
4508 if (txn->req.msg_state < HTTP_MSG_DONE) {
4509 /* The client seems to still be sending data, probably
4510 * because we got an error response during an upload.
4511 * We have the choice of either breaking the connection
4512 * or letting it pass through. Let's do the later.
4513 */
4514 goto wait_other_side;
4515 }
4516
4517 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4518 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004519 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004521 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 goto wait_other_side;
4523 }
4524
4525 /* When we get here, it means that both the request and the
4526 * response have finished receiving. Depending on the connection
4527 * mode, we'll have to wait for the last bytes to leave in either
4528 * direction, and sometimes for a close to be effective.
4529 */
4530
4531 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4532 /* Server-close mode : shut read and wait for the request
4533 * side to close its output buffer. The caller will detect
4534 * when we're in DONE and the other is in CLOSED and will
4535 * catch that for the final cleanup.
4536 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004537 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4538 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004539 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004540 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4541 /* Option forceclose is set, or either side wants to close,
4542 * let's enforce it now that we're not expecting any new
4543 * data to come. The caller knows the session is complete
4544 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4547 channel_shutr_now(chn);
4548 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004549 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 }
4551 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004552 /* The last possible modes are keep-alive and tunnel. Since tunnel
4553 * mode does not set the body analyser, we can't reach this place
4554 * in tunnel mode, so we're left with keep-alive only.
4555 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004556 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004557 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004558 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004559 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560 }
4561
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004562 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004564 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4566 goto http_msg_closing;
4567 }
4568 else {
4569 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4570 goto http_msg_closed;
4571 }
4572 }
4573 goto wait_other_side;
4574 }
4575
4576 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4577 http_msg_closing:
4578 /* nothing else to forward, just waiting for the output buffer
4579 * to be empty and for the shutw_now to take effect.
4580 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004581 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4583 goto http_msg_closed;
4584 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004585 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004587 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004588 if (objt_server(s->target))
4589 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 goto wait_other_side;
4591 }
4592 }
4593
4594 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4595 http_msg_closed:
4596 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004597 bi_erase(chn);
4598 channel_auto_close(chn);
4599 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004600 goto wait_other_side;
4601 }
4602
4603 wait_other_side:
4604 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004605 /* We force the response to leave immediately if we're waiting for the
4606 * other side, since there is no pending shutdown to push it out.
4607 */
4608 if (!channel_is_empty(chn))
4609 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611}
4612
4613
4614/* Resync the request and response state machines. Return 1 if either state
4615 * changes.
4616 */
4617int http_resync_states(struct session *s)
4618{
4619 struct http_txn *txn = &s->txn;
4620 int old_req_state = txn->req.msg_state;
4621 int old_res_state = txn->rsp.msg_state;
4622
4623 http_silent_debug(__LINE__, s);
4624 http_sync_req_state(s);
4625 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004626 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 if (!http_sync_res_state(s))
4628 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004629 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 if (!http_sync_req_state(s))
4631 break;
4632 }
4633 http_silent_debug(__LINE__, s);
4634 /* OK, both state machines agree on a compatible state.
4635 * There are a few cases we're interested in :
4636 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4637 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4638 * directions, so let's simply disable both analysers.
4639 * - HTTP_MSG_CLOSED on the response only means we must abort the
4640 * request.
4641 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4642 * with server-close mode means we've completed one request and we
4643 * must re-initialize the server connection.
4644 */
4645
4646 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4647 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4648 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4649 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4650 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004651 channel_auto_close(s->req);
4652 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004654 channel_auto_close(s->rep);
4655 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004657 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4658 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004659 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004660 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004661 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004662 channel_auto_close(s->rep);
4663 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004664 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004665 channel_abort(s->req);
4666 channel_auto_close(s->req);
4667 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004668 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669 }
4670 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4671 txn->rsp.msg_state == HTTP_MSG_DONE &&
4672 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4673 /* server-close: terminate this server connection and
4674 * reinitialize a fresh-new transaction.
4675 */
4676 http_end_txn_clean_session(s);
4677 }
4678
4679 http_silent_debug(__LINE__, s);
4680 return txn->req.msg_state != old_req_state ||
4681 txn->rsp.msg_state != old_res_state;
4682}
4683
Willy Tarreaud98cf932009-12-27 22:54:55 +01004684/* This function is an analyser which forwards request body (including chunk
4685 * sizes if any). It is called as soon as we must forward, even if we forward
4686 * zero byte. The only situation where it must not be called is when we're in
4687 * tunnel mode and we want to forward till the close. It's used both to forward
4688 * remaining data and to resync after end of body. It expects the msg_state to
4689 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4690 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004691 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004692 * bytes of pending data + the headers if not already done (between sol and sov).
4693 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004694 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004695int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004696{
4697 struct http_txn *txn = &s->txn;
4698 struct http_msg *msg = &s->txn.req;
4699
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004700 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4701 return 0;
4702
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004703 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004704 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004705 /* Output closed while we were sending data. We must abort and
4706 * wake the other side up.
4707 */
4708 msg->msg_state = HTTP_MSG_ERROR;
4709 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004710 return 1;
4711 }
4712
Willy Tarreau4fe41902010-06-07 22:27:41 +02004713 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004714 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004715
4716 /* Note that we don't have to send 100-continue back because we don't
4717 * need the data to complete our job, and it's up to the server to
4718 * decide whether to return 100, 417 or anything else in return of
4719 * an "Expect: 100-continue" header.
4720 */
4721
4722 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004723 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004724 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004725 * is still null. We must save the body in msg->next because it
4726 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004727 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004728 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004729
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004730 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004731 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004732 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004733 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004734 }
4735
Willy Tarreaud98cf932009-12-27 22:54:55 +01004736 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004737 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004738
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004740 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004741 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004742 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004743 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004744 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004745 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004746 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004747 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004748
Willy Tarreaucaabe412010-01-03 23:08:28 +01004749 if (msg->msg_state == HTTP_MSG_DATA) {
4750 /* must still forward */
4751 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004752 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004753
4754 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004755 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004756 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004758 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004759 }
4760 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004761 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004762 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 * TRAILERS state.
4764 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004765 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766
Willy Tarreau54d23df2012-10-25 19:04:45 +02004767 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004768 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004769 else if (ret < 0) {
4770 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004771 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004772 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004773 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004774 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004776 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004777 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004778 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004779 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004780
4781 if (ret == 0)
4782 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004783 else if (ret < 0) {
4784 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004785 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004786 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004787 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004788 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004789 /* we're in MSG_CHUNK_SIZE now */
4790 }
4791 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004792 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793
4794 if (ret == 0)
4795 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004796 else if (ret < 0) {
4797 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004798 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004799 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004801 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802 /* we're in HTTP_MSG_DONE now */
4803 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004805 int old_state = msg->msg_state;
4806
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004808 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004809 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4810 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812 if (http_resync_states(s)) {
4813 /* some state changes occurred, maybe the analyser
4814 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004815 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004816 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004817 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004818 /* request errors are most likely due to
4819 * the server aborting the transfer.
4820 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004821 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004822 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004823 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004824 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004826 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004828 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004829
4830 /* If "option abortonclose" is set on the backend, we
4831 * want to monitor the client's connection and forward
4832 * any shutdown notification to the server, which will
4833 * decide whether to close or to go on processing the
4834 * request.
4835 */
4836 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004837 channel_auto_read(req);
4838 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004839 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004840 else if (s->txn.meth == HTTP_METH_POST) {
4841 /* POST requests may require to read extra CRLF
4842 * sent by broken browsers and which could cause
4843 * an RST to be sent upon close on some systems
4844 * (eg: Linux).
4845 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004846 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004847 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004848
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004850 }
4851 }
4852
Willy Tarreaud98cf932009-12-27 22:54:55 +01004853 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004854 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004855 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004856 if (!(s->flags & SN_ERR_MASK))
4857 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004858 if (!(s->flags & SN_FINST_MASK)) {
4859 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4860 s->flags |= SN_FINST_H;
4861 else
4862 s->flags |= SN_FINST_D;
4863 }
4864
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004865 s->fe->fe_counters.cli_aborts++;
4866 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004867 if (objt_server(s->target))
4868 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004869
4870 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004871 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004872
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004873 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004874 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004876
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004877 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004878 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004879 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004880 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004881 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004882
Willy Tarreau5c620922011-05-11 19:56:11 +02004883 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004884 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004885 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004886 * modes are already handled by the stream sock layer. We must not do
4887 * this in content-length mode because it could present the MSG_MORE
4888 * flag with the last block of forwarded data, which would cause an
4889 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004890 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004891 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004893
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004895 return 0;
4896
Willy Tarreaud98cf932009-12-27 22:54:55 +01004897 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004898 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004899 if (s->listener->counters)
4900 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004901 return_bad_req_stats_ok:
4902 txn->req.msg_state = HTTP_MSG_ERROR;
4903 if (txn->status) {
4904 /* Note: we don't send any error if some data were already sent */
4905 stream_int_retnclose(req->prod, NULL);
4906 } else {
4907 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004908 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004909 }
4910 req->analysers = 0;
4911 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912
4913 if (!(s->flags & SN_ERR_MASK))
4914 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004915 if (!(s->flags & SN_FINST_MASK)) {
4916 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4917 s->flags |= SN_FINST_H;
4918 else
4919 s->flags |= SN_FINST_D;
4920 }
4921 return 0;
4922
4923 aborted_xfer:
4924 txn->req.msg_state = HTTP_MSG_ERROR;
4925 if (txn->status) {
4926 /* Note: we don't send any error if some data were already sent */
4927 stream_int_retnclose(req->prod, NULL);
4928 } else {
4929 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004930 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004931 }
4932 req->analysers = 0;
4933 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4934
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004935 s->fe->fe_counters.srv_aborts++;
4936 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004937 if (objt_server(s->target))
4938 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004939
4940 if (!(s->flags & SN_ERR_MASK))
4941 s->flags |= SN_ERR_SRVCL;
4942 if (!(s->flags & SN_FINST_MASK)) {
4943 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4944 s->flags |= SN_FINST_H;
4945 else
4946 s->flags |= SN_FINST_D;
4947 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948 return 0;
4949}
4950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004951/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4952 * processing can continue on next analysers, or zero if it either needs more
4953 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4954 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4955 * when it has nothing left to do, and may remove any analyser when it wants to
4956 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004957 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004958int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004959{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004960 struct http_txn *txn = &s->txn;
4961 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004962 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004963 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004964 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004965 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004966
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004967 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 +02004968 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004969 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004970 rep,
4971 rep->rex, rep->wex,
4972 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004973 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004974 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004975
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004976 /*
4977 * Now parse the partial (or complete) lines.
4978 * We will check the response syntax, and also join multi-line
4979 * headers. An index of all the lines will be elaborated while
4980 * parsing.
4981 *
4982 * For the parsing, we use a 28 states FSM.
4983 *
4984 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004985 * rep->buf->p = beginning of response
4986 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4987 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004988 * msg->eol = end of current header or line (LF or CRLF)
4989 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004990 */
4991
Willy Tarreau83e3af02009-12-28 17:39:57 +01004992 /* There's a protected area at the end of the buffer for rewriting
4993 * purposes. We don't want to start to parse the request if the
4994 * protected area is affected, because we may have to move processed
4995 * data later, which is much more complicated.
4996 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004997 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004998 if (unlikely(!channel_reserved(rep))) {
4999 /* some data has still not left the buffer, wake us once that's done */
5000 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5001 goto abort_response;
5002 channel_dont_close(rep);
5003 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5004 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005005 }
5006
Willy Tarreau379357a2013-06-08 12:55:46 +02005007 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5008 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5009 buffer_slow_realign(rep->buf);
5010
Willy Tarreau9b28e032012-10-12 23:49:43 +02005011 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005012 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005013 }
5014
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 /* 1: we might have to print this header in debug mode */
5016 if (unlikely((global.mode & MODE_DEBUG) &&
5017 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005018 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005019 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005020
Willy Tarreau9b28e032012-10-12 23:49:43 +02005021 sol = rep->buf->p;
5022 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005024
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005025 sol += hdr_idx_first_pos(&txn->hdr_idx);
5026 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005027
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005028 while (cur_idx) {
5029 eol = sol + txn->hdr_idx.v[cur_idx].len;
5030 debug_hdr("srvhdr", s, sol, eol);
5031 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5032 cur_idx = txn->hdr_idx.v[cur_idx].next;
5033 }
5034 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005036 /*
5037 * Now we quickly check if we have found a full valid response.
5038 * If not so, we check the FD and buffer states before leaving.
5039 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005040 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 * responses are checked first.
5042 *
5043 * Depending on whether the client is still there or not, we
5044 * may send an error response back or not. Note that normally
5045 * we should only check for HTTP status there, and check I/O
5046 * errors somewhere else.
5047 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048
Willy Tarreau655dce92009-11-08 13:10:58 +01005049 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 /* Invalid response */
5051 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5052 /* we detected a parsing error. We want to archive this response
5053 * in the dedicated proxy area for later troubleshooting.
5054 */
5055 hdr_response_bad:
5056 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005057 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005059 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005060 if (objt_server(s->target)) {
5061 objt_server(s->target)->counters.failed_resp++;
5062 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005063 }
Willy Tarreau64648412010-03-05 10:41:54 +01005064 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005065 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005066 rep->analysers = 0;
5067 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005068 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005069 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005070 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005071
5072 if (!(s->flags & SN_ERR_MASK))
5073 s->flags |= SN_ERR_PRXCOND;
5074 if (!(s->flags & SN_FINST_MASK))
5075 s->flags |= SN_FINST_H;
5076
5077 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005081 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005082 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005083 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005084 goto hdr_response_bad;
5085 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005086
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005088 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005090 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005091
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005092 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005093 if (objt_server(s->target)) {
5094 objt_server(s->target)->counters.failed_resp++;
5095 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005096 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005097
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005098 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005099 rep->analysers = 0;
5100 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005101 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005102 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005103 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005104
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 if (!(s->flags & SN_ERR_MASK))
5106 s->flags |= SN_ERR_SRVCL;
5107 if (!(s->flags & SN_FINST_MASK))
5108 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005109 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005110 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005111
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005112 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005113 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005115 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005116
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005117 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005118 if (objt_server(s->target)) {
5119 objt_server(s->target)->counters.failed_resp++;
5120 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005121 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005122
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005123 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005124 rep->analysers = 0;
5125 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005126 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005127 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005128 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005129
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005130 if (!(s->flags & SN_ERR_MASK))
5131 s->flags |= SN_ERR_SRVTO;
5132 if (!(s->flags & SN_FINST_MASK))
5133 s->flags |= SN_FINST_H;
5134 return 0;
5135 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005136
Willy Tarreauf003d372012-11-26 13:35:37 +01005137 /* client abort with an abortonclose */
5138 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5139 s->fe->fe_counters.cli_aborts++;
5140 s->be->be_counters.cli_aborts++;
5141 if (objt_server(s->target))
5142 objt_server(s->target)->counters.cli_aborts++;
5143
5144 rep->analysers = 0;
5145 channel_auto_close(rep);
5146
5147 txn->status = 400;
5148 bi_erase(rep);
5149 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5150
5151 if (!(s->flags & SN_ERR_MASK))
5152 s->flags |= SN_ERR_CLICL;
5153 if (!(s->flags & SN_FINST_MASK))
5154 s->flags |= SN_FINST_H;
5155
5156 /* process_session() will take care of the error */
5157 return 0;
5158 }
5159
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005160 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005161 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005162 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005163 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005164
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005165 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005166 if (objt_server(s->target)) {
5167 objt_server(s->target)->counters.failed_resp++;
5168 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005169 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005170
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005171 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 rep->analysers = 0;
5173 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005174 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005175 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005176 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005177
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 if (!(s->flags & SN_ERR_MASK))
5179 s->flags |= SN_ERR_SRVCL;
5180 if (!(s->flags & SN_FINST_MASK))
5181 s->flags |= SN_FINST_H;
5182 return 0;
5183 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005184
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005186 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005187 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005188 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005189
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005190 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005192 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193
5194 if (!(s->flags & SN_ERR_MASK))
5195 s->flags |= SN_ERR_CLICL;
5196 if (!(s->flags & SN_FINST_MASK))
5197 s->flags |= SN_FINST_H;
5198
5199 /* process_session() will take care of the error */
5200 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005202
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005203 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204 return 0;
5205 }
5206
5207 /* More interesting part now : we know that we have a complete
5208 * response which at least looks like HTTP. We have an indicator
5209 * of each header's length, so we can parse them quickly.
5210 */
5211
5212 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005213 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005214
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 /*
5216 * 1: get the status code
5217 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005218 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 if (n < 1 || n > 5)
5220 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005221 /* when the client triggers a 4xx from the server, it's most often due
5222 * to a missing object or permission. These events should be tracked
5223 * because if they happen often, it may indicate a brute force or a
5224 * vulnerability scan.
5225 */
5226 if (n == 4)
5227 session_inc_http_err_ctr(s);
5228
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005229 if (objt_server(s->target))
5230 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231
Willy Tarreau5b154472009-12-21 20:11:07 +01005232 /* check if the response is HTTP/1.1 or above */
5233 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005234 ((rep->buf->p[5] > '1') ||
5235 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005236 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005237
5238 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005239 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 +01005240
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005241 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005242 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005243
Willy Tarreau9b28e032012-10-12 23:49:43 +02005244 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005245
Willy Tarreau39650402010-03-15 19:44:39 +01005246 /* Adjust server's health based on status code. Note: status codes 501
5247 * and 505 are triggered on demand by client request, so we must not
5248 * count them as server failures.
5249 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005250 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005251 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005252 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005253 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005254 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005255 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005256
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005257 /*
5258 * 2: check for cacheability.
5259 */
5260
5261 switch (txn->status) {
5262 case 200:
5263 case 203:
5264 case 206:
5265 case 300:
5266 case 301:
5267 case 410:
5268 /* RFC2616 @13.4:
5269 * "A response received with a status code of
5270 * 200, 203, 206, 300, 301 or 410 MAY be stored
5271 * by a cache (...) unless a cache-control
5272 * directive prohibits caching."
5273 *
5274 * RFC2616 @9.5: POST method :
5275 * "Responses to this method are not cacheable,
5276 * unless the response includes appropriate
5277 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005278 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005279 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005280 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5282 break;
5283 default:
5284 break;
5285 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005287 /*
5288 * 3: we may need to capture headers
5289 */
5290 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005291 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005292 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005293 txn->rsp.cap, s->fe->rsp_cap);
5294
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005295 /* 4: determine the transfer-length.
5296 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5297 * the presence of a message-body in a RESPONSE and its transfer length
5298 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005299 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005300 * All responses to the HEAD request method MUST NOT include a
5301 * message-body, even though the presence of entity-header fields
5302 * might lead one to believe they do. All 1xx (informational), 204
5303 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5304 * message-body. All other responses do include a message-body,
5305 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005306 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005307 * 1. Any response which "MUST NOT" include a message-body (such as the
5308 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5309 * always terminated by the first empty line after the header fields,
5310 * regardless of the entity-header fields present in the message.
5311 *
5312 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5313 * the "chunked" transfer-coding (Section 6.2) is used, the
5314 * transfer-length is defined by the use of this transfer-coding.
5315 * If a Transfer-Encoding header field is present and the "chunked"
5316 * transfer-coding is not present, the transfer-length is defined by
5317 * the sender closing the connection.
5318 *
5319 * 3. If a Content-Length header field is present, its decimal value in
5320 * OCTETs represents both the entity-length and the transfer-length.
5321 * If a message is received with both a Transfer-Encoding header
5322 * field and a Content-Length header field, the latter MUST be ignored.
5323 *
5324 * 4. If the message uses the media type "multipart/byteranges", and
5325 * the transfer-length is not otherwise specified, then this self-
5326 * delimiting media type defines the transfer-length. This media
5327 * type MUST NOT be used unless the sender knows that the recipient
5328 * can parse it; the presence in a request of a Range header with
5329 * multiple byte-range specifiers from a 1.1 client implies that the
5330 * client can parse multipart/byteranges responses.
5331 *
5332 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005333 */
5334
5335 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005336 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005337 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005338 * FIXME: should we parse anyway and return an error on chunked encoding ?
5339 */
5340 if (txn->meth == HTTP_METH_HEAD ||
5341 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005342 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005343 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005344 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005345 goto skip_content_length;
5346 }
5347
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005348 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005349 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005350 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005351 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005352 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005353 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5354 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005355 /* bad transfer-encoding (chunked followed by something else) */
5356 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005357 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005358 break;
5359 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005360 }
5361
5362 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5363 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005364 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005365 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005366 signed long long cl;
5367
Willy Tarreauad14f752011-09-02 20:33:27 +02005368 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005369 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005370 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005371 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005372
Willy Tarreauad14f752011-09-02 20:33:27 +02005373 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005374 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005375 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005376 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005377
Willy Tarreauad14f752011-09-02 20:33:27 +02005378 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005379 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005380 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005381 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005382
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005383 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005384 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005385 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005386 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005387
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005388 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005389 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005390 }
5391
William Lallemand82fe75c2012-10-23 10:25:10 +02005392 if (s->fe->comp || s->be->comp)
5393 select_compression_response_header(s, rep->buf);
5394
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005395 /* FIXME: we should also implement the multipart/byterange method.
5396 * For now on, we resort to close mode in this case (unknown length).
5397 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005398skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005399
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005400 /* end of job, return OK */
5401 rep->analysers &= ~an_bit;
5402 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005403 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 return 1;
5405}
5406
5407/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005408 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5409 * and updates t->rep->analysers. It might make sense to explode it into several
5410 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005411 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005412int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413{
5414 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415 struct http_msg *msg = &txn->rsp;
5416 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005417 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005418 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005420 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 +02005421 now_ms, __FUNCTION__,
5422 t,
5423 rep,
5424 rep->rex, rep->wex,
5425 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005426 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427 rep->analysers);
5428
Willy Tarreau655dce92009-11-08 13:10:58 +01005429 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005430 return 0;
5431
5432 rep->analysers &= ~an_bit;
5433 rep->analyse_exp = TICK_ETERNITY;
5434
Willy Tarreau5b154472009-12-21 20:11:07 +01005435 /* Now we have to check if we need to modify the Connection header.
5436 * This is more difficult on the response than it is on the request,
5437 * because we can have two different HTTP versions and we don't know
5438 * how the client will interprete a response. For instance, let's say
5439 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5440 * HTTP/1.1 response without any header. Maybe it will bound itself to
5441 * HTTP/1.0 because it only knows about it, and will consider the lack
5442 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5443 * the lack of header as a keep-alive. Thus we will use two flags
5444 * indicating how a request MAY be understood by the client. In case
5445 * of multiple possibilities, we'll fix the header to be explicit. If
5446 * ambiguous cases such as both close and keepalive are seen, then we
5447 * will fall back to explicit close. Note that we won't take risks with
5448 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005449 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005450 */
5451
Willy Tarreaudc008c52010-02-01 16:20:08 +01005452 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5453 txn->status == 101)) {
5454 /* Either we've established an explicit tunnel, or we're
5455 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005456 * to understand the next protocols. We have to switch to tunnel
5457 * mode, so that we transfer the request and responses then let
5458 * this protocol pass unmodified. When we later implement specific
5459 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005460 * header which contains information about that protocol for
5461 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005462 */
5463 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5464 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005465 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5466 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5467 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005468 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005469
Willy Tarreau60466522010-01-18 19:08:45 +01005470 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005471 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005472 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5473 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005474
Willy Tarreau60466522010-01-18 19:08:45 +01005475 /* now adjust header transformations depending on current state */
5476 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5477 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5478 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005479 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005480 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005481 }
Willy Tarreau60466522010-01-18 19:08:45 +01005482 else { /* SCL / KAL */
5483 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005484 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005485 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005486 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005487
Willy Tarreau60466522010-01-18 19:08:45 +01005488 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005489 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005490
Willy Tarreau60466522010-01-18 19:08:45 +01005491 /* Some keep-alive responses are converted to Server-close if
5492 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005493 */
Willy Tarreau60466522010-01-18 19:08:45 +01005494 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5495 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005496 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005497 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005498 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005499 }
5500
Willy Tarreau7959a552013-09-23 16:44:27 +02005501 /* we want to have the response time before we start processing it */
5502 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5503
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005504 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005505 /*
5506 * 3: we will have to evaluate the filters.
5507 * As opposed to version 1.2, now they will be evaluated in the
5508 * filters order and not in the header order. This means that
5509 * each filter has to be validated among all headers.
5510 *
5511 * Filters are tried with ->be first, then with ->fe if it is
5512 * different from ->be.
5513 */
5514
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005515 cur_proxy = t->be;
5516 while (1) {
5517 struct proxy *rule_set = cur_proxy;
5518
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005519 /* evaluate http-response rules */
5520 if (!http_res_last_rule)
5521 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5522
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005523 /* try headers filters */
5524 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005525 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005526 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005527 if (objt_server(t->target)) {
5528 objt_server(t->target)->counters.failed_resp++;
5529 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005530 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005531 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005532 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005533 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005534 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005535 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005536 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005537 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005538 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005539 if (!(t->flags & SN_ERR_MASK))
5540 t->flags |= SN_ERR_PRXCOND;
5541 if (!(t->flags & SN_FINST_MASK))
5542 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005543 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005544 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005545 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005546
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005547 /* has the response been denied ? */
5548 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005549 if (objt_server(t->target))
5550 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005551
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005552 t->be->be_counters.denied_resp++;
5553 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005554 if (t->listener->counters)
5555 t->listener->counters->denied_resp++;
5556
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005557 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005558 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005559
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005560 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005561 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005562 if (txn->status < 200)
5563 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005564 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005565 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005566 ret = acl_pass(ret);
5567 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5568 ret = !ret;
5569 if (!ret)
5570 continue;
5571 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005572 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005573 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005574 }
5575
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005576 /* check whether we're already working on the frontend */
5577 if (cur_proxy == t->fe)
5578 break;
5579 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005580 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005581
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005582 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005583 * We may be facing a 100-continue response, in which case this
5584 * is not the right response, and we're waiting for the next one.
5585 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005586 * next one.
5587 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005588 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005589 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005590 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005591 msg->msg_state = HTTP_MSG_RPBEFORE;
5592 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005593 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005594 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5595 return 1;
5596 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005597 else if (unlikely(txn->status < 200))
5598 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005599
5600 /* we don't have any 1xx status code now */
5601
5602 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005603 * 4: check for server cookie.
5604 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005605 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5606 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005607 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005608
Willy Tarreaubaaee002006-06-26 02:48:02 +02005609
Willy Tarreaua15645d2007-03-18 16:22:39 +01005610 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005611 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005612 */
Willy Tarreau67402132012-05-31 20:40:20 +02005613 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005614 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005615
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005616 /*
5617 * 6: add server cookie in the response if needed
5618 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005619 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005620 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005621 (!(t->flags & SN_DIRECT) ||
5622 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5623 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5624 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5625 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005626 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005627 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005628 /* the server is known, it's not the one the client requested, or the
5629 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005630 * insert a set-cookie here, except if we want to insert only on POST
5631 * requests and this one isn't. Note that servers which don't have cookies
5632 * (eg: some backup servers) will return a full cookie removal request.
5633 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005634 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005635 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005636 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5637 t->be->cookie_name);
5638 }
5639 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005640 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005641
5642 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5643 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005644 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5645 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5646 trash.len += 5;
5647
Willy Tarreauef4f3912010-10-07 21:00:29 +02005648 if (t->be->cookie_maxlife) {
5649 /* emit first_date, which is either the original one or
5650 * the current date.
5651 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005652 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005653 s30tob64(txn->cookie_first_date ?
5654 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005655 (date.tv_sec+3) >> 2, trash.str + trash.len);
5656 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005657 }
5658 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005659 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005662 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005663 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005664
Willy Tarreau4992dd22012-05-31 21:02:17 +02005665 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005666 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005667
5668 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005669 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005670
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005671 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005672 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005673
Willy Tarreauf1348312010-10-07 15:54:11 +02005674 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005675 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005676 /* the server did not change, only the date was updated */
5677 txn->flags |= TX_SCK_UPDATED;
5678 else
5679 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005680
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005681 /* Here, we will tell an eventual cache on the client side that we don't
5682 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5683 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5684 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5685 */
Willy Tarreau67402132012-05-31 20:40:20 +02005686 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005687
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005688 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5689
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005690 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005691 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005692 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005693 }
5694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005695
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005696 /*
5697 * 7: check if result will be cacheable with a cookie.
5698 * We'll block the response if security checks have caught
5699 * nasty things such as a cacheable cookie.
5700 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005701 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5702 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005703 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005704
5705 /* we're in presence of a cacheable response containing
5706 * a set-cookie header. We'll block it as requested by
5707 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005708 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005709 if (objt_server(t->target))
5710 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005711
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005712 t->be->be_counters.denied_resp++;
5713 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005714 if (t->listener->counters)
5715 t->listener->counters->denied_resp++;
5716
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005717 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005718 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005719 send_log(t->be, LOG_ALERT,
5720 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005721 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722 goto return_srv_prx_502;
5723 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005724
5725 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005726 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005727 * If an "Upgrade" token is found, the header is left untouched in order
5728 * not to have to deal with some client bugs : some of them fail an upgrade
5729 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005731 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5732 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5733 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005734 unsigned int want_flags = 0;
5735
5736 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5737 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5738 /* we want a keep-alive response here. Keep-alive header
5739 * required if either side is not 1.1.
5740 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005741 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005742 want_flags |= TX_CON_KAL_SET;
5743 }
5744 else {
5745 /* we want a close response here. Close header required if
5746 * the server is 1.1, regardless of the client.
5747 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005748 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005749 want_flags |= TX_CON_CLO_SET;
5750 }
5751
5752 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005753 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005754 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005755
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005756 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005757 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005758 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005759 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005760
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005761 /*************************************************************
5762 * OK, that's finished for the headers. We have done what we *
5763 * could. Let's switch to the DATA state. *
5764 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766 /* if the user wants to log as soon as possible, without counting
5767 * bytes from the server, then this is the right moment. We have
5768 * to temporarily assign bytes_out to log what we currently have.
5769 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005770 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005771 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5772 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005773 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005774 t->logs.bytes_out = 0;
5775 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005777 /* Note: we must not try to cheat by jumping directly to DATA,
5778 * otherwise we would not let the client side wake up.
5779 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005781 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005782 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005783 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005784}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785
Willy Tarreaud98cf932009-12-27 22:54:55 +01005786/* This function is an analyser which forwards response body (including chunk
5787 * sizes if any). It is called as soon as we must forward, even if we forward
5788 * zero byte. The only situation where it must not be called is when we're in
5789 * tunnel mode and we want to forward till the close. It's used both to forward
5790 * remaining data and to resync after end of body. It expects the msg_state to
5791 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5792 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005793 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005794 * bytes of pending data + the headers if not already done (between sol and sov).
5795 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005796 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005797int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005798{
5799 struct http_txn *txn = &s->txn;
5800 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005801 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005802 static struct buffer *tmpbuf = NULL;
5803 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005804 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005805 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005806
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005807 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5808 return 0;
5809
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005810 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005811 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005812 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005813 /* Output closed while we were sending data. We must abort and
5814 * wake the other side up.
5815 */
5816 msg->msg_state = HTTP_MSG_ERROR;
5817 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005818 return 1;
5819 }
5820
Willy Tarreau4fe41902010-06-07 22:27:41 +02005821 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005822 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005823
William Lallemand82fe75c2012-10-23 10:25:10 +02005824 /* this is the first time we need the compression buffer */
5825 if (s->comp_algo != NULL && tmpbuf == NULL) {
5826 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5827 goto aborted_xfer; /* no memory */
5828 }
5829
Willy Tarreaud98cf932009-12-27 22:54:55 +01005830 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005831 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005832 * rep->buf.p still points to the beginning of the message and msg->sol
5833 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005834 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005835 channel_forward(res, msg->sov);
5836 msg->next = 0;
5837 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005838
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005839 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005840 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005841 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005842 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005843 }
5844
William Lallemand82fe75c2012-10-23 10:25:10 +02005845 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005846 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005847 if (ret < 0)
5848 goto missing_data; /* not enough spaces in buffers */
5849 compressing = 1;
5850 }
5851
Willy Tarreaud98cf932009-12-27 22:54:55 +01005852 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005853 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005854 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005855 if (s->comp_algo == NULL) {
5856 bytes = msg->sov - msg->sol;
5857 if (msg->chunk_len || bytes) {
5858 msg->sol = msg->sov;
5859 msg->next -= bytes; /* will be forwarded */
5860 msg->chunk_len += bytes;
5861 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5862 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005863 }
5864
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005865 switch (msg->msg_state - HTTP_MSG_DATA) {
5866 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005867 if (compressing) {
5868 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5869 if (ret < 0)
5870 goto aborted_xfer;
5871 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005872
5873 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005874 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005875
5876 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005877 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005878 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005879 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005880 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005881 if (compressing && consumed_data) {
5882 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5883 compressing = 0;
5884 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005885 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005886 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005887 /* fall through for HTTP_MSG_CHUNK_CRLF */
5888
5889 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5890 /* we want the CRLF after the data */
5891
5892 ret = http_skip_chunk_crlf(msg);
5893 if (ret == 0)
5894 goto missing_data;
5895 else if (ret < 0) {
5896 if (msg->err_pos >= 0)
5897 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5898 goto return_bad_res;
5899 }
5900 /* skipping data in buffer for compression */
5901 if (compressing) {
5902 b_adv(res->buf, msg->next);
5903 msg->next = 0;
5904 msg->sov = 0;
5905 msg->sol = 0;
5906 }
5907 /* we're in MSG_CHUNK_SIZE now, fall through */
5908
5909 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005910 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005911 * set ->sov and ->next to point to the body and switch to DATA or
5912 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005913 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005914
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005915 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005916 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005917 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005918 else if (ret < 0) {
5919 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005920 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005921 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005922 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005923 if (compressing) {
5924 if (likely(msg->chunk_len > 0)) {
5925 /* skipping data if we are in compression mode */
5926 b_adv(res->buf, msg->next);
5927 msg->next = 0;
5928 msg->sov = 0;
5929 msg->sol = 0;
5930 } else {
5931 if (consumed_data) {
5932 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5933 compressing = 0;
5934 }
5935 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005936 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005937 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005938 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005939
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005940 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5941 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 if (ret == 0)
5943 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005944 else if (ret < 0) {
5945 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005946 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005947 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005948 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005949 if (s->comp_algo != NULL) {
5950 /* forwarding trailers */
5951 channel_forward(res, msg->next);
5952 msg->next = 0;
5953 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005954 /* we're in HTTP_MSG_DONE now, but we might still have
5955 * some data pending, so let's loop over once.
5956 */
5957 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005958
5959 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005960 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005961
5962 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005963 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005964 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5965 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005966 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005967 if (http_resync_states(s)) {
5968 http_silent_debug(__LINE__, s);
5969 /* some state changes occurred, maybe the analyser
5970 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005971 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005972 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005973 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005974 /* response errors are most likely due to
5975 * the client aborting the transfer.
5976 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005977 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005978 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005979 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005980 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005981 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005982 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005983 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005984 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005985 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005986 }
5987 }
5988
Willy Tarreaud98cf932009-12-27 22:54:55 +01005989 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005990 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005991 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5992 compressing = 0;
5993 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005994
5995 if (res->flags & CF_SHUTW)
5996 goto aborted_xfer;
5997
5998 /* stop waiting for data if the input is closed before the end. If the
5999 * client side was already closed, it means that the client has aborted,
6000 * so we don't want to count this as a server abort. Otherwise it's a
6001 * server abort.
6002 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006003 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006004 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6005 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006006 if (!(s->flags & SN_ERR_MASK))
6007 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006008 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006009 if (objt_server(s->target))
6010 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006011 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006012 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006013
Willy Tarreau40dba092010-03-04 18:14:51 +01006014 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006015 if (!s->req->analysers)
6016 goto return_bad_res;
6017
Willy Tarreauea953162012-05-18 23:41:28 +02006018 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006019 if (s->comp_algo == NULL) {
6020 bytes = msg->sov - msg->sol;
6021 if (msg->chunk_len || bytes) {
6022 msg->sol = msg->sov;
6023 msg->next -= bytes; /* will be forwarded */
6024 msg->chunk_len += bytes;
6025 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6026 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006027 }
6028
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006029 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006030 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006031 * Similarly, with keep-alive on the client side, we don't want to forward a
6032 * close.
6033 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006034 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006035 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6036 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006037 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006038
Willy Tarreau5c620922011-05-11 19:56:11 +02006039 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006040 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006041 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006042 * modes are already handled by the stream sock layer. We must not do
6043 * this in content-length mode because it could present the MSG_MORE
6044 * flag with the last block of forwarded data, which would cause an
6045 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006046 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006047 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006048 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006049
Willy Tarreaud98cf932009-12-27 22:54:55 +01006050 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006051 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006052 return 0;
6053
Willy Tarreau40dba092010-03-04 18:14:51 +01006054 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006055 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006056 if (objt_server(s->target))
6057 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006058
6059 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006060 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006061 /* don't send any error message as we're in the body */
6062 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006063 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006064 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006065 if (objt_server(s->target))
6066 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006067
6068 if (!(s->flags & SN_ERR_MASK))
6069 s->flags |= SN_ERR_PRXCOND;
6070 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006071 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006072 return 0;
6073
6074 aborted_xfer:
6075 txn->rsp.msg_state = HTTP_MSG_ERROR;
6076 /* don't send any error message as we're in the body */
6077 stream_int_retnclose(res->cons, NULL);
6078 res->analysers = 0;
6079 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6080
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006081 s->fe->fe_counters.cli_aborts++;
6082 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006083 if (objt_server(s->target))
6084 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006085
6086 if (!(s->flags & SN_ERR_MASK))
6087 s->flags |= SN_ERR_CLICL;
6088 if (!(s->flags & SN_FINST_MASK))
6089 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090 return 0;
6091}
6092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006093/* Iterate the same filter through all request headers.
6094 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006095 * Since it can manage the switch to another backend, it updates the per-proxy
6096 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006097 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006098int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006099{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006100 char term;
6101 char *cur_ptr, *cur_end, *cur_next;
6102 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006103 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006105 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 last_hdr = 0;
6108
Willy Tarreau9b28e032012-10-12 23:49:43 +02006109 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006110 old_idx = 0;
6111
6112 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006113 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006115 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006116 (exp->action == ACT_ALLOW ||
6117 exp->action == ACT_DENY ||
6118 exp->action == ACT_TARPIT))
6119 return 0;
6120
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006121 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122 if (!cur_idx)
6123 break;
6124
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006125 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126 cur_ptr = cur_next;
6127 cur_end = cur_ptr + cur_hdr->len;
6128 cur_next = cur_end + cur_hdr->cr + 1;
6129
6130 /* Now we have one header between cur_ptr and cur_end,
6131 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132 */
6133
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006134 /* The annoying part is that pattern matching needs
6135 * that we modify the contents to null-terminate all
6136 * strings before testing them.
6137 */
6138
6139 term = *cur_end;
6140 *cur_end = '\0';
6141
6142 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6143 switch (exp->action) {
6144 case ACT_SETBE:
6145 /* It is not possible to jump a second time.
6146 * FIXME: should we return an HTTP/500 here so that
6147 * the admin knows there's a problem ?
6148 */
6149 if (t->be != t->fe)
6150 break;
6151
6152 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006153 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006154 last_hdr = 1;
6155 break;
6156
6157 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006158 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159 last_hdr = 1;
6160 break;
6161
6162 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006163 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006164 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006165 break;
6166
6167 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006168 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006169 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006170 break;
6171
6172 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006173 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6174 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175 /* FIXME: if the user adds a newline in the replacement, the
6176 * index will not be recalculated for now, and the new line
6177 * will not be counted as a new header.
6178 */
6179
6180 cur_end += delta;
6181 cur_next += delta;
6182 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006183 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006184 break;
6185
6186 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006187 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006188 cur_next += delta;
6189
Willy Tarreaufa355d42009-11-29 18:12:29 +01006190 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006191 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6192 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006193 cur_hdr->len = 0;
6194 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006195 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196 break;
6197
6198 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006200 if (cur_end)
6201 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006202
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006203 /* keep the link from this header to next one in case of later
6204 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006205 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006206 old_idx = cur_idx;
6207 }
6208 return 0;
6209}
6210
6211
6212/* Apply the filter to the request line.
6213 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6214 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006215 * Since it can manage the switch to another backend, it updates the per-proxy
6216 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006218int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006219{
6220 char term;
6221 char *cur_ptr, *cur_end;
6222 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006223 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006224 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225
Willy Tarreau3d300592007-03-18 18:34:41 +01006226 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006227 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006228 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006229 (exp->action == ACT_ALLOW ||
6230 exp->action == ACT_DENY ||
6231 exp->action == ACT_TARPIT))
6232 return 0;
6233 else if (exp->action == ACT_REMOVE)
6234 return 0;
6235
6236 done = 0;
6237
Willy Tarreau9b28e032012-10-12 23:49:43 +02006238 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006239 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006240
6241 /* Now we have the request line between cur_ptr and cur_end */
6242
6243 /* The annoying part is that pattern matching needs
6244 * that we modify the contents to null-terminate all
6245 * strings before testing them.
6246 */
6247
6248 term = *cur_end;
6249 *cur_end = '\0';
6250
6251 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6252 switch (exp->action) {
6253 case ACT_SETBE:
6254 /* It is not possible to jump a second time.
6255 * FIXME: should we return an HTTP/500 here so that
6256 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 if (t->be != t->fe)
6259 break;
6260
6261 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006262 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006263 done = 1;
6264 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006266 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006267 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006268 done = 1;
6269 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006271 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006272 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006273 done = 1;
6274 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006277 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006278 done = 1;
6279 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006281 case ACT_REPLACE:
6282 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006283 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6284 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285 /* FIXME: if the user adds a newline in the replacement, the
6286 * index will not be recalculated for now, and the new line
6287 * will not be counted as a new header.
6288 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006289
Willy Tarreaufa355d42009-11-29 18:12:29 +01006290 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006291 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006292 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006293 HTTP_MSG_RQMETH,
6294 cur_ptr, cur_end + 1,
6295 NULL, NULL);
6296 if (unlikely(!cur_end))
6297 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006299 /* we have a full request and we know that we have either a CR
6300 * or an LF at <ptr>.
6301 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006302 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6303 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304 /* there is no point trying this regex on headers */
6305 return 1;
6306 }
6307 }
6308 *cur_end = term; /* restore the string terminator */
6309 return done;
6310}
Willy Tarreau97de6242006-12-27 17:18:38 +01006311
Willy Tarreau58f10d72006-12-04 02:26:12 +01006312
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006314/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006315 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006316 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006317 * unparsable request. Since it can manage the switch to another backend, it
6318 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006319 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006320int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006322 struct http_txn *txn = &s->txn;
6323 struct hdr_exp *exp;
6324
6325 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006328 /*
6329 * The interleaving of transformations and verdicts
6330 * makes it difficult to decide to continue or stop
6331 * the evaluation.
6332 */
6333
Willy Tarreau6c123b12010-01-28 20:22:06 +01006334 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6335 break;
6336
Willy Tarreau3d300592007-03-18 18:34:41 +01006337 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006338 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006339 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006340 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006341
6342 /* if this filter had a condition, evaluate it now and skip to
6343 * next filter if the condition does not match.
6344 */
6345 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006346 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006347 ret = acl_pass(ret);
6348 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6349 ret = !ret;
6350
6351 if (!ret)
6352 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 }
6354
6355 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006356 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 if (unlikely(ret < 0))
6358 return -1;
6359
6360 if (likely(ret == 0)) {
6361 /* The filter did not match the request, it can be
6362 * iterated through all headers.
6363 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006364 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006368}
6369
6370
Willy Tarreaua15645d2007-03-18 16:22:39 +01006371
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006373 * Try to retrieve the server associated to the appsession.
6374 * If the server is found, it's assigned to the session.
6375 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006376void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006377 struct http_txn *txn = &t->txn;
6378 appsess *asession = NULL;
6379 char *sessid_temp = NULL;
6380
Cyril Bontéb21570a2009-11-29 20:04:48 +01006381 if (len > t->be->appsession_len) {
6382 len = t->be->appsession_len;
6383 }
6384
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006385 if (t->be->options2 & PR_O2_AS_REQL) {
6386 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006387 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006388 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006389 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006390 }
6391
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006392 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006393 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6394 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6395 return;
6396 }
6397
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006398 memcpy(txn->sessid, buf, len);
6399 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006400 }
6401
6402 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6403 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6404 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6405 return;
6406 }
6407
Cyril Bontéb21570a2009-11-29 20:04:48 +01006408 memcpy(sessid_temp, buf, len);
6409 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006410
6411 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6412 /* free previously allocated memory */
6413 pool_free2(apools.sessid, sessid_temp);
6414
6415 if (asession != NULL) {
6416 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6417 if (!(t->be->options2 & PR_O2_AS_REQL))
6418 asession->request_count++;
6419
6420 if (asession->serverid != NULL) {
6421 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006422
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006423 while (srv) {
6424 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006425 if ((srv->state & SRV_RUNNING) ||
6426 (t->be->options & PR_O_PERSIST) ||
6427 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006428 /* we found the server and it's usable */
6429 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006430 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006431 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006432 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006433
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006434 break;
6435 } else {
6436 txn->flags &= ~TX_CK_MASK;
6437 txn->flags |= TX_CK_DOWN;
6438 }
6439 }
6440 srv = srv->next;
6441 }
6442 }
6443 }
6444}
6445
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446/* Find the end of a cookie value contained between <s> and <e>. It works the
6447 * same way as with headers above except that the semi-colon also ends a token.
6448 * See RFC2965 for more information. Note that it requires a valid header to
6449 * return a valid result.
6450 */
6451char *find_cookie_value_end(char *s, const char *e)
6452{
6453 int quoted, qdpair;
6454
6455 quoted = qdpair = 0;
6456 for (; s < e; s++) {
6457 if (qdpair) qdpair = 0;
6458 else if (quoted) {
6459 if (*s == '\\') qdpair = 1;
6460 else if (*s == '"') quoted = 0;
6461 }
6462 else if (*s == '"') quoted = 1;
6463 else if (*s == ',' || *s == ';') return s;
6464 }
6465 return s;
6466}
6467
6468/* Delete a value in a header between delimiters <from> and <next> in buffer
6469 * <buf>. The number of characters displaced is returned, and the pointer to
6470 * the first delimiter is updated if required. The function tries as much as
6471 * possible to respect the following principles :
6472 * - replace <from> delimiter by the <next> one unless <from> points to a
6473 * colon, in which case <next> is simply removed
6474 * - set exactly one space character after the new first delimiter, unless
6475 * there are not enough characters in the block being moved to do so.
6476 * - remove unneeded spaces before the previous delimiter and after the new
6477 * one.
6478 *
6479 * It is the caller's responsibility to ensure that :
6480 * - <from> points to a valid delimiter or the colon ;
6481 * - <next> points to a valid delimiter or the final CR/LF ;
6482 * - there are non-space chars before <from> ;
6483 * - there is a CR/LF at or after <next>.
6484 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006485int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006486{
6487 char *prev = *from;
6488
6489 if (*prev == ':') {
6490 /* We're removing the first value, preserve the colon and add a
6491 * space if possible.
6492 */
6493 if (!http_is_crlf[(unsigned char)*next])
6494 next++;
6495 prev++;
6496 if (prev < next)
6497 *prev++ = ' ';
6498
6499 while (http_is_spht[(unsigned char)*next])
6500 next++;
6501 } else {
6502 /* Remove useless spaces before the old delimiter. */
6503 while (http_is_spht[(unsigned char)*(prev-1)])
6504 prev--;
6505 *from = prev;
6506
6507 /* copy the delimiter and if possible a space if we're
6508 * not at the end of the line.
6509 */
6510 if (!http_is_crlf[(unsigned char)*next]) {
6511 *prev++ = *next++;
6512 if (prev + 1 < next)
6513 *prev++ = ' ';
6514 while (http_is_spht[(unsigned char)*next])
6515 next++;
6516 }
6517 }
6518 return buffer_replace2(buf, prev, next, NULL, 0);
6519}
6520
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006521/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006522 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006523 * desirable to call it only when needed. This code is quite complex because
6524 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6525 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006527void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006529 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006532 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6533 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006536 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006537 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006539 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006541 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006543 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 hdr_beg = hdr_next;
6545 hdr_end = hdr_beg + cur_hdr->len;
6546 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 /* We have one full header between hdr_beg and hdr_end, and the
6549 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550 * "Cookie:" headers.
6551 */
6552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006554 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555 old_idx = cur_idx;
6556 continue;
6557 }
6558
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 del_from = NULL; /* nothing to be deleted */
6560 preserve_hdr = 0; /* assume we may kill the whole header */
6561
Willy Tarreau58f10d72006-12-04 02:26:12 +01006562 /* Now look for cookies. Conforming to RFC2109, we have to support
6563 * attributes whose name begin with a '$', and associate them with
6564 * the right cookie, if we want to delete this cookie.
6565 * So there are 3 cases for each cookie read :
6566 * 1) it's a special attribute, beginning with a '$' : ignore it.
6567 * 2) it's a server id cookie that we *MAY* want to delete : save
6568 * some pointers on it (last semi-colon, beginning of cookie...)
6569 * 3) it's an application cookie : we *MAY* have to delete a previous
6570 * "special" cookie.
6571 * At the end of loop, if a "special" cookie remains, we may have to
6572 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 * *MUST* delete it.
6574 *
6575 * Note: RFC2965 is unclear about the processing of spaces around
6576 * the equal sign in the ATTR=VALUE form. A careful inspection of
6577 * the RFC explicitly allows spaces before it, and not within the
6578 * tokens (attrs or values). An inspection of RFC2109 allows that
6579 * too but section 10.1.3 lets one think that spaces may be allowed
6580 * after the equal sign too, resulting in some (rare) buggy
6581 * implementations trying to do that. So let's do what servers do.
6582 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6583 * allowed quoted strings in values, with any possible character
6584 * after a backslash, including control chars and delimitors, which
6585 * causes parsing to become ambiguous. Browsers also allow spaces
6586 * within values even without quotes.
6587 *
6588 * We have to keep multiple pointers in order to support cookie
6589 * removal at the beginning, middle or end of header without
6590 * corrupting the header. All of these headers are valid :
6591 *
6592 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6593 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6594 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6595 * | | | | | | | | |
6596 * | | | | | | | | hdr_end <--+
6597 * | | | | | | | +--> next
6598 * | | | | | | +----> val_end
6599 * | | | | | +-----------> val_beg
6600 * | | | | +--------------> equal
6601 * | | | +----------------> att_end
6602 * | | +---------------------> att_beg
6603 * | +--------------------------> prev
6604 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605 */
6606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6608 /* Iterate through all cookies on this line */
6609
6610 /* find att_beg */
6611 att_beg = prev + 1;
6612 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6613 att_beg++;
6614
6615 /* find att_end : this is the first character after the last non
6616 * space before the equal. It may be equal to hdr_end.
6617 */
6618 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620 while (equal < hdr_end) {
6621 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006622 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 if (http_is_spht[(unsigned char)*equal++])
6624 continue;
6625 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626 }
6627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6629 * is between <att_beg> and <equal>, both may be identical.
6630 */
6631
6632 /* look for end of cookie if there is an equal sign */
6633 if (equal < hdr_end && *equal == '=') {
6634 /* look for the beginning of the value */
6635 val_beg = equal + 1;
6636 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6637 val_beg++;
6638
6639 /* find the end of the value, respecting quotes */
6640 next = find_cookie_value_end(val_beg, hdr_end);
6641
6642 /* make val_end point to the first white space or delimitor after the value */
6643 val_end = next;
6644 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6645 val_end--;
6646 } else {
6647 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006648 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006650 /* We have nothing to do with attributes beginning with '$'. However,
6651 * they will automatically be removed if a header before them is removed,
6652 * since they're supposed to be linked together.
6653 */
6654 if (*att_beg == '$')
6655 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006657 /* Ignore cookies with no equal sign */
6658 if (equal == next) {
6659 /* This is not our cookie, so we must preserve it. But if we already
6660 * scheduled another cookie for removal, we cannot remove the
6661 * complete header, but we can remove the previous block itself.
6662 */
6663 preserve_hdr = 1;
6664 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006665 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 val_end += delta;
6667 next += delta;
6668 hdr_end += delta;
6669 hdr_next += delta;
6670 cur_hdr->len += delta;
6671 http_msg_move_end(&txn->req, delta);
6672 prev = del_from;
6673 del_from = NULL;
6674 }
6675 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006677
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 /* if there are spaces around the equal sign, we need to
6679 * strip them otherwise we'll get trouble for cookie captures,
6680 * or even for rewrites. Since this happens extremely rarely,
6681 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006682 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6684 int stripped_before = 0;
6685 int stripped_after = 0;
6686
6687 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006688 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 equal += stripped_before;
6690 val_beg += stripped_before;
6691 }
6692
6693 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006694 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 val_beg += stripped_after;
6696 stripped_before += stripped_after;
6697 }
6698
6699 val_end += stripped_before;
6700 next += stripped_before;
6701 hdr_end += stripped_before;
6702 hdr_next += stripped_before;
6703 cur_hdr->len += stripped_before;
6704 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006705 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006707
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006708 /* First, let's see if we want to capture this cookie. We check
6709 * that we don't already have a client side cookie, because we
6710 * can only capture one. Also as an optimisation, we ignore
6711 * cookies shorter than the declared name.
6712 */
6713 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6714 (val_end - att_beg >= t->fe->capture_namelen) &&
6715 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6716 int log_len = val_end - att_beg;
6717
6718 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6719 Alert("HTTP logging : out of memory.\n");
6720 } else {
6721 if (log_len > t->fe->capture_len)
6722 log_len = t->fe->capture_len;
6723 memcpy(txn->cli_cookie, att_beg, log_len);
6724 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006725 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006727
Willy Tarreaubca99692010-10-06 19:25:55 +02006728 /* Persistence cookies in passive, rewrite or insert mode have the
6729 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006731 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006733 * For cookies in prefix mode, the form is :
6734 *
6735 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006736 */
6737 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6738 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6739 struct server *srv = t->be->srv;
6740 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006741
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6743 * have the server ID between val_beg and delim, and the original cookie between
6744 * delim+1 and val_end. Otherwise, delim==val_end :
6745 *
6746 * Cookie: NAME=SRV; # in all but prefix modes
6747 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6748 * | || || | |+-> next
6749 * | || || | +--> val_end
6750 * | || || +---------> delim
6751 * | || |+------------> val_beg
6752 * | || +-------------> att_end = equal
6753 * | |+-----------------> att_beg
6754 * | +------------------> prev
6755 * +-------------------------> hdr_beg
6756 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006757
Willy Tarreau67402132012-05-31 20:40:20 +02006758 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 for (delim = val_beg; delim < val_end; delim++)
6760 if (*delim == COOKIE_DELIM)
6761 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006762 } else {
6763 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006765 /* Now check if the cookie contains a date field, which would
6766 * appear after a vertical bar ('|') just after the server name
6767 * and before the delimiter.
6768 */
6769 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6770 if (vbar1) {
6771 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006772 * right is the last seen date. It is a base64 encoded
6773 * 30-bit value representing the UNIX date since the
6774 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006775 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006776 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006777 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006778 if (val_end - vbar1 >= 5) {
6779 val = b64tos30(vbar1);
6780 if (val > 0)
6781 txn->cookie_last_date = val << 2;
6782 }
6783 /* look for a second vertical bar */
6784 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6785 if (vbar1 && (val_end - vbar1 > 5)) {
6786 val = b64tos30(vbar1 + 1);
6787 if (val > 0)
6788 txn->cookie_first_date = val << 2;
6789 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006790 }
6791 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006792
Willy Tarreauf64d1412010-10-07 20:06:11 +02006793 /* if the cookie has an expiration date and the proxy wants to check
6794 * it, then we do that now. We first check if the cookie is too old,
6795 * then only if it has expired. We detect strict overflow because the
6796 * time resolution here is not great (4 seconds). Cookies with dates
6797 * in the future are ignored if their offset is beyond one day. This
6798 * allows an admin to fix timezone issues without expiring everyone
6799 * and at the same time avoids keeping unwanted side effects for too
6800 * long.
6801 */
6802 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006803 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6804 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006805 txn->flags &= ~TX_CK_MASK;
6806 txn->flags |= TX_CK_OLD;
6807 delim = val_beg; // let's pretend we have not found the cookie
6808 txn->cookie_first_date = 0;
6809 txn->cookie_last_date = 0;
6810 }
6811 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006812 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6813 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006814 txn->flags &= ~TX_CK_MASK;
6815 txn->flags |= TX_CK_EXPIRED;
6816 delim = val_beg; // let's pretend we have not found the cookie
6817 txn->cookie_first_date = 0;
6818 txn->cookie_last_date = 0;
6819 }
6820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 /* Here, we'll look for the first running server which supports the cookie.
6822 * This allows to share a same cookie between several servers, for example
6823 * to dedicate backup servers to specific servers only.
6824 * However, to prevent clients from sticking to cookie-less backup server
6825 * when they have incidentely learned an empty cookie, we simply ignore
6826 * empty cookies and mark them as invalid.
6827 * The same behaviour is applied when persistence must be ignored.
6828 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006829 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 while (srv) {
6833 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6834 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6835 if ((srv->state & SRV_RUNNING) ||
6836 (t->be->options & PR_O_PERSIST) ||
6837 (t->flags & SN_FORCE_PRST)) {
6838 /* we found the server and we can use it */
6839 txn->flags &= ~TX_CK_MASK;
6840 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6841 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006842 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006843 break;
6844 } else {
6845 /* we found a server, but it's down,
6846 * mark it as such and go on in case
6847 * another one is available.
6848 */
6849 txn->flags &= ~TX_CK_MASK;
6850 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006851 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006852 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 srv = srv->next;
6854 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855
Willy Tarreauf64d1412010-10-07 20:06:11 +02006856 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006857 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006858 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006859 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6860 txn->flags |= TX_CK_UNUSED;
6861 else
6862 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006863 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 /* depending on the cookie mode, we may have to either :
6866 * - delete the complete cookie if we're in insert+indirect mode, so that
6867 * the server never sees it ;
6868 * - remove the server id from the cookie value, and tag the cookie as an
6869 * application cookie so that it does not get accidentely removed later,
6870 * if we're in cookie prefix mode
6871 */
Willy Tarreau67402132012-05-31 20:40:20 +02006872 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006874
Willy Tarreau9b28e032012-10-12 23:49:43 +02006875 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 val_end += delta;
6877 next += delta;
6878 hdr_end += delta;
6879 hdr_next += delta;
6880 cur_hdr->len += delta;
6881 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 del_from = NULL;
6884 preserve_hdr = 1; /* we want to keep this cookie */
6885 }
6886 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006887 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006888 del_from = prev;
6889 }
6890 } else {
6891 /* This is not our cookie, so we must preserve it. But if we already
6892 * scheduled another cookie for removal, we cannot remove the
6893 * complete header, but we can remove the previous block itself.
6894 */
6895 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006897 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006898 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006899 if (att_beg >= del_from)
6900 att_beg += delta;
6901 if (att_end >= del_from)
6902 att_end += delta;
6903 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 val_end += delta;
6905 next += delta;
6906 hdr_end += delta;
6907 hdr_next += delta;
6908 cur_hdr->len += delta;
6909 http_msg_move_end(&txn->req, delta);
6910 prev = del_from;
6911 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006912 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 /* Look for the appsession cookie unless persistence must be ignored */
6916 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6917 int cmp_len, value_len;
6918 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 if (t->be->options2 & PR_O2_AS_PFX) {
6921 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6922 value_begin = att_beg + t->be->appsession_name_len;
6923 value_len = val_end - att_beg - t->be->appsession_name_len;
6924 } else {
6925 cmp_len = att_end - att_beg;
6926 value_begin = val_beg;
6927 value_len = val_end - val_beg;
6928 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 /* let's see if the cookie is our appcookie */
6931 if (cmp_len == t->be->appsession_name_len &&
6932 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6933 manage_client_side_appsession(t, value_begin, value_len);
6934 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006935 }
6936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 /* continue with next cookie on this header line */
6938 att_beg = next;
6939 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 /* There are no more cookies on this line.
6942 * We may still have one (or several) marked for deletion at the
6943 * end of the line. We must do this now in two ways :
6944 * - if some cookies must be preserved, we only delete from the
6945 * mark to the end of line ;
6946 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006949 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006951 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953 cur_hdr->len += delta;
6954 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006955 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006956
6957 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006958 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6959 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006960 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006961 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006962 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006963 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006964 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965 }
6966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006970}
6971
6972
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973/* Iterate the same filter through all response headers contained in <rtr>.
6974 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6975 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006976int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977{
6978 char term;
6979 char *cur_ptr, *cur_end, *cur_next;
6980 int cur_idx, old_idx, last_hdr;
6981 struct http_txn *txn = &t->txn;
6982 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006983 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984
6985 last_hdr = 0;
6986
Willy Tarreau9b28e032012-10-12 23:49:43 +02006987 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 old_idx = 0;
6989
6990 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006991 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006993 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 (exp->action == ACT_ALLOW ||
6995 exp->action == ACT_DENY))
6996 return 0;
6997
6998 cur_idx = txn->hdr_idx.v[old_idx].next;
6999 if (!cur_idx)
7000 break;
7001
7002 cur_hdr = &txn->hdr_idx.v[cur_idx];
7003 cur_ptr = cur_next;
7004 cur_end = cur_ptr + cur_hdr->len;
7005 cur_next = cur_end + cur_hdr->cr + 1;
7006
7007 /* Now we have one header between cur_ptr and cur_end,
7008 * and the next header starts at cur_next.
7009 */
7010
7011 /* The annoying part is that pattern matching needs
7012 * that we modify the contents to null-terminate all
7013 * strings before testing them.
7014 */
7015
7016 term = *cur_end;
7017 *cur_end = '\0';
7018
7019 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7020 switch (exp->action) {
7021 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 last_hdr = 1;
7024 break;
7025
7026 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007027 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007028 last_hdr = 1;
7029 break;
7030
7031 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007032 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7033 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 /* FIXME: if the user adds a newline in the replacement, the
7035 * index will not be recalculated for now, and the new line
7036 * will not be counted as a new header.
7037 */
7038
7039 cur_end += delta;
7040 cur_next += delta;
7041 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007042 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 break;
7044
7045 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007046 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 cur_next += delta;
7048
Willy Tarreaufa355d42009-11-29 18:12:29 +01007049 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7051 txn->hdr_idx.used--;
7052 cur_hdr->len = 0;
7053 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007054 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 break;
7056
7057 }
7058 }
7059 if (cur_end)
7060 *cur_end = term; /* restore the string terminator */
7061
7062 /* keep the link from this header to next one in case of later
7063 * removal of next header.
7064 */
7065 old_idx = cur_idx;
7066 }
7067 return 0;
7068}
7069
7070
7071/* Apply the filter to the status line in the response buffer <rtr>.
7072 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7073 * or -1 if a replacement resulted in an invalid status line.
7074 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007075int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076{
7077 char term;
7078 char *cur_ptr, *cur_end;
7079 int done;
7080 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007081 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082
7083
Willy Tarreau3d300592007-03-18 18:34:41 +01007084 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007086 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007087 (exp->action == ACT_ALLOW ||
7088 exp->action == ACT_DENY))
7089 return 0;
7090 else if (exp->action == ACT_REMOVE)
7091 return 0;
7092
7093 done = 0;
7094
Willy Tarreau9b28e032012-10-12 23:49:43 +02007095 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007096 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007097
7098 /* Now we have the status line between cur_ptr and cur_end */
7099
7100 /* The annoying part is that pattern matching needs
7101 * that we modify the contents to null-terminate all
7102 * strings before testing them.
7103 */
7104
7105 term = *cur_end;
7106 *cur_end = '\0';
7107
7108 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7109 switch (exp->action) {
7110 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007111 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112 done = 1;
7113 break;
7114
7115 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007116 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 done = 1;
7118 break;
7119
7120 case ACT_REPLACE:
7121 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007122 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7123 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124 /* FIXME: if the user adds a newline in the replacement, the
7125 * index will not be recalculated for now, and the new line
7126 * will not be counted as a new header.
7127 */
7128
Willy Tarreaufa355d42009-11-29 18:12:29 +01007129 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007131 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007132 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007133 cur_ptr, cur_end + 1,
7134 NULL, NULL);
7135 if (unlikely(!cur_end))
7136 return -1;
7137
7138 /* we have a full respnse and we know that we have either a CR
7139 * or an LF at <ptr>.
7140 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007141 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007142 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 /* there is no point trying this regex on headers */
7144 return 1;
7145 }
7146 }
7147 *cur_end = term; /* restore the string terminator */
7148 return done;
7149}
7150
7151
7152
7153/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007154 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7156 * unparsable response.
7157 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007158int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007160 struct http_txn *txn = &s->txn;
7161 struct hdr_exp *exp;
7162
7163 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 int ret;
7165
7166 /*
7167 * The interleaving of transformations and verdicts
7168 * makes it difficult to decide to continue or stop
7169 * the evaluation.
7170 */
7171
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007172 if (txn->flags & TX_SVDENY)
7173 break;
7174
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7177 exp->action == ACT_PASS)) {
7178 exp = exp->next;
7179 continue;
7180 }
7181
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007182 /* if this filter had a condition, evaluate it now and skip to
7183 * next filter if the condition does not match.
7184 */
7185 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007186 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007187 ret = acl_pass(ret);
7188 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7189 ret = !ret;
7190 if (!ret)
7191 continue;
7192 }
7193
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007195 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 if (unlikely(ret < 0))
7197 return -1;
7198
7199 if (likely(ret == 0)) {
7200 /* The filter did not match the response, it can be
7201 * iterated through all headers.
7202 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007203 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 }
7206 return 0;
7207}
7208
7209
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007211 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007212 * desirable to call it only when needed. This function is also used when we
7213 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007215void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216{
7217 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007218 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007219 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007221 char *hdr_beg, *hdr_end, *hdr_next;
7222 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 /* Iterate through the headers.
7225 * we start with the start line.
7226 */
7227 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007228 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229
7230 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7231 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007232 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233
7234 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007235 hdr_beg = hdr_next;
7236 hdr_end = hdr_beg + cur_hdr->len;
7237 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238
Willy Tarreau24581ba2010-08-31 22:39:35 +02007239 /* We have one full header between hdr_beg and hdr_end, and the
7240 * next header starts at hdr_next. We're only interested in
7241 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 */
7243
Willy Tarreau24581ba2010-08-31 22:39:35 +02007244 is_cookie2 = 0;
7245 prev = hdr_beg + 10;
7246 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007247 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007248 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7249 if (!val) {
7250 old_idx = cur_idx;
7251 continue;
7252 }
7253 is_cookie2 = 1;
7254 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 }
7256
Willy Tarreau24581ba2010-08-31 22:39:35 +02007257 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7258 * <prev> points to the colon.
7259 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007260 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261
Willy Tarreau24581ba2010-08-31 22:39:35 +02007262 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7263 * check-cache is enabled) and we are not interested in checking
7264 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007265 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007266 if (t->be->cookie_name == NULL &&
7267 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007268 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 return;
7270
Willy Tarreau24581ba2010-08-31 22:39:35 +02007271 /* OK so now we know we have to process this response cookie.
7272 * The format of the Set-Cookie header is slightly different
7273 * from the format of the Cookie header in that it does not
7274 * support the comma as a cookie delimiter (thus the header
7275 * cannot be folded) because the Expires attribute described in
7276 * the original Netscape's spec may contain an unquoted date
7277 * with a comma inside. We have to live with this because
7278 * many browsers don't support Max-Age and some browsers don't
7279 * support quoted strings. However the Set-Cookie2 header is
7280 * clean.
7281 *
7282 * We have to keep multiple pointers in order to support cookie
7283 * removal at the beginning, middle or end of header without
7284 * corrupting the header (in case of set-cookie2). A special
7285 * pointer, <scav> points to the beginning of the set-cookie-av
7286 * fields after the first semi-colon. The <next> pointer points
7287 * either to the end of line (set-cookie) or next unquoted comma
7288 * (set-cookie2). All of these headers are valid :
7289 *
7290 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7291 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7292 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7293 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7294 * | | | | | | | | | |
7295 * | | | | | | | | +-> next hdr_end <--+
7296 * | | | | | | | +------------> scav
7297 * | | | | | | +--------------> val_end
7298 * | | | | | +--------------------> val_beg
7299 * | | | | +----------------------> equal
7300 * | | | +------------------------> att_end
7301 * | | +----------------------------> att_beg
7302 * | +------------------------------> prev
7303 * +-----------------------------------------> hdr_beg
7304 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305
Willy Tarreau24581ba2010-08-31 22:39:35 +02007306 for (; prev < hdr_end; prev = next) {
7307 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308
Willy Tarreau24581ba2010-08-31 22:39:35 +02007309 /* find att_beg */
7310 att_beg = prev + 1;
7311 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7312 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313
Willy Tarreau24581ba2010-08-31 22:39:35 +02007314 /* find att_end : this is the first character after the last non
7315 * space before the equal. It may be equal to hdr_end.
7316 */
7317 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
Willy Tarreau24581ba2010-08-31 22:39:35 +02007319 while (equal < hdr_end) {
7320 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7321 break;
7322 if (http_is_spht[(unsigned char)*equal++])
7323 continue;
7324 att_end = equal;
7325 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7328 * is between <att_beg> and <equal>, both may be identical.
7329 */
7330
7331 /* look for end of cookie if there is an equal sign */
7332 if (equal < hdr_end && *equal == '=') {
7333 /* look for the beginning of the value */
7334 val_beg = equal + 1;
7335 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7336 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337
Willy Tarreau24581ba2010-08-31 22:39:35 +02007338 /* find the end of the value, respecting quotes */
7339 next = find_cookie_value_end(val_beg, hdr_end);
7340
7341 /* make val_end point to the first white space or delimitor after the value */
7342 val_end = next;
7343 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7344 val_end--;
7345 } else {
7346 /* <equal> points to next comma, semi-colon or EOL */
7347 val_beg = val_end = next = equal;
7348 }
7349
7350 if (next < hdr_end) {
7351 /* Set-Cookie2 supports multiple cookies, and <next> points to
7352 * a colon or semi-colon before the end. So skip all attr-value
7353 * pairs and look for the next comma. For Set-Cookie, since
7354 * commas are permitted in values, skip to the end.
7355 */
7356 if (is_cookie2)
7357 next = find_hdr_value_end(next, hdr_end);
7358 else
7359 next = hdr_end;
7360 }
7361
7362 /* Now everything is as on the diagram above */
7363
7364 /* Ignore cookies with no equal sign */
7365 if (equal == val_end)
7366 continue;
7367
7368 /* If there are spaces around the equal sign, we need to
7369 * strip them otherwise we'll get trouble for cookie captures,
7370 * or even for rewrites. Since this happens extremely rarely,
7371 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007373 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7374 int stripped_before = 0;
7375 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376
Willy Tarreau24581ba2010-08-31 22:39:35 +02007377 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007378 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007379 equal += stripped_before;
7380 val_beg += stripped_before;
7381 }
7382
7383 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007384 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007385 val_beg += stripped_after;
7386 stripped_before += stripped_after;
7387 }
7388
7389 val_end += stripped_before;
7390 next += stripped_before;
7391 hdr_end += stripped_before;
7392 hdr_next += stripped_before;
7393 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007394 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007395 }
7396
7397 /* First, let's see if we want to capture this cookie. We check
7398 * that we don't already have a server side cookie, because we
7399 * can only capture one. Also as an optimisation, we ignore
7400 * cookies shorter than the declared name.
7401 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007402 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007403 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 (val_end - att_beg >= t->fe->capture_namelen) &&
7405 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7406 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007407 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 Alert("HTTP logging : out of memory.\n");
7409 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007410 else {
7411 if (log_len > t->fe->capture_len)
7412 log_len = t->fe->capture_len;
7413 memcpy(txn->srv_cookie, att_beg, log_len);
7414 txn->srv_cookie[log_len] = 0;
7415 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 }
7417
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007418 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007419 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007420 if (!(t->flags & SN_IGNORE_PRST) &&
7421 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7422 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007423 /* assume passive cookie by default */
7424 txn->flags &= ~TX_SCK_MASK;
7425 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426
7427 /* If the cookie is in insert mode on a known server, we'll delete
7428 * this occurrence because we'll insert another one later.
7429 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007430 * a direct access.
7431 */
Willy Tarreau67402132012-05-31 20:40:20 +02007432 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007433 /* The "preserve" flag was set, we don't want to touch the
7434 * server's cookie.
7435 */
7436 }
Willy Tarreau67402132012-05-31 20:40:20 +02007437 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7438 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007439 /* this cookie must be deleted */
7440 if (*prev == ':' && next == hdr_end) {
7441 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007442 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007443 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7444 txn->hdr_idx.used--;
7445 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007446 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007447 hdr_next += delta;
7448 http_msg_move_end(&txn->rsp, delta);
7449 /* note: while both invalid now, <next> and <hdr_end>
7450 * are still equal, so the for() will stop as expected.
7451 */
7452 } else {
7453 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007454 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 next = prev;
7456 hdr_end += delta;
7457 hdr_next += delta;
7458 cur_hdr->len += delta;
7459 http_msg_move_end(&txn->rsp, delta);
7460 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007461 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007462 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007464 }
Willy Tarreau67402132012-05-31 20:40:20 +02007465 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007466 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 * with this server since we know it.
7468 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007469 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 next += delta;
7471 hdr_end += delta;
7472 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007474 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475
Willy Tarreauf1348312010-10-07 15:54:11 +02007476 txn->flags &= ~TX_SCK_MASK;
7477 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007478 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007479 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007481 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007483 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007484 next += delta;
7485 hdr_end += delta;
7486 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007488 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489
Willy Tarreau827aee92011-03-10 16:55:02 +01007490 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007491 txn->flags &= ~TX_SCK_MASK;
7492 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 }
7494 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007495 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7496 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007497 int cmp_len, value_len;
7498 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499
Cyril Bontéb21570a2009-11-29 20:04:48 +01007500 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007501 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7502 value_begin = att_beg + t->be->appsession_name_len;
7503 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007504 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007505 cmp_len = att_end - att_beg;
7506 value_begin = val_beg;
7507 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007508 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007509
Cyril Bonté17530c32010-04-06 21:11:10 +02007510 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007511 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7512 /* free a possibly previously allocated memory */
7513 pool_free2(apools.sessid, txn->sessid);
7514
Cyril Bontéb21570a2009-11-29 20:04:48 +01007515 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007516 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007517 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7518 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7519 return;
7520 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007521 memcpy(txn->sessid, value_begin, value_len);
7522 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 }
7525 /* that's done for this cookie, check the next one on the same
7526 * line when next != hdr_end (only if is_cookie2).
7527 */
7528 }
7529 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007532
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007533 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007534 appsess *asession = NULL;
7535 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007536 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007537 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007538 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007539 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7540 Alert("Not enough Memory process_srv():asession:calloc().\n");
7541 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7542 return;
7543 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007544 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7545
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007546 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7547 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7548 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007549 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007550 return;
7551 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007552 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007553 asession->sessid[t->be->appsession_len] = 0;
7554
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007555 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007556 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007557 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007558 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007559 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007560 return;
7561 }
7562 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007563 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007564
7565 asession->request_count = 0;
7566 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7567 }
7568
7569 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7570 asession->request_count++;
7571 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007572}
7573
7574
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575/*
7576 * Check if response is cacheable or not. Updates t->flags.
7577 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007578void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579{
7580 struct http_txn *txn = &t->txn;
7581 char *p1, *p2;
7582
7583 char *cur_ptr, *cur_end, *cur_next;
7584 int cur_idx;
7585
Willy Tarreau5df51872007-11-25 16:20:08 +01007586 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587 return;
7588
7589 /* Iterate through the headers.
7590 * we start with the start line.
7591 */
7592 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007593 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594
7595 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7596 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007597 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598
7599 cur_hdr = &txn->hdr_idx.v[cur_idx];
7600 cur_ptr = cur_next;
7601 cur_end = cur_ptr + cur_hdr->len;
7602 cur_next = cur_end + cur_hdr->cr + 1;
7603
7604 /* We have one full header between cur_ptr and cur_end, and the
7605 * next header starts at cur_next. We're only interested in
7606 * "Cookie:" headers.
7607 */
7608
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007609 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7610 if (val) {
7611 if ((cur_end - (cur_ptr + val) >= 8) &&
7612 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7613 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7614 return;
7615 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 }
7617
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007618 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7619 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 continue;
7621
7622 /* OK, right now we know we have a cache-control header at cur_ptr */
7623
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007624 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625
7626 if (p1 >= cur_end) /* no more info */
7627 continue;
7628
7629 /* p1 is at the beginning of the value */
7630 p2 = p1;
7631
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007632 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007633 p2++;
7634
7635 /* we have a complete value between p1 and p2 */
7636 if (p2 < cur_end && *p2 == '=') {
7637 /* we have something of the form no-cache="set-cookie" */
7638 if ((cur_end - p1 >= 21) &&
7639 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7640 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007641 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007642 continue;
7643 }
7644
7645 /* OK, so we know that either p2 points to the end of string or to a comma */
7646 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007647 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007648 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7649 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7650 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007651 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652 return;
7653 }
7654
7655 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007656 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657 continue;
7658 }
7659 }
7660}
7661
7662
Willy Tarreau58f10d72006-12-04 02:26:12 +01007663/*
7664 * Try to retrieve a known appsession in the URI, then the associated server.
7665 * If the server is found, it's assigned to the session.
7666 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007667void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007668{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007669 char *end_params, *first_param, *cur_param, *next_param;
7670 char separator;
7671 int value_len;
7672
7673 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007675 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007676 (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 +01007677 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Cyril Bontéb21570a2009-11-29 20:04:48 +01007680 first_param = NULL;
7681 switch (mode) {
7682 case PR_O2_AS_M_PP:
7683 first_param = memchr(begin, ';', len);
7684 break;
7685 case PR_O2_AS_M_QS:
7686 first_param = memchr(begin, '?', len);
7687 break;
7688 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Cyril Bontéb21570a2009-11-29 20:04:48 +01007690 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007692 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693
Cyril Bontéb21570a2009-11-29 20:04:48 +01007694 switch (mode) {
7695 case PR_O2_AS_M_PP:
7696 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7697 end_params = (char *) begin + len;
7698 }
7699 separator = ';';
7700 break;
7701 case PR_O2_AS_M_QS:
7702 end_params = (char *) begin + len;
7703 separator = '&';
7704 break;
7705 default:
7706 /* unknown mode, shouldn't happen */
7707 return;
7708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709
Cyril Bontéb21570a2009-11-29 20:04:48 +01007710 cur_param = next_param = end_params;
7711 while (cur_param > first_param) {
7712 cur_param--;
7713 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7714 /* let's see if this is the appsession parameter */
7715 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7716 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7717 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7718 /* Cool... it's the right one */
7719 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7720 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7721 if (value_len > 0) {
7722 manage_client_side_appsession(t, cur_param, value_len);
7723 }
7724 break;
7725 }
7726 next_param = cur_param;
7727 }
7728 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007730 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007731 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007732#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733}
7734
Willy Tarreaub2513902006-12-17 14:52:38 +01007735/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007736 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007737 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007738 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007739 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007740 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007741 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007742 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007743 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007744int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007745{
7746 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007747 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007748 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007749
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007750 if (!uri_auth)
7751 return 0;
7752
Cyril Bonté70be45d2010-10-12 00:14:35 +02007753 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007754 return 0;
7755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007756 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007757 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007758 return 0;
7759
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007760 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007761 return 0;
7762
Willy Tarreaub2513902006-12-17 14:52:38 +01007763 return 1;
7764}
7765
Willy Tarreau4076a152009-04-02 15:18:36 +02007766/*
7767 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007768 * By default it tries to report the error position as msg->err_pos. However if
7769 * this one is not set, it will then report msg->next, which is the last known
7770 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007771 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007772 */
7773void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007774 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007775 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007776{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007777 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007778 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007779
Willy Tarreau9b28e032012-10-12 23:49:43 +02007780 es->len = MIN(chn->buf->i, sizeof(es->buf));
7781 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007782 len1 = MIN(len1, es->len);
7783 len2 = es->len - len1; /* remaining data if buffer wraps */
7784
Willy Tarreau9b28e032012-10-12 23:49:43 +02007785 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007786 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007787 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007788
Willy Tarreau4076a152009-04-02 15:18:36 +02007789 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007790 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007791 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007792 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007793
Willy Tarreau4076a152009-04-02 15:18:36 +02007794 es->when = date; // user-visible date
7795 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007796 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007797 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007798 if (objt_conn(s->req->prod->end))
7799 es->src = __objt_conn(s->req->prod->end)->addr.from;
7800 else
7801 memset(&es->src, 0, sizeof(es->src));
7802
Willy Tarreau078272e2010-12-12 12:46:33 +01007803 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007804 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007805 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007806 es->s_flags = s->flags;
7807 es->t_flags = s->txn.flags;
7808 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007809 es->b_out = chn->buf->o;
7810 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007811 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007812 es->m_clen = msg->chunk_len;
7813 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007814}
Willy Tarreaub2513902006-12-17 14:52:38 +01007815
Willy Tarreau294c4732011-12-16 21:35:50 +01007816/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7817 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7818 * performed over the whole headers. Otherwise it must contain a valid header
7819 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7820 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7821 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7822 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007823 * -1. The value fetch stops at commas, so this function is suited for use with
7824 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007825 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007826 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007827unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007828 struct hdr_idx *idx, int occ,
7829 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007830{
Willy Tarreau294c4732011-12-16 21:35:50 +01007831 struct hdr_ctx local_ctx;
7832 char *ptr_hist[MAX_HDR_HISTORY];
7833 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007834 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007835 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007836
Willy Tarreau294c4732011-12-16 21:35:50 +01007837 if (!ctx) {
7838 local_ctx.idx = 0;
7839 ctx = &local_ctx;
7840 }
7841
Willy Tarreaubce70882009-09-07 11:51:47 +02007842 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007843 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007845 occ--;
7846 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007847 *vptr = ctx->line + ctx->val;
7848 *vlen = ctx->vlen;
7849 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007850 }
7851 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007852 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007853 }
7854
7855 /* negative occurrence, we scan all the list then walk back */
7856 if (-occ > MAX_HDR_HISTORY)
7857 return 0;
7858
Willy Tarreau294c4732011-12-16 21:35:50 +01007859 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007860 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007861 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7862 len_hist[hist_ptr] = ctx->vlen;
7863 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007864 hist_ptr = 0;
7865 found++;
7866 }
7867 if (-occ > found)
7868 return 0;
7869 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007870 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7871 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7872 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007873 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007874 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007875 if (hist_ptr >= MAX_HDR_HISTORY)
7876 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007877 *vptr = ptr_hist[hist_ptr];
7878 *vlen = len_hist[hist_ptr];
7879 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007880}
7881
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007882/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7883 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7884 * performed over the whole headers. Otherwise it must contain a valid header
7885 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7886 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7887 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7888 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7889 * -1. This function differs from http_get_hdr() in that it only returns full
7890 * line header values and does not stop at commas.
7891 * The return value is 0 if nothing was found, or non-zero otherwise.
7892 */
7893unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7894 struct hdr_idx *idx, int occ,
7895 struct hdr_ctx *ctx, char **vptr, int *vlen)
7896{
7897 struct hdr_ctx local_ctx;
7898 char *ptr_hist[MAX_HDR_HISTORY];
7899 int len_hist[MAX_HDR_HISTORY];
7900 unsigned int hist_ptr;
7901 int found;
7902
7903 if (!ctx) {
7904 local_ctx.idx = 0;
7905 ctx = &local_ctx;
7906 }
7907
7908 if (occ >= 0) {
7909 /* search from the beginning */
7910 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7911 occ--;
7912 if (occ <= 0) {
7913 *vptr = ctx->line + ctx->val;
7914 *vlen = ctx->vlen;
7915 return 1;
7916 }
7917 }
7918 return 0;
7919 }
7920
7921 /* negative occurrence, we scan all the list then walk back */
7922 if (-occ > MAX_HDR_HISTORY)
7923 return 0;
7924
7925 found = hist_ptr = 0;
7926 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7927 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7928 len_hist[hist_ptr] = ctx->vlen;
7929 if (++hist_ptr >= MAX_HDR_HISTORY)
7930 hist_ptr = 0;
7931 found++;
7932 }
7933 if (-occ > found)
7934 return 0;
7935 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7936 * find occurrence -occ, so we have to check [hist_ptr+occ].
7937 */
7938 hist_ptr += occ;
7939 if (hist_ptr >= MAX_HDR_HISTORY)
7940 hist_ptr -= MAX_HDR_HISTORY;
7941 *vptr = ptr_hist[hist_ptr];
7942 *vlen = len_hist[hist_ptr];
7943 return 1;
7944}
7945
Willy Tarreaubaaee002006-06-26 02:48:02 +02007946/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007947 * Print a debug line with a header. Always stop at the first CR or LF char,
7948 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7949 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950 */
7951void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7952{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007953 int max;
7954 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007955 dir,
7956 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
7957 objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007958
7959 for (max = 0; start + max < end; max++)
7960 if (start[max] == '\r' || start[max] == '\n')
7961 break;
7962
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007963 UBOUND(max, trash.size - trash.len - 3);
7964 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7965 trash.str[trash.len++] = '\n';
7966 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967}
7968
Willy Tarreau0937bc42009-12-22 15:03:09 +01007969/*
7970 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7971 * the required fields are properly allocated and that we only need to (re)init
7972 * them. This should be used before processing any new request.
7973 */
7974void http_init_txn(struct session *s)
7975{
7976 struct http_txn *txn = &s->txn;
7977 struct proxy *fe = s->fe;
7978
7979 txn->flags = 0;
7980 txn->status = -1;
7981
Willy Tarreauf64d1412010-10-07 20:06:11 +02007982 txn->cookie_first_date = 0;
7983 txn->cookie_last_date = 0;
7984
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007985 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007986 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007987 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007988 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007989 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007990 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007991 txn->req.chunk_len = 0LL;
7992 txn->req.body_len = 0LL;
7993 txn->rsp.chunk_len = 0LL;
7994 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007995 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7996 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007997 txn->req.chn = s->req;
7998 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007999
8000 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008001
8002 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8003 if (fe->options2 & PR_O2_REQBUG_OK)
8004 txn->req.err_pos = -1; /* let buggy requests pass */
8005
Willy Tarreau46023632010-01-07 22:51:47 +01008006 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008007 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8008
Willy Tarreau46023632010-01-07 22:51:47 +01008009 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008010 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8011
8012 if (txn->hdr_idx.v)
8013 hdr_idx_init(&txn->hdr_idx);
8014}
8015
8016/* to be used at the end of a transaction */
8017void http_end_txn(struct session *s)
8018{
8019 struct http_txn *txn = &s->txn;
8020
8021 /* these ones will have been dynamically allocated */
8022 pool_free2(pool2_requri, txn->uri);
8023 pool_free2(pool2_capture, txn->cli_cookie);
8024 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008025 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008026 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008027
William Lallemanda73203e2012-03-12 12:48:57 +01008028 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008029 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008030 txn->uri = NULL;
8031 txn->srv_cookie = NULL;
8032 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008033
8034 if (txn->req.cap) {
8035 struct cap_hdr *h;
8036 for (h = s->fe->req_cap; h; h = h->next)
8037 pool_free2(h->pool, txn->req.cap[h->index]);
8038 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8039 }
8040
8041 if (txn->rsp.cap) {
8042 struct cap_hdr *h;
8043 for (h = s->fe->rsp_cap; h; h = h->next)
8044 pool_free2(h->pool, txn->rsp.cap[h->index]);
8045 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8046 }
8047
Willy Tarreau0937bc42009-12-22 15:03:09 +01008048}
8049
8050/* to be used at the end of a transaction to prepare a new one */
8051void http_reset_txn(struct session *s)
8052{
8053 http_end_txn(s);
8054 http_init_txn(s);
8055
8056 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008057 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008058 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008059 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008060 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008061 /* re-init store persistence */
8062 s->store_count = 0;
8063
Willy Tarreau0937bc42009-12-22 15:03:09 +01008064 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008065
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008066 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008067
Willy Tarreau739cfba2010-01-25 23:11:14 +01008068 /* We must trim any excess data from the response buffer, because we
8069 * may have blocked an invalid response from a server that we don't
8070 * want to accidentely forward once we disable the analysers, nor do
8071 * we want those data to come along with next response. A typical
8072 * example of such data would be from a buggy server responding to
8073 * a HEAD with some data, or sending more than the advertised
8074 * content-length.
8075 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008076 if (unlikely(s->rep->buf->i))
8077 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008078
Willy Tarreau0937bc42009-12-22 15:03:09 +01008079 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008080 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008081
Willy Tarreaud04e8582010-05-31 12:31:35 +02008082 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008083 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008084
8085 s->req->rex = TICK_ETERNITY;
8086 s->req->wex = TICK_ETERNITY;
8087 s->req->analyse_exp = TICK_ETERNITY;
8088 s->rep->rex = TICK_ETERNITY;
8089 s->rep->wex = TICK_ETERNITY;
8090 s->rep->analyse_exp = TICK_ETERNITY;
8091}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008092
Willy Tarreauff011f22011-01-06 17:51:27 +01008093void free_http_req_rules(struct list *r) {
8094 struct http_req_rule *tr, *pr;
8095
8096 list_for_each_entry_safe(pr, tr, r, list) {
8097 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008098 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008099 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008100
8101 free(pr);
8102 }
8103}
8104
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008105/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008106struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8107{
8108 struct http_req_rule *rule;
8109 int cur_arg;
8110
8111 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8112 if (!rule) {
8113 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008114 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008115 }
8116
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008117 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008118 rule->action = HTTP_REQ_ACT_ALLOW;
8119 cur_arg = 1;
8120 } else if (!strcmp(args[0], "deny")) {
8121 rule->action = HTTP_REQ_ACT_DENY;
8122 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008123 } else if (!strcmp(args[0], "tarpit")) {
8124 rule->action = HTTP_REQ_ACT_TARPIT;
8125 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008126 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008127 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008128 cur_arg = 1;
8129
8130 while(*args[cur_arg]) {
8131 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008132 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008133 cur_arg+=2;
8134 continue;
8135 } else
8136 break;
8137 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008138 } else if (!strcmp(args[0], "set-nice")) {
8139 rule->action = HTTP_REQ_ACT_SET_NICE;
8140 cur_arg = 1;
8141
8142 if (!*args[cur_arg] ||
8143 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8144 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8145 file, linenum, args[0]);
8146 goto out_err;
8147 }
8148 rule->arg.nice = atoi(args[cur_arg]);
8149 if (rule->arg.nice < -1024)
8150 rule->arg.nice = -1024;
8151 else if (rule->arg.nice > 1024)
8152 rule->arg.nice = 1024;
8153 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008154 } else if (!strcmp(args[0], "set-tos")) {
8155#ifdef IP_TOS
8156 char *err;
8157 rule->action = HTTP_REQ_ACT_SET_TOS;
8158 cur_arg = 1;
8159
8160 if (!*args[cur_arg] ||
8161 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8162 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8163 file, linenum, args[0]);
8164 goto out_err;
8165 }
8166
8167 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8168 if (err && *err != '\0') {
8169 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8170 file, linenum, err, args[0]);
8171 goto out_err;
8172 }
8173 cur_arg++;
8174#else
8175 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8176 goto out_err;
8177#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008178 } else if (!strcmp(args[0], "set-mark")) {
8179#ifdef SO_MARK
8180 char *err;
8181 rule->action = HTTP_REQ_ACT_SET_MARK;
8182 cur_arg = 1;
8183
8184 if (!*args[cur_arg] ||
8185 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8186 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8187 file, linenum, args[0]);
8188 goto out_err;
8189 }
8190
8191 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8192 if (err && *err != '\0') {
8193 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8194 file, linenum, err, args[0]);
8195 goto out_err;
8196 }
8197 cur_arg++;
8198 global.last_checks |= LSTCHK_NETADM;
8199#else
8200 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8201 goto out_err;
8202#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008203 } else if (!strcmp(args[0], "set-log-level")) {
8204 rule->action = HTTP_REQ_ACT_SET_LOGL;
8205 cur_arg = 1;
8206
8207 if (!*args[cur_arg] ||
8208 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8209 bad_log_level:
8210 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8211 file, linenum, args[0]);
8212 goto out_err;
8213 }
8214 if (strcmp(args[cur_arg], "silent") == 0)
8215 rule->arg.loglevel = -1;
8216 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8217 goto bad_log_level;
8218 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008219 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8220 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8221 cur_arg = 1;
8222
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008223 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8224 (*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 +01008225 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8226 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008227 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008228 }
8229
8230 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8231 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8232 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008233
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008234 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008235 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8236 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008237 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008238 } else if (strcmp(args[0], "redirect") == 0) {
8239 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008240 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008241
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008242 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008243 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8244 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8245 goto out_err;
8246 }
8247
8248 /* this redirect rule might already contain a parsed condition which
8249 * we'll pass to the http-request rule.
8250 */
8251 rule->action = HTTP_REQ_ACT_REDIR;
8252 rule->arg.redir = redir;
8253 rule->cond = redir->cond;
8254 redir->cond = NULL;
8255 cur_arg = 2;
8256 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008257 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008258 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008259 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008260 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008261 }
8262
8263 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8264 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008265 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008266
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008267 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8268 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8269 file, linenum, args[0], errmsg);
8270 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008271 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008272 }
8273 rule->cond = cond;
8274 }
8275 else if (*args[cur_arg]) {
8276 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8277 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8278 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008279 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008280 }
8281
8282 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008283 out_err:
8284 free(rule);
8285 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008286}
8287
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008288/* parse an "http-respose" rule */
8289struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8290{
8291 struct http_res_rule *rule;
8292 int cur_arg;
8293
8294 rule = calloc(1, sizeof(*rule));
8295 if (!rule) {
8296 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8297 goto out_err;
8298 }
8299
8300 if (!strcmp(args[0], "allow")) {
8301 rule->action = HTTP_RES_ACT_ALLOW;
8302 cur_arg = 1;
8303 } else if (!strcmp(args[0], "deny")) {
8304 rule->action = HTTP_RES_ACT_DENY;
8305 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008306 } else if (!strcmp(args[0], "set-nice")) {
8307 rule->action = HTTP_RES_ACT_SET_NICE;
8308 cur_arg = 1;
8309
8310 if (!*args[cur_arg] ||
8311 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8312 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8313 file, linenum, args[0]);
8314 goto out_err;
8315 }
8316 rule->arg.nice = atoi(args[cur_arg]);
8317 if (rule->arg.nice < -1024)
8318 rule->arg.nice = -1024;
8319 else if (rule->arg.nice > 1024)
8320 rule->arg.nice = 1024;
8321 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008322 } else if (!strcmp(args[0], "set-tos")) {
8323#ifdef IP_TOS
8324 char *err;
8325 rule->action = HTTP_RES_ACT_SET_TOS;
8326 cur_arg = 1;
8327
8328 if (!*args[cur_arg] ||
8329 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8330 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8331 file, linenum, args[0]);
8332 goto out_err;
8333 }
8334
8335 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8336 if (err && *err != '\0') {
8337 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8338 file, linenum, err, args[0]);
8339 goto out_err;
8340 }
8341 cur_arg++;
8342#else
8343 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8344 goto out_err;
8345#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008346 } else if (!strcmp(args[0], "set-mark")) {
8347#ifdef SO_MARK
8348 char *err;
8349 rule->action = HTTP_RES_ACT_SET_MARK;
8350 cur_arg = 1;
8351
8352 if (!*args[cur_arg] ||
8353 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8354 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8355 file, linenum, args[0]);
8356 goto out_err;
8357 }
8358
8359 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8360 if (err && *err != '\0') {
8361 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8362 file, linenum, err, args[0]);
8363 goto out_err;
8364 }
8365 cur_arg++;
8366 global.last_checks |= LSTCHK_NETADM;
8367#else
8368 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8369 goto out_err;
8370#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008371 } else if (!strcmp(args[0], "set-log-level")) {
8372 rule->action = HTTP_RES_ACT_SET_LOGL;
8373 cur_arg = 1;
8374
8375 if (!*args[cur_arg] ||
8376 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8377 bad_log_level:
8378 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8379 file, linenum, args[0]);
8380 goto out_err;
8381 }
8382 if (strcmp(args[cur_arg], "silent") == 0)
8383 rule->arg.loglevel = -1;
8384 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8385 goto bad_log_level;
8386 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008387 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8388 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8389 cur_arg = 1;
8390
8391 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8392 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8393 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8394 file, linenum, args[0]);
8395 goto out_err;
8396 }
8397
8398 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8399 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8400 LIST_INIT(&rule->arg.hdr_add.fmt);
8401
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008402 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008403 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8404 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8405 cur_arg += 2;
8406 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008407 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008408 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8409 goto out_err;
8410 }
8411
8412 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8413 struct acl_cond *cond;
8414 char *errmsg = NULL;
8415
8416 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8417 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8418 file, linenum, args[0], errmsg);
8419 free(errmsg);
8420 goto out_err;
8421 }
8422 rule->cond = cond;
8423 }
8424 else if (*args[cur_arg]) {
8425 Alert("parsing [%s:%d]: 'http-response %s' expects"
8426 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8427 file, linenum, args[0], args[cur_arg]);
8428 goto out_err;
8429 }
8430
8431 return rule;
8432 out_err:
8433 free(rule);
8434 return NULL;
8435}
8436
Willy Tarreau4baae242012-12-27 12:00:31 +01008437/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008438 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8439 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008440 */
8441struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008442 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008443{
8444 struct redirect_rule *rule;
8445 int cur_arg;
8446 int type = REDIRECT_TYPE_NONE;
8447 int code = 302;
8448 const char *destination = NULL;
8449 const char *cookie = NULL;
8450 int cookie_set = 0;
8451 unsigned int flags = REDIRECT_FLAG_NONE;
8452 struct acl_cond *cond = NULL;
8453
8454 cur_arg = 0;
8455 while (*(args[cur_arg])) {
8456 if (strcmp(args[cur_arg], "location") == 0) {
8457 if (!*args[cur_arg + 1])
8458 goto missing_arg;
8459
8460 type = REDIRECT_TYPE_LOCATION;
8461 cur_arg++;
8462 destination = args[cur_arg];
8463 }
8464 else if (strcmp(args[cur_arg], "prefix") == 0) {
8465 if (!*args[cur_arg + 1])
8466 goto missing_arg;
8467
8468 type = REDIRECT_TYPE_PREFIX;
8469 cur_arg++;
8470 destination = args[cur_arg];
8471 }
8472 else if (strcmp(args[cur_arg], "scheme") == 0) {
8473 if (!*args[cur_arg + 1])
8474 goto missing_arg;
8475
8476 type = REDIRECT_TYPE_SCHEME;
8477 cur_arg++;
8478 destination = args[cur_arg];
8479 }
8480 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8481 if (!*args[cur_arg + 1])
8482 goto missing_arg;
8483
8484 cur_arg++;
8485 cookie = args[cur_arg];
8486 cookie_set = 1;
8487 }
8488 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8489 if (!*args[cur_arg + 1])
8490 goto missing_arg;
8491
8492 cur_arg++;
8493 cookie = args[cur_arg];
8494 cookie_set = 0;
8495 }
8496 else if (strcmp(args[cur_arg], "code") == 0) {
8497 if (!*args[cur_arg + 1])
8498 goto missing_arg;
8499
8500 cur_arg++;
8501 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008502 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008503 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008504 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008505 args[cur_arg - 1], args[cur_arg]);
8506 return NULL;
8507 }
8508 }
8509 else if (!strcmp(args[cur_arg],"drop-query")) {
8510 flags |= REDIRECT_FLAG_DROP_QS;
8511 }
8512 else if (!strcmp(args[cur_arg],"append-slash")) {
8513 flags |= REDIRECT_FLAG_APPEND_SLASH;
8514 }
8515 else if (strcmp(args[cur_arg], "if") == 0 ||
8516 strcmp(args[cur_arg], "unless") == 0) {
8517 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8518 if (!cond) {
8519 memprintf(errmsg, "error in condition: %s", *errmsg);
8520 return NULL;
8521 }
8522 break;
8523 }
8524 else {
8525 memprintf(errmsg,
8526 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8527 args[cur_arg]);
8528 return NULL;
8529 }
8530 cur_arg++;
8531 }
8532
8533 if (type == REDIRECT_TYPE_NONE) {
8534 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8535 return NULL;
8536 }
8537
8538 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8539 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008540 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008541
8542 if (!use_fmt) {
8543 /* old-style static redirect rule */
8544 rule->rdr_str = strdup(destination);
8545 rule->rdr_len = strlen(destination);
8546 }
8547 else {
8548 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008549
8550 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8551 * if prefix == "/", we don't want to add anything, otherwise it
8552 * makes it hard for the user to configure a self-redirection.
8553 */
8554 proxy->conf.args.ctx = ARGC_RDR;
8555 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8556 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8557 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8558 }
8559 }
8560
Willy Tarreau4baae242012-12-27 12:00:31 +01008561 if (cookie) {
8562 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8563 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8564 */
8565 rule->cookie_len = strlen(cookie);
8566 if (cookie_set) {
8567 rule->cookie_str = malloc(rule->cookie_len + 10);
8568 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8569 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8570 rule->cookie_len += 9;
8571 } else {
8572 rule->cookie_str = malloc(rule->cookie_len + 21);
8573 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8574 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8575 rule->cookie_len += 20;
8576 }
8577 }
8578 rule->type = type;
8579 rule->code = code;
8580 rule->flags = flags;
8581 LIST_INIT(&rule->list);
8582 return rule;
8583
8584 missing_arg:
8585 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8586 return NULL;
8587}
8588
Willy Tarreau8797c062007-05-07 00:55:35 +02008589/************************************************************************/
8590/* The code below is dedicated to ACL parsing and matching */
8591/************************************************************************/
8592
8593
Willy Tarreau14174bc2012-04-16 14:34:04 +02008594/* This function ensures that the prerequisites for an L7 fetch are ready,
8595 * which means that a request or response is ready. If some data is missing,
8596 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008597 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8598 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008599 *
8600 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008601 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8602 * decide whether or not an HTTP message is present ;
8603 * 0 if the requested data cannot be fetched or if it is certain that
8604 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008605 * 1 if an HTTP message is ready
8606 */
8607static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008608smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008609 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008610{
8611 struct http_txn *txn = l7;
8612 struct http_msg *msg = &txn->req;
8613
8614 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8615 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8616 */
8617
8618 if (unlikely(!s || !txn))
8619 return 0;
8620
8621 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008622 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008623
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008624 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008625 if (unlikely(!s->req))
8626 return 0;
8627
Willy Tarreauaae75e32013-03-29 12:31:49 +01008628 /* If the buffer does not leave enough free space at the end,
8629 * we must first realign it.
8630 */
8631 if (s->req->buf->p > s->req->buf->data &&
8632 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8633 buffer_slow_realign(s->req->buf);
8634
Willy Tarreau14174bc2012-04-16 14:34:04 +02008635 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008636 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008637 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008638
8639 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008640 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008641 http_msg_analyzer(msg, &txn->hdr_idx);
8642
8643 /* Still no valid request ? */
8644 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008645 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008646 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008647 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008648 }
8649 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008650 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008651 return 0;
8652 }
8653
8654 /* OK we just got a valid HTTP request. We have some minor
8655 * preparation to perform so that further checks can rely
8656 * on HTTP tests.
8657 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008658
8659 /* If the request was parsed but was too large, we must absolutely
8660 * return an error so that it is not processed. At the moment this
8661 * cannot happen, but if the parsers are to change in the future,
8662 * we want this check to be maintained.
8663 */
8664 if (unlikely(s->req->buf->i + s->req->buf->p >
8665 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8666 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008667 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008668 return 1;
8669 }
8670
Willy Tarreau9b28e032012-10-12 23:49:43 +02008671 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008672 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8673 s->flags |= SN_REDIRECTABLE;
8674
Willy Tarreau506d0502013-07-06 13:29:24 +02008675 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8676 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008677 }
8678
Willy Tarreau506d0502013-07-06 13:29:24 +02008679 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008680 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008681 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008682
8683 /* otherwise everything's ready for the request */
8684 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008685 else {
8686 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008687 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8688 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008689 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008690 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008691 }
8692
8693 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008694 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008695 return 1;
8696}
Willy Tarreau8797c062007-05-07 00:55:35 +02008697
Willy Tarreauc0239e02012-04-16 14:42:55 +02008698#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008699 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02008700
Willy Tarreau24e32d82012-04-23 23:55:44 +02008701#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008702 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02008703
Willy Tarreau8797c062007-05-07 00:55:35 +02008704
8705/* 1. Check on METHOD
8706 * We use the pre-parsed method if it is known, and store its number as an
8707 * integer. If it is unknown, we use the pointer and the length.
8708 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008709static int pat_parse_meth(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008710{
8711 int len, meth;
8712
Willy Tarreauae8b7962007-06-09 23:10:04 +02008713 len = strlen(*text);
8714 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008715
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008716 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008717 pattern->val.i = meth;
8718 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008719 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008720 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008721 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008722 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008723 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008724 pattern->len = len;
8725 }
8726 return 1;
8727}
8728
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008729/* This function fetches the method of current HTTP request and stores
8730 * it in the global pattern struct as a chunk. There are two possibilities :
8731 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8732 * in <len> and <ptr> is NULL ;
8733 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8734 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008735 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008736 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008737static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008738smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008739 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008740{
8741 int meth;
8742 struct http_txn *txn = l7;
8743
Willy Tarreau24e32d82012-04-23 23:55:44 +02008744 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008745
Willy Tarreau8797c062007-05-07 00:55:35 +02008746 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008747 smp->type = SMP_T_UINT;
8748 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008749 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008750 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8751 /* ensure the indexes are not affected */
8752 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008753 smp->type = SMP_T_CSTR;
8754 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008755 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008756 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008757 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008758 return 1;
8759}
8760
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008761/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008762static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008763{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008764 int icase;
8765
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008766
Willy Tarreauf853c462012-04-23 18:53:56 +02008767 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008768 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008769 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008770 return PAT_MATCH;
8771 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008772 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008773
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008774 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8775 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008776 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008777
8778 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008779 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008780 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008781
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008782 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008783 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8784 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008785 return PAT_NOMATCH;
8786 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008787}
8788
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008789static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008790smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008791 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008792{
8793 struct http_txn *txn = l7;
8794 char *ptr;
8795 int len;
8796
Willy Tarreauc0239e02012-04-16 14:42:55 +02008797 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008798
Willy Tarreau8797c062007-05-07 00:55:35 +02008799 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008800 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008801
8802 while ((len-- > 0) && (*ptr++ != '/'));
8803 if (len <= 0)
8804 return 0;
8805
Willy Tarreauf853c462012-04-23 18:53:56 +02008806 smp->type = SMP_T_CSTR;
8807 smp->data.str.str = ptr;
8808 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008809
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008810 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008811 return 1;
8812}
8813
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008814static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008815smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008816 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008817{
8818 struct http_txn *txn = l7;
8819 char *ptr;
8820 int len;
8821
Willy Tarreauc0239e02012-04-16 14:42:55 +02008822 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008823
Willy Tarreauf26b2522012-12-14 08:33:14 +01008824 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8825 return 0;
8826
Willy Tarreau8797c062007-05-07 00:55:35 +02008827 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008828 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008829
8830 while ((len-- > 0) && (*ptr++ != '/'));
8831 if (len <= 0)
8832 return 0;
8833
Willy Tarreauf853c462012-04-23 18:53:56 +02008834 smp->type = SMP_T_CSTR;
8835 smp->data.str.str = ptr;
8836 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008837
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008838 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008839 return 1;
8840}
8841
8842/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008843static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008844smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008845 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008846{
8847 struct http_txn *txn = l7;
8848 char *ptr;
8849 int len;
8850
Willy Tarreauc0239e02012-04-16 14:42:55 +02008851 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008852
Willy Tarreauf26b2522012-12-14 08:33:14 +01008853 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8854 return 0;
8855
Willy Tarreau8797c062007-05-07 00:55:35 +02008856 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008857 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008858
Willy Tarreauf853c462012-04-23 18:53:56 +02008859 smp->type = SMP_T_UINT;
8860 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008861 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008862 return 1;
8863}
8864
8865/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008866static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008867smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008868 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008869{
8870 struct http_txn *txn = l7;
8871
Willy Tarreauc0239e02012-04-16 14:42:55 +02008872 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008873
Willy Tarreauf853c462012-04-23 18:53:56 +02008874 smp->type = SMP_T_CSTR;
8875 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008876 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008877 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008878 return 1;
8879}
8880
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008881static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008882smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008883 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008884{
8885 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008886 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008887
Willy Tarreauc0239e02012-04-16 14:42:55 +02008888 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008889
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008890 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8891 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008892 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008893
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008894 smp->type = SMP_T_IPV4;
8895 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008896 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008897 return 1;
8898}
8899
8900static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008901smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008902 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008903{
8904 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008905 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008906
Willy Tarreauc0239e02012-04-16 14:42:55 +02008907 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008908
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008909 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8910 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
8911 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008912
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008913 smp->type = SMP_T_UINT;
8914 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008915 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008916 return 1;
8917}
8918
Willy Tarreau185b5c42012-04-26 15:11:51 +02008919/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8920 * Accepts an optional argument of type string containing the header field name,
8921 * and an optional argument of type signed or unsigned integer to request an
8922 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008923 * headers are considered from the first one. It does not stop on commas and
8924 * returns full lines instead (useful for User-Agent or Date for example).
8925 */
8926static int
8927smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008928 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008929{
8930 struct http_txn *txn = l7;
8931 struct hdr_idx *idx = &txn->hdr_idx;
8932 struct hdr_ctx *ctx = smp->ctx.a[0];
8933 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8934 int occ = 0;
8935 const char *name_str = NULL;
8936 int name_len = 0;
8937
8938 if (!ctx) {
8939 /* first call */
8940 ctx = &static_hdr_ctx;
8941 ctx->idx = 0;
8942 smp->ctx.a[0] = ctx;
8943 }
8944
8945 if (args) {
8946 if (args[0].type != ARGT_STR)
8947 return 0;
8948 name_str = args[0].data.str.str;
8949 name_len = args[0].data.str.len;
8950
8951 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8952 occ = args[1].data.uint;
8953 }
8954
8955 CHECK_HTTP_MESSAGE_FIRST();
8956
8957 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
8958 /* search for header from the beginning */
8959 ctx->idx = 0;
8960
8961 if (!occ && !(opt & SMP_OPT_ITERATE))
8962 /* no explicit occurrence and single fetch => last header by default */
8963 occ = -1;
8964
8965 if (!occ)
8966 /* prepare to report multiple occurrences for ACL fetches */
8967 smp->flags |= SMP_F_NOT_LAST;
8968
8969 smp->type = SMP_T_CSTR;
8970 smp->flags |= SMP_F_VOL_HDR;
8971 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
8972 return 1;
8973
8974 smp->flags &= ~SMP_F_NOT_LAST;
8975 return 0;
8976}
8977
8978/* 6. Check on HTTP header count. The number of occurrences is returned.
8979 * Accepts exactly 1 argument of type string. It does not stop on commas and
8980 * returns full lines instead (useful for User-Agent or Date for example).
8981 */
8982static int
8983smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008984 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008985{
8986 struct http_txn *txn = l7;
8987 struct hdr_idx *idx = &txn->hdr_idx;
8988 struct hdr_ctx ctx;
8989 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8990 int cnt;
8991
8992 if (!args || args->type != ARGT_STR)
8993 return 0;
8994
8995 CHECK_HTTP_MESSAGE_FIRST();
8996
8997 ctx.idx = 0;
8998 cnt = 0;
8999 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9000 cnt++;
9001
9002 smp->type = SMP_T_UINT;
9003 smp->data.uint = cnt;
9004 smp->flags = SMP_F_VOL_HDR;
9005 return 1;
9006}
9007
9008/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9009 * Accepts an optional argument of type string containing the header field name,
9010 * and an optional argument of type signed or unsigned integer to request an
9011 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009012 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009013 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009014static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009015smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009016 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009017{
9018 struct http_txn *txn = l7;
9019 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009020 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009021 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009022 int occ = 0;
9023 const char *name_str = NULL;
9024 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009025
Willy Tarreaua890d072013-04-02 12:01:06 +02009026 if (!ctx) {
9027 /* first call */
9028 ctx = &static_hdr_ctx;
9029 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009030 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009031 }
9032
Willy Tarreau185b5c42012-04-26 15:11:51 +02009033 if (args) {
9034 if (args[0].type != ARGT_STR)
9035 return 0;
9036 name_str = args[0].data.str.str;
9037 name_len = args[0].data.str.len;
9038
9039 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9040 occ = args[1].data.uint;
9041 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009042
Willy Tarreaue333ec92012-04-16 16:26:40 +02009043 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009044
Willy Tarreau185b5c42012-04-26 15:11:51 +02009045 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009046 /* search for header from the beginning */
9047 ctx->idx = 0;
9048
Willy Tarreau185b5c42012-04-26 15:11:51 +02009049 if (!occ && !(opt & SMP_OPT_ITERATE))
9050 /* no explicit occurrence and single fetch => last header by default */
9051 occ = -1;
9052
9053 if (!occ)
9054 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009055 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009056
Willy Tarreau185b5c42012-04-26 15:11:51 +02009057 smp->type = SMP_T_CSTR;
9058 smp->flags |= SMP_F_VOL_HDR;
9059 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 +02009060 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009061
Willy Tarreau37406352012-04-23 16:16:37 +02009062 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009063 return 0;
9064}
9065
Willy Tarreauc11416f2007-06-17 16:58:38 +02009066/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009067 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009068 */
9069static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009070smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009071 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009072{
9073 struct http_txn *txn = l7;
9074 struct hdr_idx *idx = &txn->hdr_idx;
9075 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009076 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009077 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009078
Willy Tarreau24e32d82012-04-23 23:55:44 +02009079 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009080 return 0;
9081
Willy Tarreaue333ec92012-04-16 16:26:40 +02009082 CHECK_HTTP_MESSAGE_FIRST();
9083
Willy Tarreau33a7e692007-06-10 19:45:56 +02009084 ctx.idx = 0;
9085 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009086 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 +02009087 cnt++;
9088
Willy Tarreauf853c462012-04-23 18:53:56 +02009089 smp->type = SMP_T_UINT;
9090 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009091 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009092 return 1;
9093}
9094
Willy Tarreau185b5c42012-04-26 15:11:51 +02009095/* Fetch an HTTP header's integer value. The integer value is returned. It
9096 * takes a mandatory argument of type string and an optional one of type int
9097 * to designate a specific occurrence. It returns an unsigned integer, which
9098 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009099 */
9100static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009101smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009102 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009103{
Willy Tarreauef38c392013-07-22 16:29:32 +02009104 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009105
Willy Tarreauf853c462012-04-23 18:53:56 +02009106 if (ret > 0) {
9107 smp->type = SMP_T_UINT;
9108 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9109 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009110
Willy Tarreaud53e2422012-04-16 17:21:11 +02009111 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009112}
9113
Cyril Bonté69fa9922012-10-25 00:01:06 +02009114/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9115 * and an optional one of type int to designate a specific occurrence.
9116 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009117 */
9118static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009119smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009120 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009121{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009122 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009123
Willy Tarreauef38c392013-07-22 16:29:32 +02009124 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009125 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9126 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009127 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009128 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009129 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009130 if (smp->data.str.len < temp->size - 1) {
9131 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9132 temp->str[smp->data.str.len] = '\0';
9133 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9134 smp->type = SMP_T_IPV6;
9135 break;
9136 }
9137 }
9138 }
9139
Willy Tarreaud53e2422012-04-16 17:21:11 +02009140 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009141 if (!(smp->flags & SMP_F_NOT_LAST))
9142 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009143 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009144 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009145}
9146
Willy Tarreau737b0c12007-06-10 21:28:46 +02009147/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9148 * the first '/' after the possible hostname, and ends before the possible '?'.
9149 */
9150static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009151smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009152 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009153{
9154 struct http_txn *txn = l7;
9155 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009156
Willy Tarreauc0239e02012-04-16 14:42:55 +02009157 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009158
Willy Tarreau9b28e032012-10-12 23:49:43 +02009159 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009160 ptr = http_get_path(txn);
9161 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009162 return 0;
9163
9164 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009165 smp->type = SMP_T_CSTR;
9166 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009167
9168 while (ptr < end && *ptr != '?')
9169 ptr++;
9170
Willy Tarreauf853c462012-04-23 18:53:56 +02009171 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009172 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009173 return 1;
9174}
9175
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009176/* This produces a concatenation of the first occurrence of the Host header
9177 * followed by the path component if it begins with a slash ('/'). This means
9178 * that '*' will not be added, resulting in exactly the first Host entry.
9179 * If no Host header is found, then the path is returned as-is. The returned
9180 * value is stored in the trash so it does not need to be marked constant.
9181 */
9182static int
9183smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009184 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009185{
9186 struct http_txn *txn = l7;
9187 char *ptr, *end, *beg;
9188 struct hdr_ctx ctx;
9189
9190 CHECK_HTTP_MESSAGE_FIRST();
9191
9192 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009193 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 +02009194 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009195 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009196
9197 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009198 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009199 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009200 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009201 smp->data.str.len = ctx.vlen;
9202
9203 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009204 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 +02009205 beg = http_get_path(txn);
9206 if (!beg)
9207 beg = end;
9208
9209 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9210
9211 if (beg < ptr && *beg == '/') {
9212 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9213 smp->data.str.len += ptr - beg;
9214 }
9215
9216 smp->flags = SMP_F_VOL_1ST;
9217 return 1;
9218}
9219
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009220/* This produces a 32-bit hash of the concatenation of the first occurrence of
9221 * the Host header followed by the path component if it begins with a slash ('/').
9222 * This means that '*' will not be added, resulting in exactly the first Host
9223 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009224 * is hashed using the path hash followed by a full avalanche hash and provides a
9225 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009226 * high-traffic sites without having to store whole paths.
9227 */
9228static int
9229smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009230 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009231{
9232 struct http_txn *txn = l7;
9233 struct hdr_ctx ctx;
9234 unsigned int hash = 0;
9235 char *ptr, *beg, *end;
9236 int len;
9237
9238 CHECK_HTTP_MESSAGE_FIRST();
9239
9240 ctx.idx = 0;
9241 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9242 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9243 ptr = ctx.line + ctx.val;
9244 len = ctx.vlen;
9245 while (len--)
9246 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9247 }
9248
9249 /* now retrieve the path */
9250 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9251 beg = http_get_path(txn);
9252 if (!beg)
9253 beg = end;
9254
9255 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9256
9257 if (beg < ptr && *beg == '/') {
9258 while (beg < ptr)
9259 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9260 }
9261 hash = full_hash(hash);
9262
9263 smp->type = SMP_T_UINT;
9264 smp->data.uint = hash;
9265 smp->flags = SMP_F_VOL_1ST;
9266 return 1;
9267}
9268
Willy Tarreau4a550602012-12-09 14:53:32 +01009269/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009270 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9271 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9272 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009273 * that in environments where IPv6 is insignificant, truncating the output to
9274 * 8 bytes would still work.
9275 */
9276static int
9277smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009278 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009279{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009280 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009281 struct connection *cli_conn = objt_conn(l4->si[0].end);
9282
9283 if (!cli_conn)
9284 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009285
Willy Tarreauef38c392013-07-22 16:29:32 +02009286 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009287 return 0;
9288
Willy Tarreau47ca5452012-12-23 20:22:19 +01009289 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009290 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9291 temp->len += sizeof(smp->data.uint);
9292
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009293 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009294 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009295 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009296 temp->len += 4;
9297 break;
9298 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009299 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009300 temp->len += 16;
9301 break;
9302 default:
9303 return 0;
9304 }
9305
9306 smp->data.str = *temp;
9307 smp->type = SMP_T_BIN;
9308 return 1;
9309}
9310
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009311static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009312smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009313 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009314{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009315 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9316 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9317 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009318
Willy Tarreau24e32d82012-04-23 23:55:44 +02009319 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009320
Willy Tarreauf853c462012-04-23 18:53:56 +02009321 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009322 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009323 return 1;
9324}
9325
Willy Tarreau7f18e522010-10-22 20:04:13 +02009326/* return a valid test if the current request is the first one on the connection */
9327static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009328smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009329 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009330{
9331 if (!s)
9332 return 0;
9333
Willy Tarreauf853c462012-04-23 18:53:56 +02009334 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009335 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009336 return 1;
9337}
9338
Willy Tarreau34db1082012-04-19 17:16:54 +02009339/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009340static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009341smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009342 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009343{
9344
Willy Tarreau24e32d82012-04-23 23:55:44 +02009345 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009346 return 0;
9347
Willy Tarreauc0239e02012-04-16 14:42:55 +02009348 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009349
Willy Tarreauc0239e02012-04-16 14:42:55 +02009350 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009351 return 0;
9352
Willy Tarreauf853c462012-04-23 18:53:56 +02009353 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009354 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 +01009355 return 1;
9356}
Willy Tarreau8797c062007-05-07 00:55:35 +02009357
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009358/* Accepts exactly 1 argument of type userlist */
9359static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009360smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009361 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009362{
9363
9364 if (!args || args->type != ARGT_USR)
9365 return 0;
9366
9367 CHECK_HTTP_MESSAGE_FIRST();
9368
9369 if (!get_http_auth(l4))
9370 return 0;
9371
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009372 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009373 smp->ctx.a[0] = args->data.usr; /* user list */
9374 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9375 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9376
9377 /* if the user does not belong to the userlist or has a wrong password,
9378 * report that it unconditionally does not match. Otherwise we return
9379 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009380 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009381 */
9382 smp->type = SMP_T_BOOL;
9383 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9384 if (smp->data.uint)
9385 smp->type = SMP_T_UINT;
9386
9387 return 1;
9388}
9389
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009390/* Try to find the next occurrence of a cookie name in a cookie header value.
9391 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9392 * the cookie value is returned into *value and *value_l, and the function
9393 * returns a pointer to the next pointer to search from if the value was found.
9394 * Otherwise if the cookie was not found, NULL is returned and neither value
9395 * nor value_l are touched. The input <hdr> string should first point to the
9396 * header's value, and the <hdr_end> pointer must point to the first character
9397 * not part of the value. <list> must be non-zero if value may represent a list
9398 * of values (cookie headers). This makes it faster to abort parsing when no
9399 * list is expected.
9400 */
9401static char *
9402extract_cookie_value(char *hdr, const char *hdr_end,
9403 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009404 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009405{
9406 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9407 char *next;
9408
9409 /* we search at least a cookie name followed by an equal, and more
9410 * generally something like this :
9411 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9412 */
9413 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9414 /* Iterate through all cookies on this line */
9415
9416 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9417 att_beg++;
9418
9419 /* find att_end : this is the first character after the last non
9420 * space before the equal. It may be equal to hdr_end.
9421 */
9422 equal = att_end = att_beg;
9423
9424 while (equal < hdr_end) {
9425 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9426 break;
9427 if (http_is_spht[(unsigned char)*equal++])
9428 continue;
9429 att_end = equal;
9430 }
9431
9432 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9433 * is between <att_beg> and <equal>, both may be identical.
9434 */
9435
9436 /* look for end of cookie if there is an equal sign */
9437 if (equal < hdr_end && *equal == '=') {
9438 /* look for the beginning of the value */
9439 val_beg = equal + 1;
9440 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9441 val_beg++;
9442
9443 /* find the end of the value, respecting quotes */
9444 next = find_cookie_value_end(val_beg, hdr_end);
9445
9446 /* make val_end point to the first white space or delimitor after the value */
9447 val_end = next;
9448 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9449 val_end--;
9450 } else {
9451 val_beg = val_end = next = equal;
9452 }
9453
9454 /* We have nothing to do with attributes beginning with '$'. However,
9455 * they will automatically be removed if a header before them is removed,
9456 * since they're supposed to be linked together.
9457 */
9458 if (*att_beg == '$')
9459 continue;
9460
9461 /* Ignore cookies with no equal sign */
9462 if (equal == next)
9463 continue;
9464
9465 /* Now we have the cookie name between att_beg and att_end, and
9466 * its value between val_beg and val_end.
9467 */
9468
9469 if (att_end - att_beg == cookie_name_l &&
9470 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9471 /* let's return this value and indicate where to go on from */
9472 *value = val_beg;
9473 *value_l = val_end - val_beg;
9474 return next + 1;
9475 }
9476
9477 /* Set-Cookie headers only have the name in the first attr=value part */
9478 if (!list)
9479 break;
9480 }
9481
9482 return NULL;
9483}
9484
Willy Tarreaue333ec92012-04-16 16:26:40 +02009485/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009486 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009487 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009488 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009489 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009490 * Accepts exactly 1 argument of type string. If the input options indicate
9491 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009492 */
9493static int
Willy Tarreau51539362012-05-08 12:46:28 +02009494smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009495 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009496{
9497 struct http_txn *txn = l7;
9498 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009499 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009500 const struct http_msg *msg;
9501 const char *hdr_name;
9502 int hdr_name_len;
9503 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009504 int occ = 0;
9505 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009506
Willy Tarreau24e32d82012-04-23 23:55:44 +02009507 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009508 return 0;
9509
Willy Tarreaua890d072013-04-02 12:01:06 +02009510 if (!ctx) {
9511 /* first call */
9512 ctx = &static_hdr_ctx;
9513 ctx->idx = 0;
9514 smp->ctx.a[2] = ctx;
9515 }
9516
Willy Tarreaue333ec92012-04-16 16:26:40 +02009517 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009518
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009519 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009520 msg = &txn->req;
9521 hdr_name = "Cookie";
9522 hdr_name_len = 6;
9523 } else {
9524 msg = &txn->rsp;
9525 hdr_name = "Set-Cookie";
9526 hdr_name_len = 10;
9527 }
9528
Willy Tarreau28376d62012-04-26 21:26:10 +02009529 if (!occ && !(opt & SMP_OPT_ITERATE))
9530 /* no explicit occurrence and single fetch => last cookie by default */
9531 occ = -1;
9532
9533 /* OK so basically here, either we want only one value and it's the
9534 * last one, or we want to iterate over all of them and we fetch the
9535 * next one.
9536 */
9537
Willy Tarreau9b28e032012-10-12 23:49:43 +02009538 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009539 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009540 /* search for the header from the beginning, we must first initialize
9541 * the search parameters.
9542 */
Willy Tarreau37406352012-04-23 16:16:37 +02009543 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009544 ctx->idx = 0;
9545 }
9546
Willy Tarreau28376d62012-04-26 21:26:10 +02009547 smp->flags |= SMP_F_VOL_HDR;
9548
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009549 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009550 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9551 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009552 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9553 goto out;
9554
Willy Tarreau24e32d82012-04-23 23:55:44 +02009555 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009556 continue;
9557
Willy Tarreau37406352012-04-23 16:16:37 +02009558 smp->ctx.a[0] = ctx->line + ctx->val;
9559 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009560 }
9561
Willy Tarreauf853c462012-04-23 18:53:56 +02009562 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009563 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009564 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009565 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009566 &smp->data.str.str,
9567 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009568 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009569 found = 1;
9570 if (occ >= 0) {
9571 /* one value was returned into smp->data.str.{str,len} */
9572 smp->flags |= SMP_F_NOT_LAST;
9573 return 1;
9574 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009575 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009576 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009577 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009578 /* all cookie headers and values were scanned. If we're looking for the
9579 * last occurrence, we may return it now.
9580 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009581 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009582 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009583 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009584}
9585
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009586/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009587 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009588 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009589 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009590 */
9591static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009592smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009593 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009594{
9595 struct http_txn *txn = l7;
9596 struct hdr_idx *idx = &txn->hdr_idx;
9597 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009598 const struct http_msg *msg;
9599 const char *hdr_name;
9600 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009601 int cnt;
9602 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009603 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009604
Willy Tarreau24e32d82012-04-23 23:55:44 +02009605 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009606 return 0;
9607
Willy Tarreaue333ec92012-04-16 16:26:40 +02009608 CHECK_HTTP_MESSAGE_FIRST();
9609
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009610 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009611 msg = &txn->req;
9612 hdr_name = "Cookie";
9613 hdr_name_len = 6;
9614 } else {
9615 msg = &txn->rsp;
9616 hdr_name = "Set-Cookie";
9617 hdr_name_len = 10;
9618 }
9619
Willy Tarreau9b28e032012-10-12 23:49:43 +02009620 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009621 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009622 ctx.idx = 0;
9623 cnt = 0;
9624
9625 while (1) {
9626 /* Note: val_beg == NULL every time we need to fetch a new header */
9627 if (!val_beg) {
9628 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9629 break;
9630
Willy Tarreau24e32d82012-04-23 23:55:44 +02009631 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009632 continue;
9633
9634 val_beg = ctx.line + ctx.val;
9635 val_end = val_beg + ctx.vlen;
9636 }
9637
Willy Tarreauf853c462012-04-23 18:53:56 +02009638 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009639 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009640 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009641 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009642 &smp->data.str.str,
9643 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009644 cnt++;
9645 }
9646 }
9647
Willy Tarreauf853c462012-04-23 18:53:56 +02009648 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009649 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009650 return 1;
9651}
9652
Willy Tarreau51539362012-05-08 12:46:28 +02009653/* Fetch an cookie's integer value. The integer value is returned. It
9654 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9655 */
9656static int
9657smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009658 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009659{
Willy Tarreauef38c392013-07-22 16:29:32 +02009660 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009661
9662 if (ret > 0) {
9663 smp->type = SMP_T_UINT;
9664 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9665 }
9666
9667 return ret;
9668}
9669
Willy Tarreau8797c062007-05-07 00:55:35 +02009670/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009671/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009672/************************************************************************/
9673
David Cournapeau16023ee2010-12-23 20:55:41 +09009674/*
9675 * Given a path string and its length, find the position of beginning of the
9676 * query string. Returns NULL if no query string is found in the path.
9677 *
9678 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9679 *
9680 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9681 */
bedis4c75cca2012-10-05 08:38:24 +02009682static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009683{
9684 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009685
bedis4c75cca2012-10-05 08:38:24 +02009686 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009687 return p ? p + 1 : NULL;
9688}
9689
bedis4c75cca2012-10-05 08:38:24 +02009690static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009691{
bedis4c75cca2012-10-05 08:38:24 +02009692 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009693}
9694
9695/*
9696 * Given a url parameter, find the starting position of the first occurence,
9697 * or NULL if the parameter is not found.
9698 *
9699 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9700 * the function will return query_string+8.
9701 */
9702static char*
9703find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009704 char* url_param_name, size_t url_param_name_l,
9705 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009706{
9707 char *pos, *last;
9708
9709 pos = query_string;
9710 last = query_string + query_string_l - url_param_name_l - 1;
9711
9712 while (pos <= last) {
9713 if (pos[url_param_name_l] == '=') {
9714 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9715 return pos;
9716 pos += url_param_name_l + 1;
9717 }
bedis4c75cca2012-10-05 08:38:24 +02009718 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009719 pos++;
9720 pos++;
9721 }
9722 return NULL;
9723}
9724
9725/*
9726 * Given a url parameter name, returns its value and size into *value and
9727 * *value_l respectively, and returns non-zero. If the parameter is not found,
9728 * zero is returned and value/value_l are not touched.
9729 */
9730static int
9731find_url_param_value(char* path, size_t path_l,
9732 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009733 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009734{
9735 char *query_string, *qs_end;
9736 char *arg_start;
9737 char *value_start, *value_end;
9738
bedis4c75cca2012-10-05 08:38:24 +02009739 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009740 if (!query_string)
9741 return 0;
9742
9743 qs_end = path + path_l;
9744 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009745 url_param_name, url_param_name_l,
9746 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009747 if (!arg_start)
9748 return 0;
9749
9750 value_start = arg_start + url_param_name_l + 1;
9751 value_end = value_start;
9752
bedis4c75cca2012-10-05 08:38:24 +02009753 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009754 value_end++;
9755
9756 *value = value_start;
9757 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009758 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009759}
9760
9761static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009762smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009763 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009764{
bedis4c75cca2012-10-05 08:38:24 +02009765 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009766 struct http_txn *txn = l7;
9767 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009768
bedis4c75cca2012-10-05 08:38:24 +02009769 if (!args || args[0].type != ARGT_STR ||
9770 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009771 return 0;
9772
9773 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009774
bedis4c75cca2012-10-05 08:38:24 +02009775 if (args[1].type)
9776 delim = *args[1].data.str.str;
9777
Willy Tarreau9b28e032012-10-12 23:49:43 +02009778 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009779 args->data.str.str, args->data.str.len,
9780 &smp->data.str.str, &smp->data.str.len,
9781 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009782 return 0;
9783
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009784 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009785 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009786 return 1;
9787}
9788
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009789/* Return the signed integer value for the specified url parameter (see url_param
9790 * above).
9791 */
9792static int
9793smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009794 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009795{
Willy Tarreauef38c392013-07-22 16:29:32 +02009796 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009797
9798 if (ret > 0) {
9799 smp->type = SMP_T_UINT;
9800 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9801 }
9802
9803 return ret;
9804}
9805
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009806/* This produces a 32-bit hash of the concatenation of the first occurrence of
9807 * the Host header followed by the path component if it begins with a slash ('/').
9808 * This means that '*' will not be added, resulting in exactly the first Host
9809 * entry. If no Host header is found, then the path is used. The resulting value
9810 * is hashed using the url hash followed by a full avalanche hash and provides a
9811 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9812 * high-traffic sites without having to store whole paths.
9813 * this differs from the base32 functions in that it includes the url parameters
9814 * as well as the path
9815 */
9816static int
9817smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009818 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009819{
9820 struct http_txn *txn = l7;
9821 struct hdr_ctx ctx;
9822 unsigned int hash = 0;
9823 char *ptr, *beg, *end;
9824 int len;
9825
9826 CHECK_HTTP_MESSAGE_FIRST();
9827
9828 ctx.idx = 0;
9829 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9830 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9831 ptr = ctx.line + ctx.val;
9832 len = ctx.vlen;
9833 while (len--)
9834 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9835 }
9836
9837 /* now retrieve the path */
9838 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9839 beg = http_get_path(txn);
9840 if (!beg)
9841 beg = end;
9842
9843 for (ptr = beg; ptr < end ; ptr++);
9844
9845 if (beg < ptr && *beg == '/') {
9846 while (beg < ptr)
9847 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9848 }
9849 hash = full_hash(hash);
9850
9851 smp->type = SMP_T_UINT;
9852 smp->data.uint = hash;
9853 smp->flags = SMP_F_VOL_1ST;
9854 return 1;
9855}
9856
9857/* This concatenates the source address with the 32-bit hash of the Host and
9858 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9859 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9860 * on the source address length. The URL hash is stored before the address so
9861 * that in environments where IPv6 is insignificant, truncating the output to
9862 * 8 bytes would still work.
9863 */
9864static int
9865smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009866 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009867{
9868 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009869 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009870
Willy Tarreaue155ec22013-11-18 18:33:22 +01009871 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009872 return 0;
9873
9874 temp = get_trash_chunk();
9875 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9876 temp->len += sizeof(smp->data.uint);
9877
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009878 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009879 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009880 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009881 temp->len += 4;
9882 break;
9883 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009884 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009885 temp->len += 16;
9886 break;
9887 default:
9888 return 0;
9889 }
9890
9891 smp->data.str = *temp;
9892 smp->type = SMP_T_BIN;
9893 return 1;
9894}
9895
Willy Tarreau185b5c42012-04-26 15:11:51 +02009896/* This function is used to validate the arguments passed to any "hdr" fetch
9897 * keyword. These keywords support an optional positive or negative occurrence
9898 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9899 * is assumed that the types are already the correct ones. Returns 0 on error,
9900 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9901 * error message in case of error, that the caller is responsible for freeing.
9902 * The initial location must either be freeable or NULL.
9903 */
9904static int val_hdr(struct arg *arg, char **err_msg)
9905{
9906 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009907 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009908 return 0;
9909 }
9910 return 1;
9911}
9912
Willy Tarreau276fae92013-07-25 14:36:01 +02009913/* takes an UINT value on input supposed to represent the time since EPOCH,
9914 * adds an optional offset found in args[0] and emits a string representing
9915 * the date in RFC-1123/5322 format.
9916 */
9917static int sample_conv_http_date(const struct arg *args, struct sample *smp)
9918{
9919 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
9920 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
9921 struct chunk *temp;
9922 struct tm *tm;
9923 time_t curr_date = smp->data.uint;
9924
9925 /* add offset */
9926 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
9927 curr_date += args[0].data.sint;
9928
9929 tm = gmtime(&curr_date);
9930
9931 temp = get_trash_chunk();
9932 temp->len = snprintf(temp->str, temp->size - temp->len,
9933 "%s, %02d %s %04d %02d:%02d:%02d GMT",
9934 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
9935 tm->tm_hour, tm->tm_min, tm->tm_sec);
9936
9937 smp->data.str = *temp;
9938 smp->type = SMP_T_STR;
9939 return 1;
9940}
9941
Willy Tarreau4a568972010-05-12 08:08:50 +02009942/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009943/* All supported ACL keywords must be declared here. */
9944/************************************************************************/
9945
9946/* Note: must not be declared <const> as its list will be overwritten.
9947 * Please take care of keeping this list alphabetically sorted.
9948 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009949static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009950 { "base", "base", pat_parse_str, pat_match_str },
9951 { "base_beg", "base", pat_parse_str, pat_match_beg },
9952 { "base_dir", "base", pat_parse_str, pat_match_dir },
9953 { "base_dom", "base", pat_parse_str, pat_match_dom },
9954 { "base_end", "base", pat_parse_str, pat_match_end },
9955 { "base_len", "base", pat_parse_int, pat_match_len },
9956 { "base_reg", "base", pat_parse_reg, pat_match_reg },
9957 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009958
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009959 { "cook", "req.cook", pat_parse_str, pat_match_str },
9960 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
9961 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
9962 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
9963 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
9964 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
9965 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
9966 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009967
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009968 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
9969 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
9970 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
9971 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
9972 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
9973 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
9974 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
9975 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009976
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009977 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009978
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009979 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009980
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009981 { "path", "path", pat_parse_str, pat_match_str },
9982 { "path_beg", "path", pat_parse_str, pat_match_beg },
9983 { "path_dir", "path", pat_parse_str, pat_match_dir },
9984 { "path_dom", "path", pat_parse_str, pat_match_dom },
9985 { "path_end", "path", pat_parse_str, pat_match_end },
9986 { "path_len", "path", pat_parse_int, pat_match_len },
9987 { "path_reg", "path", pat_parse_reg, pat_match_reg },
9988 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009989
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009990 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
9991 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009992
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009993 { "scook", "res.cook", pat_parse_str, pat_match_str },
9994 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
9995 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
9996 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
9997 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
9998 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
9999 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10000 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010001
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010002 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10003 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10004 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10005 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10006 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10007 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10008 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10009 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010010
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010011 { "url", "url", pat_parse_str, pat_match_str },
10012 { "url_beg", "url", pat_parse_str, pat_match_beg },
10013 { "url_dir", "url", pat_parse_str, pat_match_dir },
10014 { "url_dom", "url", pat_parse_str, pat_match_dom },
10015 { "url_end", "url", pat_parse_str, pat_match_end },
10016 { "url_len", "url", pat_parse_int, pat_match_len },
10017 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10018 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010019
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010020 { "urlp", "urlp", pat_parse_str, pat_match_str },
10021 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10022 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10023 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10024 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10025 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10026 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10027 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010028
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010029 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010030}};
10031
10032/************************************************************************/
10033/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010034/************************************************************************/
10035/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010036static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010037 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10038 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10039 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10040
10041 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10042 * are only here to match the ACL's name, are request-only and are used
10043 * for ACL compatibility only.
10044 */
10045 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10046 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10047 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10048 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10049
10050 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10051 * only here to match the ACL's name, are request-only and are used for
10052 * ACL compatibility only.
10053 */
10054 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10055 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10056 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10057 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10058
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010059 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10060 { "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 +010010061 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10062 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10063 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010064
10065 /* HTTP protocol on the request path */
10066 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010067 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010068
10069 /* HTTP version on the request path */
10070 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010071 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010072
10073 /* HTTP version on the response path */
10074 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010075 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10076
Willy Tarreau18ed2562013-01-14 15:56:36 +010010077 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10078 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10079 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10080 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10081
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010082 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10083 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010084 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10085 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10086 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10087 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10088
10089 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10090 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10091 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10092 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10093
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010094 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10095 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010096 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10097 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10098 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10099 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10100
Willy Tarreau409bcde2013-01-08 00:31:00 +010010101 /* scook is valid only on the response and is used for ACL compatibility */
10102 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10103 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10104 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10105 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10106
10107 /* shdr is valid only on the response and is used for ACL compatibility */
10108 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10109 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10110 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10111 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10112
10113 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10114 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010115 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10116 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010117 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10118 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10119 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10120 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10121 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10122 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010123}};
10124
Willy Tarreau8797c062007-05-07 00:55:35 +020010125
Willy Tarreau276fae92013-07-25 14:36:01 +020010126/* Note: must not be declared <const> as its list will be overwritten */
10127static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10128 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10129 { NULL, NULL, 0, 0, 0 },
10130}};
10131
Willy Tarreau8797c062007-05-07 00:55:35 +020010132__attribute__((constructor))
10133static void __http_protocol_init(void)
10134{
10135 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010136 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010137 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010138}
10139
10140
Willy Tarreau58f10d72006-12-04 02:26:12 +010010141/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010142 * Local variables:
10143 * c-indent-level: 8
10144 * c-basic-offset: 8
10145 * End:
10146 */