blob: 41561c5457f9fdbe1ae1060d2459bbd9823636b3 [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 {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100297 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100298 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 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100770static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100771{
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 Tarreau2d400bb2012-05-14 12:11:47 +0200939 else if (err_type & SI_ET_CONN_RES)
940 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
941 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100943 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200944 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100945}
946
Willy Tarreau42250582007-04-01 01:30:43 +0200947extern const char sess_term_cond[8];
948extern const char sess_fin_state[8];
949extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200950struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100951struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100952struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100953
Willy Tarreau117f59e2007-03-04 18:17:17 +0100954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001043const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001044 enum ht_state state, const char *ptr, const char *end,
1045 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001047 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001048
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001051 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001052 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001053 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1054
1055 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001056 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001057 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1058 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001059 state = HTTP_MSG_ERROR;
1060 break;
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001063 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001065 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001066 goto http_msg_rpcode;
1067 }
1068 if (likely(HTTP_IS_SPHT(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1070 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001071 state = HTTP_MSG_ERROR;
1072 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001073
Willy Tarreau8973c702007-01-21 23:58:29 +01001074 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001075 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001076 if (likely(!HTTP_IS_LWS(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1078
1079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1082 }
1083
1084 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 http_msg_rsp_reason:
1087 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 msg->sl.st.r_l = 0;
1090 goto http_msg_rpline_eol;
1091
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001093 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001094 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001095 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 goto http_msg_rpreason;
1097 }
1098 if (likely(HTTP_IS_SPHT(*ptr)))
1099 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1100 /* so it's a CR/LF, so there is no reason phrase */
1101 goto http_msg_rsp_reason;
1102
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 if (likely(!HTTP_IS_CRLF(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001107 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001108 http_msg_rpline_eol:
1109 /* We have seen the end of line. Note that we do not
1110 * necessarily have the \n yet, but at least we know that we
1111 * have EITHER \r OR \n, otherwise the response would not be
1112 * complete. We can then record the response length and return
1113 * to the caller which will be able to register it.
1114 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001115 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 return ptr;
1117
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001119#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1121 exit(1);
1122#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001123 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 }
1125
1126 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001127 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 if (ret_state)
1129 *ret_state = state;
1130 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001131 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133}
1134
Willy Tarreau8973c702007-01-21 23:58:29 +01001135/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 * This function parses a request line between <ptr> and <end>, starting with
1137 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1138 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1139 * will give undefined results.
1140 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1141 * and that msg->sol points to the beginning of the request.
1142 * If a complete line is found (which implies that at least one CR or LF is
1143 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1144 * returned indicating an incomplete line (which does not mean that parts have
1145 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1146 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1147 * upon next call.
1148 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1151 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001152 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001154const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001155 enum ht_state state, const char *ptr, const char *end,
1156 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001158 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001159
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (likely(HTTP_IS_TOKEN(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1169 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 if (likely(HTTP_IS_CRLF(*ptr))) {
1172 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001177 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001179 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 msg->sl.rq.v_l = 0;
1181 goto http_msg_rqline_eol;
1182 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 state = HTTP_MSG_ERROR;
1184 break;
1185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001187 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001189 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 goto http_msg_rquri;
1191 }
1192 if (likely(HTTP_IS_SPHT(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1194 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1195 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001198 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001199 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001203 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001206
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 /* non-ASCII chars are forbidden unless option
1209 * accept-invalid-http-request is enabled in the frontend.
1210 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001211 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001212 if (msg->err_pos < -1)
1213 goto invalid_char;
1214 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001215 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001216 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1217 }
1218
1219 if (likely(HTTP_IS_CRLF(*ptr))) {
1220 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1221 goto http_msg_req09_uri_e;
1222 }
1223
1224 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001225 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001226 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001227 state = HTTP_MSG_ERROR;
1228 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001231 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001233 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 goto http_msg_rqver;
1235 }
1236 if (likely(HTTP_IS_SPHT(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1238 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1239 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001242 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001243 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001245
1246 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001248 http_msg_rqline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the request would not be
1252 * complete. We can then record the request length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001256 return ptr;
1257 }
1258
1259 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001260 state = HTTP_MSG_ERROR;
1261 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001264#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1266 exit(1);
1267#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001268 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001272 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 if (ret_state)
1274 *ret_state = state;
1275 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001276 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001280/*
1281 * Returns the data from Authorization header. Function may be called more
1282 * than once so data is stored in txn->auth_data. When no header is found
1283 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1284 * searching again for something we are unable to find anyway.
1285 */
1286
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001287char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001288
1289int
1290get_http_auth(struct session *s)
1291{
1292
1293 struct http_txn *txn = &s->txn;
1294 struct chunk auth_method;
1295 struct hdr_ctx ctx;
1296 char *h, *p;
1297 int len;
1298
1299#ifdef DEBUG_AUTH
1300 printf("Auth for session %p: %d\n", s, txn->auth.method);
1301#endif
1302
1303 if (txn->auth.method == HTTP_AUTH_WRONG)
1304 return 0;
1305
1306 if (txn->auth.method)
1307 return 1;
1308
1309 txn->auth.method = HTTP_AUTH_WRONG;
1310
1311 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001312
1313 if (txn->flags & TX_USE_PX_CONN) {
1314 h = "Proxy-Authorization";
1315 len = strlen(h);
1316 } else {
1317 h = "Authorization";
1318 len = strlen(h);
1319 }
1320
Willy Tarreau9b28e032012-10-12 23:49:43 +02001321 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001322 return 0;
1323
1324 h = ctx.line + ctx.val;
1325
1326 p = memchr(h, ' ', ctx.vlen);
1327 if (!p || p == h)
1328 return 0;
1329
1330 chunk_initlen(&auth_method, h, 0, p-h);
1331 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1332
1333 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1334
1335 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001336 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001337
1338 if (len < 0)
1339 return 0;
1340
1341
1342 get_http_auth_buff[len] = '\0';
1343
1344 p = strchr(get_http_auth_buff, ':');
1345
1346 if (!p)
1347 return 0;
1348
1349 txn->auth.user = get_http_auth_buff;
1350 *p = '\0';
1351 txn->auth.pass = p+1;
1352
1353 txn->auth.method = HTTP_AUTH_BASIC;
1354 return 1;
1355 }
1356
1357 return 0;
1358}
1359
Willy Tarreau58f10d72006-12-04 02:26:12 +01001360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361/*
1362 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001363 * depending on the initial msg->msg_state. The caller is responsible for
1364 * ensuring that the message does not wrap. The function can be preempted
1365 * everywhere when data are missing and recalled at the exact same location
1366 * with no information loss. The message may even be realigned between two
1367 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001368 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001369 * fields. Note that msg->sol will be initialized after completing the first
1370 * state, so that none of the msg pointers has to be initialized prior to the
1371 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001373void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374{
Willy Tarreau3770f232013-12-07 00:01:53 +01001375 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001379 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001380 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 ptr = buf->p + msg->next;
1382 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (unlikely(ptr >= end))
1385 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 /*
1389 * First, states that are specific to the response only.
1390 * We check them first so that request and headers are
1391 * closer to each other (accessed more often).
1392 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001396 /* we have a start of message, but we have to check
1397 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001398 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001399 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001400 if (unlikely(ptr != buf->p)) {
1401 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001402 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001403 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001404 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 }
Willy Tarreau26927362012-05-18 23:22:52 +02001406 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001407 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 hdr_idx_init(idx);
1409 state = HTTP_MSG_RPVER;
1410 goto http_msg_rpver;
1411 }
1412
1413 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1414 goto http_msg_invalid;
1415
1416 if (unlikely(*ptr == '\n'))
1417 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1419 /* stop here */
1420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 EXPECT_LF_HERE(ptr, http_msg_invalid);
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1425 /* stop here */
1426
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001428 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 case HTTP_MSG_RPVER_SP:
1430 case HTTP_MSG_RPCODE:
1431 case HTTP_MSG_RPCODE_SP:
1432 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001433 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001434 state, ptr, end,
1435 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 if (unlikely(!ptr))
1437 return;
1438
1439 /* we have a full response and we know that we have either a CR
1440 * or an LF at <ptr>.
1441 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1443
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001444 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1447 goto http_msg_rpline_end;
1448
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001450 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 /* msg->sol must point to the first of CR or LF. */
1452 EXPECT_LF_HERE(ptr, http_msg_invalid);
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1454 /* stop here */
1455
1456 /*
1457 * Second, states that are specific to the request only
1458 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001460 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001462 /* we have a start of message, but we have to check
1463 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001464 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001465 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 if (likely(ptr != buf->p)) {
1467 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001468 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001469 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau26927362012-05-18 23:22:52 +02001472 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001473 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 state = HTTP_MSG_RQMETH;
1475 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1479 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 if (unlikely(*ptr == '\n'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 case HTTP_MSG_RQMETH_SP:
1495 case HTTP_MSG_RQURI:
1496 case HTTP_MSG_RQURI_SP:
1497 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001498 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001499 state, ptr, end,
1500 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(!ptr))
1502 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 /* we have a full request and we know that we have either a CR
1505 * or an LF at <ptr>.
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001509 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (likely(*ptr == '\r'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 /* check for HTTP/0.9 request : no version information available.
1517 * msg->sol must point to the first of CR or LF.
1518 */
1519 if (unlikely(msg->sl.rq.v_l == 0))
1520 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 /*
1527 * Common states below
1528 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001530 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001531 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (likely(!HTTP_IS_CRLF(*ptr))) {
1533 goto http_msg_hdr_name;
1534 }
1535
1536 if (likely(*ptr == '\r'))
1537 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1538 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 /* assumes msg->sol points to the first char */
1543 if (likely(HTTP_IS_TOKEN(*ptr)))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001546 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549 if (likely(msg->err_pos < -1) || *ptr == '\n')
1550 goto http_msg_invalid;
1551
1552 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001553 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001554
1555 /* and we still accept this non-token character */
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001559 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001560 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(HTTP_IS_SPHT(*ptr)))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001565 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(!HTTP_IS_CRLF(*ptr))) {
1568 goto http_msg_hdr_val;
1569 }
1570
1571 if (likely(*ptr == '\r'))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1573 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001581 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 if (likely(HTTP_IS_SPHT(*ptr))) {
1583 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 for (; buf->p + msg->sov < ptr; msg->sov++)
1585 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_hdr_l1_sp;
1587 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001588 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001589 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_complete_header;
1591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001594 /* assumes msg->sol points to the first char, and msg->sov
1595 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 */
1597 if (likely(!HTTP_IS_CRLF(*ptr)))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001600 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 /* Note: we could also copy eol into ->eoh so that we have the
1602 * real header end in case it ends with lots of LWS, but is this
1603 * really needed ?
1604 */
1605 if (likely(*ptr == '\r'))
1606 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1607 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1617 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001618 for (; buf->p + msg->eol < ptr; msg->eol++)
1619 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 goto http_msg_hdr_val;
1621 }
1622 http_msg_complete_header:
1623 /*
1624 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 * Assumes msg->sol points to the first char, msg->sov points
1626 * to the first character of the value and msg->eol to the
1627 * first CR or LF so we know how the line ends. We insert last
1628 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 idx, idx->tail) < 0))
1632 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001633
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001634 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (likely(!HTTP_IS_CRLF(*ptr))) {
1636 goto http_msg_hdr_name;
1637 }
1638
1639 if (likely(*ptr == '\r'))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1641 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 /* Assumes msg->sol points to the first of either CR or LF */
1646 EXPECT_LF_HERE(ptr, http_msg_invalid);
1647 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001649 msg->eoh = msg->sol;
1650 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001651 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001653
1654 case HTTP_MSG_ERROR:
1655 /* this may only happen if we call http_msg_analyser() twice with an error */
1656 break;
1657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001659#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1661 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001662#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001663 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 }
1665 http_msg_ood:
1666 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_invalid:
1672 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001674 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return;
1676}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001677
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001678/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1679 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1680 * nothing is done and 1 is returned.
1681 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001682static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001683{
1684 int delta;
1685 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001686 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001687
1688 if (msg->sl.rq.v_l != 0)
1689 return 1;
1690
Willy Tarreau9b28e032012-10-12 23:49:43 +02001691 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692 delta = 0;
1693
1694 if (msg->sl.rq.u_l == 0) {
1695 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001698 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 }
1700 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001702 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001703 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001704 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001705 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001706 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001707 NULL, NULL);
1708 if (unlikely(!cur_end))
1709 return 0;
1710
1711 /* we have a full HTTP/1.0 request now and we know that
1712 * we have either a CR or an LF at <ptr>.
1713 */
1714 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1715 return 1;
1716}
1717
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 * and "keep-alive" values. If we already know that some headers may safely
1720 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1722 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001723 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001724 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1725 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1726 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001727 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001728 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730{
Willy Tarreau5b154472009-12-21 20:11:07 +01001731 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 const char *hdr_val = "Connection";
1733 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001734
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001735 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001736 return;
1737
Willy Tarreau88d349d2010-01-25 12:15:43 +01001738 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1739 hdr_val = "Proxy-Connection";
1740 hdr_len = 16;
1741 }
1742
Willy Tarreau5b154472009-12-21 20:11:07 +01001743 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001745 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001746 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1747 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001748 if (to_del & 2)
1749 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001750 else
1751 txn->flags |= TX_CON_KAL_SET;
1752 }
1753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1754 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001755 if (to_del & 1)
1756 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001757 else
1758 txn->flags |= TX_CON_CLO_SET;
1759 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001760 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1761 txn->flags |= TX_HDR_CONN_UPG;
1762 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001763 }
1764
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001765 txn->flags |= TX_HDR_CONN_PRS;
1766 return;
1767}
Willy Tarreau5b154472009-12-21 20:11:07 +01001768
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769/* Apply desired changes on the Connection: header. Values may be removed and/or
1770 * added depending on the <wanted> flags, which are exclusively composed of
1771 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1772 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1773 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001774void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001775{
1776 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777 const char *hdr_val = "Connection";
1778 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001779
1780 ctx.idx = 0;
1781
Willy Tarreau88d349d2010-01-25 12:15:43 +01001782
1783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1784 hdr_val = "Proxy-Connection";
1785 hdr_len = 16;
1786 }
1787
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001788 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001789 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1791 if (wanted & TX_CON_KAL_SET)
1792 txn->flags |= TX_CON_KAL_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001795 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001796 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1797 if (wanted & TX_CON_CLO_SET)
1798 txn->flags |= TX_CON_CLO_SET;
1799 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001801 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803
1804 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1805 return;
1806
1807 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1808 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 hdr_val = "Connection: close";
1810 hdr_len = 17;
1811 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1812 hdr_val = "Proxy-Connection: close";
1813 hdr_len = 23;
1814 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001815 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 }
1817
1818 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1819 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001820 hdr_val = "Connection: keep-alive";
1821 hdr_len = 22;
1822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection: keep-alive";
1824 hdr_len = 28;
1825 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001826 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001827 }
1828 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829}
1830
Willy Tarreaua458b672012-03-05 11:17:50 +01001831/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001833 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001834 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001837static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001838{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001839 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001841 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001842 const char *end = buf->data + buf->size;
1843 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001844 unsigned int chunk = 0;
1845
1846 /* The chunk size is in the following form, though we are only
1847 * interested in the size and CRLF :
1848 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1849 */
1850 while (1) {
1851 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001852 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 if (c < 0) /* not a hex digit anymore */
1856 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001857 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001858 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001859 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001861 chunk = (chunk << 4) + c;
1862 }
1863
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001866 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 while (http_is_spht[(unsigned char)*ptr]) {
1869 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001870 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001871 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001873 }
1874
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 /* Up to there, we know that at least one byte is present at *ptr. Check
1876 * for the end of chunk size.
1877 */
1878 while (1) {
1879 if (likely(HTTP_IS_CRLF(*ptr))) {
1880 /* we now have a CR or an LF at ptr */
1881 if (likely(*ptr == '\r')) {
1882 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001884 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 return 0;
1886 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001887
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001889 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 /* done */
1893 break;
1894 }
1895 else if (*ptr == ';') {
1896 /* chunk extension, ends at next CRLF */
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 Tarreau115acb92009-12-26 13:56:06 +01001900 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
1902 while (!HTTP_IS_CRLF(*ptr)) {
1903 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001905 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 return 0;
1907 }
1908 /* we have a CRLF now, loop above */
1909 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001912 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
1914
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001919 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001921 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001923 msg->chunk_len = chunk;
1924 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001927 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001929 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001930}
1931
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 * the trailers is found, it is automatically scheduled to be forwarded,
1935 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1936 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001937 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001938 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001940 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1941 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001942 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001943 * matches the length of trailers already parsed and not forwarded. It is also
1944 * important to note that this function is designed to be able to parse wrapped
1945 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001947static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001949 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001950
Willy Tarreaua458b672012-03-05 11:17:50 +01001951 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001953 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 const char *ptr = b_ptr(buf, msg->next);
1955 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001956 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 /* scan current line and stop at LF or CRLF */
1959 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001960 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 return 0;
1962
1963 if (*ptr == '\n') {
1964 if (!p1)
1965 p1 = ptr;
1966 p2 = ptr;
1967 break;
1968 }
1969
1970 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001973 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001974 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 p1 = ptr;
1976 }
1977
1978 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (ptr >= buf->data + buf->size)
1980 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 }
1982
1983 /* after LF; point to beginning of next line */
1984 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 if (p2 >= buf->data + buf->size)
1986 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001991
1992 /* schedule this line for forwarding */
1993 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 if (msg->sov >= buf->size)
1995 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001996
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001998 /* LF/CRLF at beginning of line => end of trailers at p2.
1999 * Everything was scheduled for forwarding, there's nothing
2000 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002001 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 msg->msg_state = HTTP_MSG_DONE;
2004 return 1;
2005 }
2006 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002007 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 }
2009}
2010
Willy Tarreau54d23df2012-10-25 19:04:45 +02002011/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002013 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002014 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2016 * not enough data are available, the function does not change anything and
2017 * returns zero. If a parse error is encountered, the function returns < 0 and
2018 * does not change anything. Note: this function is designed to parse wrapped
2019 * CRLF at the end of the buffer.
2020 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002021static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002022{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002023 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002024 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 int bytes;
2026
2027 /* NB: we'll check data availabilty at the end. It's not a
2028 * problem because whatever we match first will be checked
2029 * against the correct length.
2030 */
2031 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002032 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 if (*ptr == '\r') {
2034 bytes++;
2035 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 if (ptr >= buf->data + buf->size)
2037 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 }
2039
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 return 0;
2042
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002043 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047
2048 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002049 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002051 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2052 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002053 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002054 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2055 return 1;
2056}
Willy Tarreau5b154472009-12-21 20:11:07 +01002057
William Lallemand82fe75c2012-10-23 10:25:10 +02002058
2059/*
2060 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002061 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002062int select_compression_request_header(struct session *s, struct buffer *req)
2063{
2064 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002065 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002066 struct hdr_ctx ctx;
2067 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002068 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002069
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002070 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2071 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002072 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2073 */
2074 ctx.idx = 0;
2075 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2076 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002077 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2078 (ctx.vlen < 31 ||
2079 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2080 ctx.line[ctx.val + 30] < '6' ||
2081 (ctx.line[ctx.val + 30] == '6' &&
2082 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2083 s->comp_algo = NULL;
2084 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002085 }
2086
William Lallemand82fe75c2012-10-23 10:25:10 +02002087 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002088 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 +02002089 ctx.idx = 0;
2090 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002091 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2093 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002094
2095 /* remove all occurrences of the header when "compression offload" is set */
2096
2097 if ((s->be->comp && s->be->comp->offload) ||
2098 (s->fe->comp && s->fe->comp->offload)) {
2099 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2100 ctx.idx = 0;
2101 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2102 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2103 }
2104 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002105 return 1;
2106 }
2107 }
2108 }
2109 }
2110
2111 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002112 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2113 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002114 if (comp_algo->add_data == identity_add_data) {
2115 s->comp_algo = comp_algo;
2116 return 1;
2117 }
2118 }
2119 }
2120
2121 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002122 return 0;
2123}
2124
2125/*
2126 * Selects a comression algorithm depending of the server response.
2127 */
2128int select_compression_response_header(struct session *s, struct buffer *res)
2129{
2130 struct http_txn *txn = &s->txn;
2131 struct http_msg *msg = &txn->rsp;
2132 struct hdr_ctx ctx;
2133 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002134
2135 /* no common compression algorithm was found in request header */
2136 if (s->comp_algo == NULL)
2137 goto fail;
2138
2139 /* HTTP < 1.1 should not be compressed */
2140 if (!(msg->flags & HTTP_MSGF_VER_11))
2141 goto fail;
2142
William Lallemandd3002612012-11-26 14:34:47 +01002143 /* 200 only */
2144 if (txn->status != 200)
2145 goto fail;
2146
William Lallemand82fe75c2012-10-23 10:25:10 +02002147 /* Content-Length is null */
2148 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2149 goto fail;
2150
Willy Tarreau667c2a32013-04-09 08:13:58 +02002151 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2152 if (msg->flags & HTTP_MSGF_TE_CHNK)
2153 goto fail;
2154
William Lallemand82fe75c2012-10-23 10:25:10 +02002155 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002156 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002157 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2158 goto fail;
2159
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002160 /* no compression when Cache-Control: no-transform is present in the message */
2161 ctx.idx = 0;
2162 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2163 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2164 goto fail;
2165 }
2166
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 comp_type = NULL;
2168
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002169 /* we don't want to compress multipart content-types, nor content-types that are
2170 * not listed in the "compression type" directive if any. If no content-type was
2171 * found but configuration requires one, we don't compress either. Backend has
2172 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002173 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002174 ctx.idx = 0;
2175 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2176 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2177 goto fail;
2178
2179 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2180 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002181 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002182 if (ctx.vlen >= comp_type->name_len &&
2183 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 /* this Content-Type should be compressed */
2185 break;
2186 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 /* this Content-Type should not be compressed */
2188 if (comp_type == NULL)
2189 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002190 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002192 else { /* no content-type header */
2193 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2194 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002195 }
2196
William Lallemandd85f9172012-11-09 17:05:39 +01002197 /* limit compression rate */
2198 if (global.comp_rate_lim > 0)
2199 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2200 goto fail;
2201
William Lallemand072a2bf2012-11-20 17:01:01 +01002202 /* limit cpu usage */
2203 if (idle_pct < compress_min_idle)
2204 goto fail;
2205
William Lallemand4c49fae2012-11-07 15:00:23 +01002206 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002207 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002208 goto fail;
2209
William Lallemandec3e3892012-11-12 17:02:18 +01002210 s->flags |= SN_COMP_READY;
2211
William Lallemand82fe75c2012-10-23 10:25:10 +02002212 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002213 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002214 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2215 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2216
2217 /* add Transfer-Encoding header */
2218 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2219 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2220
2221 /*
2222 * Add Content-Encoding header when it's not identity encoding.
2223 * RFC 2616 : Identity encoding: This content-coding is used only in the
2224 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2225 * header.
2226 */
2227 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002228 trash.len = 18;
2229 memcpy(trash.str, "Content-Encoding: ", trash.len);
2230 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2231 trash.len += s->comp_algo->name_len;
2232 trash.str[trash.len] = '\0';
2233 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002235 return 1;
2236
2237fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002238 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002239 return 0;
2240}
2241
2242
Willy Tarreaud787e662009-07-07 10:14:51 +02002243/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2244 * processing can continue on next analysers, or zero if it either needs more
2245 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2246 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2247 * when it has nothing left to do, and may remove any analyser when it wants to
2248 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002249 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002250int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002251{
Willy Tarreau59234e92008-11-30 23:51:27 +01002252 /*
2253 * We will parse the partial (or complete) lines.
2254 * We will check the request syntax, and also join multi-line
2255 * headers. An index of all the lines will be elaborated while
2256 * parsing.
2257 *
2258 * For the parsing, we use a 28 states FSM.
2259 *
2260 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002261 * req->buf->p = beginning of request
2262 * req->buf->p + msg->eoh = end of processed headers / start of current one
2263 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002264 * msg->eol = end of current header or line (LF or CRLF)
2265 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002266 *
2267 * At end of parsing, we may perform a capture of the error (if any), and
2268 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2269 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2270 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002274 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 struct http_txn *txn = &s->txn;
2276 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002277 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002278
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002279 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 +01002280 now_ms, __FUNCTION__,
2281 s,
2282 req,
2283 req->rex, req->wex,
2284 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002286 req->analysers);
2287
Willy Tarreau52a0c602009-08-16 22:45:38 +02002288 /* we're speaking HTTP here, so let's speak HTTP to the client */
2289 s->srv_error = http_return_srv_error;
2290
Willy Tarreau83e3af02009-12-28 17:39:57 +01002291 /* There's a protected area at the end of the buffer for rewriting
2292 * purposes. We don't want to start to parse the request if the
2293 * protected area is affected, because we may have to move processed
2294 * data later, which is much more complicated.
2295 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002296 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002297 if (txn->flags & TX_NOT_FIRST) {
2298 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002299 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002300 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002301 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002302 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002303 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002304 return 0;
2305 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002306 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2307 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2308 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 }
2310
Willy Tarreau065e8332010-01-08 00:30:20 +01002311 /* Note that we have the same problem with the response ; we
2312 * may want to send a redirect, error or anything which requires
2313 * some spare space. So we'll ensure that we have at least
2314 * maxrewrite bytes available in the response buffer before
2315 * processing that one. This will only affect pipelined
2316 * keep-alive requests.
2317 */
2318 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002319 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002320 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2321 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2322 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002323 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002324 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002325 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002326 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002327 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002328 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002329 return 0;
2330 }
2331 }
2332
Willy Tarreau9b28e032012-10-12 23:49:43 +02002333 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002334 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002335 }
2336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 /* 1: we might have to print this header in debug mode */
2338 if (unlikely((global.mode & MODE_DEBUG) &&
2339 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002340 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002344 /* this is a bit complex : in case of error on the request line,
2345 * we know that rq.l is still zero, so we display only the part
2346 * up to the end of the line (truncated by debug_hdr).
2347 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002348 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 sol += hdr_idx_first_pos(&txn->hdr_idx);
2352 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002353
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 while (cur_idx) {
2355 eol = sol + txn->hdr_idx.v[cur_idx].len;
2356 debug_hdr("clihdr", s, sol, eol);
2357 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2358 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 }
2361
Willy Tarreau58f10d72006-12-04 02:26:12 +01002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /*
2364 * Now we quickly check if we have found a full valid request.
2365 * If not so, we check the FD and buffer states before leaving.
2366 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002367 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002368 * requests are checked first. When waiting for a second request
2369 * on a keep-alive session, if we encounter and error, close, t/o,
2370 * we note the error in the session flags but don't set any state.
2371 * Since the error will be noted there, it will not be counted by
2372 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002373 * Last, we may increase some tracked counters' http request errors on
2374 * the cases that are deliberately the client's fault. For instance,
2375 * a timeout or connection reset is not counted as an error. However
2376 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau655dce92009-11-08 13:10:58 +01002379 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002380 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002382 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002384 session_inc_http_req_ctr(s);
2385 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002386 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* 1: Since we are in header mode, if there's no space
2391 * left for headers, we won't be able to free more
2392 * later, so the session will never terminate. We
2393 * must terminate it now.
2394 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002395 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 /* FIXME: check if URI is set and return Status
2397 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002398 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002399 session_inc_http_req_ctr(s);
2400 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002401 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002402 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002403 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 goto return_bad_req;
2405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002408 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002409 if (!(s->flags & SN_ERR_MASK))
2410 s->flags |= SN_ERR_CLICL;
2411
Willy Tarreaufcffa692010-01-10 14:21:19 +01002412 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002413 goto failed_keep_alive;
2414
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002416 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002417 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002418 session_inc_http_err_ctr(s);
2419 }
2420
Willy Tarreaudc979f22012-12-04 10:39:01 +01002421 txn->status = 400;
2422 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 msg->msg_state = HTTP_MSG_ERROR;
2424 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002425
Willy Tarreauda7ff642010-06-23 11:44:09 +02002426 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002427 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002428 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002429 if (s->listener->counters)
2430 s->listener->counters->failed_req++;
2431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 if (!(s->flags & SN_FINST_MASK))
2433 s->flags |= SN_FINST_R;
2434 return 0;
2435 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002438 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002439 if (!(s->flags & SN_ERR_MASK))
2440 s->flags |= SN_ERR_CLITO;
2441
Willy Tarreaufcffa692010-01-10 14:21:19 +01002442 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002443 goto failed_keep_alive;
2444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002446 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002447 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002448 session_inc_http_err_ctr(s);
2449 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002450 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002451 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 msg->msg_state = HTTP_MSG_ERROR;
2453 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002454
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002456 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002457 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002458 if (s->listener->counters)
2459 s->listener->counters->failed_req++;
2460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 if (!(s->flags & SN_FINST_MASK))
2462 s->flags |= SN_FINST_R;
2463 return 0;
2464 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002467 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002468 if (!(s->flags & SN_ERR_MASK))
2469 s->flags |= SN_ERR_CLICL;
2470
Willy Tarreaufcffa692010-01-10 14:21:19 +01002471 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002472 goto failed_keep_alive;
2473
Willy Tarreau4076a152009-04-02 15:18:36 +02002474 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002475 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002477 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 msg->msg_state = HTTP_MSG_ERROR;
2479 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480
Willy Tarreauda7ff642010-06-23 11:44:09 +02002481 session_inc_http_err_ctr(s);
2482 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002483 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485 if (s->listener->counters)
2486 s->listener->counters->failed_req++;
2487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 if (!(s->flags & SN_FINST_MASK))
2489 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002490 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 }
2492
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002493 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002494 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2495 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002496#ifdef TCP_QUICKACK
Willy Tarreauf79c8172013-10-21 16:30:56 +02002497 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 +01002498 /* We need more data, we have to re-enable quick-ack in case we
2499 * previously disabled it, otherwise we might cause the client
2500 * to delay next data.
2501 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002502 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002503 }
2504#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002505
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2507 /* If the client starts to talk, let's fall back to
2508 * request timeout processing.
2509 */
2510 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002511 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002512 }
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002515 if (!tick_isset(req->analyse_exp)) {
2516 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2517 (txn->flags & TX_WAIT_NEXT_RQ) &&
2518 tick_isset(s->be->timeout.httpka))
2519 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2520 else
2521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* we're not ready yet */
2525 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002526
2527 failed_keep_alive:
2528 /* Here we process low-level errors for keep-alive requests. In
2529 * short, if the request is not the first one and it experiences
2530 * a timeout, read error or shutdown, we just silently close so
2531 * that the client can try again.
2532 */
2533 txn->status = 0;
2534 msg->msg_state = HTTP_MSG_RQBEFORE;
2535 req->analysers = 0;
2536 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002537 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002538 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002539 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002540 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002542
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 /* OK now we have a complete HTTP request with indexed headers. Let's
2544 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002545 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002546 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002547 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002548 * byte after the last LF. msg->sov points to the first byte of data.
2549 * msg->eol cannot be trusted because it may have been left uninitialized
2550 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002551 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002552
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002554 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2555
Willy Tarreaub16a5742010-01-10 14:46:16 +01002556 if (txn->flags & TX_WAIT_NEXT_RQ) {
2557 /* kill the pending keep-alive timeout */
2558 txn->flags &= ~TX_WAIT_NEXT_RQ;
2559 req->analyse_exp = TICK_ETERNITY;
2560 }
2561
2562
Willy Tarreaud787e662009-07-07 10:14:51 +02002563 /* Maybe we found in invalid header name while we were configured not
2564 * to block on that, so we have to capture it now.
2565 */
2566 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002567 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /*
2570 * 1: identify the method
2571 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002572 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002573
2574 /* we can make use of server redirect on GET and HEAD */
2575 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2576 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002577
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 /*
2579 * 2: check if the URI matches the monitor_uri.
2580 * We have to do this for every request which gets in, because
2581 * the monitor-uri is defined by the frontend.
2582 */
2583 if (unlikely((s->fe->monitor_uri_len != 0) &&
2584 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002585 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 s->fe->monitor_uri,
2587 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002594 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002595
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002597 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002598 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 ret = acl_pass(ret);
2601 if (cond->pol == ACL_COND_UNLESS)
2602 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 if (ret) {
2605 /* we fail this request, let's return 503 service unavail */
2606 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002608 if (!(s->flags & SN_ERR_MASK))
2609 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002611 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002612 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002613
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 /* nothing to fail, let's reply normaly */
2615 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002616 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 goto return_prx_cond;
2620 }
2621
2622 /*
2623 * 3: Maybe we have to copy the original REQURI for the logs ?
2624 * Note: we cannot log anymore if the request has been
2625 * classified as invalid.
2626 */
2627 if (unlikely(s->logs.logwait & LW_REQ)) {
2628 /* we have a complete HTTP request that we must log */
2629 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2630 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 if (urilen >= REQURI_LEN)
2633 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002634 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002637 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 s->do_log(s);
2639 } else {
2640 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002641 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 }
Willy Tarreau06619262006-12-17 08:37:22 +01002643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002645 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002646 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647
Willy Tarreau5b154472009-12-21 20:11:07 +01002648 /* ... and check if the request is HTTP/1.1 or above */
2649 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002650 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2651 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2652 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002653 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002654
2655 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002656 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 +01002657
Willy Tarreau88d349d2010-01-25 12:15:43 +01002658 /* if the frontend has "option http-use-proxy-header", we'll check if
2659 * we have what looks like a proxied connection instead of a connection,
2660 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2661 * Note that this is *not* RFC-compliant, however browsers and proxies
2662 * happen to do that despite being non-standard :-(
2663 * We consider that a request not beginning with either '/' or '*' is
2664 * a proxied connection, which covers both "scheme://location" and
2665 * CONNECT ip:port.
2666 */
2667 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002669 txn->flags |= TX_USE_PX_CONN;
2670
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002671 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002672 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002675 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679 /* 6: determine the transfer-length.
2680 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2681 * the presence of a message-body in a REQUEST and its transfer length
2682 * must be determined that way (in order of precedence) :
2683 * 1. The presence of a message-body in a request is signaled by the
2684 * inclusion of a Content-Length or Transfer-Encoding header field
2685 * in the request's header fields. When a request message contains
2686 * both a message-body of non-zero length and a method that does
2687 * not define any semantics for that request message-body, then an
2688 * origin server SHOULD either ignore the message-body or respond
2689 * with an appropriate error message (e.g., 413). A proxy or
2690 * gateway, when presented the same request, SHOULD either forward
2691 * the request inbound with the message- body or ignore the
2692 * message-body when determining a response.
2693 *
2694 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2695 * and the "chunked" transfer-coding (Section 6.2) is used, the
2696 * transfer-length is defined by the use of this transfer-coding.
2697 * If a Transfer-Encoding header field is present and the "chunked"
2698 * transfer-coding is not present, the transfer-length is defined
2699 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002700 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 * 3. If a Content-Length header field is present, its decimal value in
2702 * OCTETs represents both the entity-length and the transfer-length.
2703 * If a message is received with both a Transfer-Encoding header
2704 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002705 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002706 * 4. By the server closing the connection. (Closing the connection
2707 * cannot be used to indicate the end of a request body, since that
2708 * would leave no possibility for the server to send back a response.)
2709 *
2710 * Whenever a transfer-coding is applied to a message-body, the set of
2711 * transfer-codings MUST include "chunked", unless the message indicates
2712 * it is terminated by closing the connection. When the "chunked"
2713 * transfer-coding is used, it MUST be the last transfer-coding applied
2714 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002715 */
2716
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002717 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002718 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002720 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002723 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2724 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 /* bad transfer-encoding (chunked followed by something else) */
2726 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002728 break;
2729 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 }
2731
Willy Tarreau32b47f42009-10-18 20:55:02 +02002732 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002733 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002734 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735 signed long long cl;
2736
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002739 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002740 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741
Willy Tarreauad14f752011-09-02 20:33:27 +02002742 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002744 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746
Willy Tarreauad14f752011-09-02 20:33:27 +02002747 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002748 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002752 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002753 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002755 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002757 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002758 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002759 }
2760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761 /* bodyless requests have a known length */
2762 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002763 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002766 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 req->analyse_exp = TICK_ETERNITY;
2768 return 1;
2769
2770 return_bad_req:
2771 /* We centralize bad requests processing here */
2772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2773 /* we detected a parsing error. We want to archive this request
2774 * in the dedicated proxy area for later troubleshooting.
2775 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002776 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 }
2778
2779 txn->req.msg_state = HTTP_MSG_ERROR;
2780 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002781 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002782
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002783 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002784 if (s->listener->counters)
2785 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002786
2787 return_prx_cond:
2788 if (!(s->flags & SN_ERR_MASK))
2789 s->flags |= SN_ERR_PRXCOND;
2790 if (!(s->flags & SN_FINST_MASK))
2791 s->flags |= SN_FINST_R;
2792
2793 req->analysers = 0;
2794 req->analyse_exp = TICK_ETERNITY;
2795 return 0;
2796}
2797
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002798
Willy Tarreau347a35d2013-11-22 17:51:09 +01002799/* This function prepares an applet to handle the stats. It can deal with the
2800 * "100-continue" expectation, check that admin rules are met for POST requests,
2801 * and program a response message if something was unexpected. It cannot fail
2802 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002803 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002804 * s->target which is supposed to already point to the stats applet. The caller
2805 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002806 */
2807int http_handle_stats(struct session *s, struct channel *req)
2808{
2809 struct stats_admin_rule *stats_admin_rule;
2810 struct stream_interface *si = s->rep->prod;
2811 struct http_txn *txn = &s->txn;
2812 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002813 struct uri_auth *uri_auth = s->be->uri_auth;
2814 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002815 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002817 appctx = si_appctx(si);
2818 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2819 appctx->st1 = appctx->st2 = 0;
2820 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2821 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002822
2823 uri = msg->chn->buf->p + msg->sl.rq.u;
2824 lookup = uri + uri_auth->uri_len;
2825
2826 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2827 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002828 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002829 break;
2830 }
2831 }
2832
2833 if (uri_auth->refresh) {
2834 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2835 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002836 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002837 break;
2838 }
2839 }
2840 }
2841
2842 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2843 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002844 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002845 break;
2846 }
2847 }
2848
2849 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2850 if (memcmp(h, ";st=", 4) == 0) {
2851 int i;
2852 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002853 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002854 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2855 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002857 break;
2858 }
2859 }
2860 break;
2861 }
2862 }
2863
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx->ctx.stats.scope_str = 0;
2865 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002866 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2867 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2868 int itx = 0;
2869 const char *h2;
2870 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2871 const char *err;
2872
2873 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2874 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002875 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002876 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2877 itx++;
2878 h++;
2879 }
2880
2881 if (itx > STAT_SCOPE_TXT_MAXLEN)
2882 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002885 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002886 memcpy(scope_txt, h2, itx);
2887 scope_txt[itx] = '\0';
2888 err = invalid_char(scope_txt);
2889 if (err) {
2890 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 appctx->ctx.stats.scope_str = 0;
2892 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002893 }
2894 break;
2895 }
2896 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002897
2898 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002899 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002900 int ret = 1;
2901
2902 if (stats_admin_rule->cond) {
2903 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2904 ret = acl_pass(ret);
2905 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2906 ret = !ret;
2907 }
2908
2909 if (ret) {
2910 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002911 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002912 break;
2913 }
2914 }
2915
2916 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002917 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002918 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002919 if (msg->msg_state < HTTP_MSG_100_SENT) {
2920 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2921 * send an HTTP/1.1 100 Continue intermediate response.
2922 */
2923 if (msg->flags & HTTP_MSGF_VER_11) {
2924 struct hdr_ctx ctx;
2925 ctx.idx = 0;
2926 /* Expect is allowed in 1.1, look for it */
2927 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2928 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2929 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2930 }
2931 }
2932 msg->msg_state = HTTP_MSG_100_SENT;
2933 s->logs.tv_request = now; /* update the request timer to reflect full request */
2934 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002936 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002937 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2939 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002940 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002941 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002942 else {
2943 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002945 }
2946
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002947 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002948 return 1;
2949}
2950
Lukas Tribus67db8df2013-06-23 17:37:13 +02002951/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2952 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2953 */
2954static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2955{
2956#ifdef IP_TOS
2957 if (from.ss_family == AF_INET)
2958 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2959#endif
2960#ifdef IPV6_TCLASS
2961 if (from.ss_family == AF_INET6) {
2962 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2963 /* v4-mapped addresses need IP_TOS */
2964 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2965 else
2966 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2967 }
2968#endif
2969}
2970
Willy Tarreau20b0de52012-12-24 15:45:22 +01002971/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002972 * transaction <txn>. Returns the first rule that prevents further processing
2973 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2974 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2975 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002977static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002978http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002979{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002980 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002981 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002982 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983
Willy Tarreauff011f22011-01-06 17:51:27 +01002984 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986 continue;
2987
Willy Tarreau96257ec2012-12-27 10:46:37 +01002988 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002990 int ret;
2991
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002992 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 ret = acl_pass(ret);
2994
Willy Tarreauff011f22011-01-06 17:51:27 +01002995 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002996 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002997
2998 if (!ret) /* condition not matched */
2999 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 }
3001
Willy Tarreau20b0de52012-12-24 15:45:22 +01003002
Willy Tarreau96257ec2012-12-27 10:46:37 +01003003 switch (rule->action) {
3004 case HTTP_REQ_ACT_ALLOW:
3005 return NULL; /* "allow" rules are OK */
3006
3007 case HTTP_REQ_ACT_DENY:
3008 txn->flags |= TX_CLDENY;
3009 return rule;
3010
Willy Tarreauccbcc372012-12-27 12:37:57 +01003011 case HTTP_REQ_ACT_TARPIT:
3012 txn->flags |= TX_CLTARPIT;
3013 return rule;
3014
Willy Tarreau96257ec2012-12-27 10:46:37 +01003015 case HTTP_REQ_ACT_AUTH:
3016 return rule;
3017
Willy Tarreau81499eb2012-12-27 12:19:02 +01003018 case HTTP_REQ_ACT_REDIR:
3019 return rule;
3020
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003021 case HTTP_REQ_ACT_SET_NICE:
3022 s->task->nice = rule->arg.nice;
3023 break;
3024
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003025 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003026 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003027 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003028 break;
3029
Willy Tarreau51347ed2013-06-11 19:34:13 +02003030 case HTTP_REQ_ACT_SET_MARK:
3031#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003032 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003033 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003034#endif
3035 break;
3036
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003037 case HTTP_REQ_ACT_SET_LOGL:
3038 s->logs.level = rule->arg.loglevel;
3039 break;
3040
Willy Tarreau96257ec2012-12-27 10:46:37 +01003041 case HTTP_REQ_ACT_SET_HDR:
3042 ctx.idx = 0;
3043 /* remove all occurrences of the header */
3044 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3045 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3046 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003047 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003048 /* now fall through to header addition */
3049
3050 case HTTP_REQ_ACT_ADD_HDR:
3051 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3052 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3053 trash.len = rule->arg.hdr_add.name_len;
3054 trash.str[trash.len++] = ':';
3055 trash.str[trash.len++] = ' ';
3056 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3057 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3058 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003059 }
3060 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003061
3062 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003063 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003064}
3065
Willy Tarreau71241ab2012-12-27 11:30:54 +01003066
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003067/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3068 * transaction <txn>. Returns the first rule that prevents further processing
3069 * of the response (deny, ...) or NULL if it executed all rules or stopped
3070 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3071 * rule.
3072 */
3073static struct http_res_rule *
3074http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3075{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003076 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003077 struct http_res_rule *rule;
3078 struct hdr_ctx ctx;
3079
3080 list_for_each_entry(rule, rules, list) {
3081 if (rule->action >= HTTP_RES_ACT_MAX)
3082 continue;
3083
3084 /* check optional condition */
3085 if (rule->cond) {
3086 int ret;
3087
3088 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3089 ret = acl_pass(ret);
3090
3091 if (rule->cond->pol == ACL_COND_UNLESS)
3092 ret = !ret;
3093
3094 if (!ret) /* condition not matched */
3095 continue;
3096 }
3097
3098
3099 switch (rule->action) {
3100 case HTTP_RES_ACT_ALLOW:
3101 return NULL; /* "allow" rules are OK */
3102
3103 case HTTP_RES_ACT_DENY:
3104 txn->flags |= TX_SVDENY;
3105 return rule;
3106
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003107 case HTTP_RES_ACT_SET_NICE:
3108 s->task->nice = rule->arg.nice;
3109 break;
3110
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003111 case HTTP_RES_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003112 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003113 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003114 break;
3115
Willy Tarreau51347ed2013-06-11 19:34:13 +02003116 case HTTP_RES_ACT_SET_MARK:
3117#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003118 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003119 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003120#endif
3121 break;
3122
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003123 case HTTP_RES_ACT_SET_LOGL:
3124 s->logs.level = rule->arg.loglevel;
3125 break;
3126
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003127 case HTTP_RES_ACT_SET_HDR:
3128 ctx.idx = 0;
3129 /* remove all occurrences of the header */
3130 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3131 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3132 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3133 }
3134 /* now fall through to header addition */
3135
3136 case HTTP_RES_ACT_ADD_HDR:
3137 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3138 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3139 trash.len = rule->arg.hdr_add.name_len;
3140 trash.str[trash.len++] = ':';
3141 trash.str[trash.len++] = ' ';
3142 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3143 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3144 break;
3145 }
3146 }
3147
3148 /* we reached the end of the rules, nothing to report */
3149 return NULL;
3150}
3151
3152
Willy Tarreau71241ab2012-12-27 11:30:54 +01003153/* Perform an HTTP redirect based on the information in <rule>. The function
3154 * returns non-zero on success, or zero in case of a, irrecoverable error such
3155 * as too large a request to build a valid response.
3156 */
3157static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3158{
3159 struct http_msg *msg = &txn->req;
3160 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003161 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003162
3163 /* build redirect message */
3164 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003165 case 308:
3166 msg_fmt = HTTP_308;
3167 break;
3168 case 307:
3169 msg_fmt = HTTP_307;
3170 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171 case 303:
3172 msg_fmt = HTTP_303;
3173 break;
3174 case 301:
3175 msg_fmt = HTTP_301;
3176 break;
3177 case 302:
3178 default:
3179 msg_fmt = HTTP_302;
3180 break;
3181 }
3182
3183 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3184 return 0;
3185
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003186 location = trash.str + trash.len;
3187
Willy Tarreau71241ab2012-12-27 11:30:54 +01003188 switch(rule->type) {
3189 case REDIRECT_TYPE_SCHEME: {
3190 const char *path;
3191 const char *host;
3192 struct hdr_ctx ctx;
3193 int pathlen;
3194 int hostlen;
3195
3196 host = "";
3197 hostlen = 0;
3198 ctx.idx = 0;
3199 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3200 host = ctx.line + ctx.val;
3201 hostlen = ctx.vlen;
3202 }
3203
3204 path = http_get_path(txn);
3205 /* build message using path */
3206 if (path) {
3207 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3208 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3209 int qs = 0;
3210 while (qs < pathlen) {
3211 if (path[qs] == '?') {
3212 pathlen = qs;
3213 break;
3214 }
3215 qs++;
3216 }
3217 }
3218 } else {
3219 path = "/";
3220 pathlen = 1;
3221 }
3222
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003223 if (rule->rdr_str) { /* this is an old "redirect" rule */
3224 /* check if we can add scheme + "://" + host + path */
3225 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3226 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* add scheme */
3229 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3230 trash.len += rule->rdr_len;
3231 }
3232 else {
3233 /* add scheme with executing log format */
3234 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003236 /* check if we can add scheme + "://" + host + path */
3237 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3238 return 0;
3239 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003240 /* add "://" */
3241 memcpy(trash.str + trash.len, "://", 3);
3242 trash.len += 3;
3243
3244 /* add host */
3245 memcpy(trash.str + trash.len, host, hostlen);
3246 trash.len += hostlen;
3247
3248 /* add path */
3249 memcpy(trash.str + trash.len, path, pathlen);
3250 trash.len += pathlen;
3251
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253 if (trash.len && trash.str[trash.len - 1] != '/' &&
3254 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3255 if (trash.len > trash.size - 5)
3256 return 0;
3257 trash.str[trash.len] = '/';
3258 trash.len++;
3259 }
3260
3261 break;
3262 }
3263 case REDIRECT_TYPE_PREFIX: {
3264 const char *path;
3265 int pathlen;
3266
3267 path = http_get_path(txn);
3268 /* build message using path */
3269 if (path) {
3270 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3271 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3272 int qs = 0;
3273 while (qs < pathlen) {
3274 if (path[qs] == '?') {
3275 pathlen = qs;
3276 break;
3277 }
3278 qs++;
3279 }
3280 }
3281 } else {
3282 path = "/";
3283 pathlen = 1;
3284 }
3285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003286 if (rule->rdr_str) { /* this is an old "redirect" rule */
3287 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3288 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003289
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003290 /* add prefix. Note that if prefix == "/", we don't want to
3291 * add anything, otherwise it makes it hard for the user to
3292 * configure a self-redirection.
3293 */
3294 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3295 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3296 trash.len += rule->rdr_len;
3297 }
3298 }
3299 else {
3300 /* add prefix with executing log format */
3301 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3302
3303 /* Check length */
3304 if (trash.len + pathlen > trash.size - 4)
3305 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306 }
3307
3308 /* add path */
3309 memcpy(trash.str + trash.len, path, pathlen);
3310 trash.len += pathlen;
3311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313 if (trash.len && trash.str[trash.len - 1] != '/' &&
3314 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3315 if (trash.len > trash.size - 5)
3316 return 0;
3317 trash.str[trash.len] = '/';
3318 trash.len++;
3319 }
3320
3321 break;
3322 }
3323 case REDIRECT_TYPE_LOCATION:
3324 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003325 if (rule->rdr_str) { /* this is an old "redirect" rule */
3326 if (trash.len + rule->rdr_len > trash.size - 4)
3327 return 0;
3328
3329 /* add location */
3330 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3331 trash.len += rule->rdr_len;
3332 }
3333 else {
3334 /* add location with executing log format */
3335 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 /* Check left length */
3338 if (trash.len > trash.size - 4)
3339 return 0;
3340 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003341 break;
3342 }
3343
3344 if (rule->cookie_len) {
3345 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3346 trash.len += 14;
3347 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3348 trash.len += rule->cookie_len;
3349 memcpy(trash.str + trash.len, "\r\n", 2);
3350 trash.len += 2;
3351 }
3352
3353 /* add end of headers and the keep-alive/close status.
3354 * We may choose to set keep-alive if the Location begins
3355 * with a slash, because the client will come back to the
3356 * same server.
3357 */
3358 txn->status = rule->code;
3359 /* let's log the request time */
3360 s->logs.tv_request = now;
3361
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003362 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003363 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3364 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3365 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3366 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3367 /* keep-alive possible */
3368 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3369 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3370 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3371 trash.len += 30;
3372 } else {
3373 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3374 trash.len += 24;
3375 }
3376 }
3377 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3378 trash.len += 4;
3379 bo_inject(txn->rsp.chn, trash.str, trash.len);
3380 /* "eat" the request */
3381 bi_fast_delete(txn->req.chn->buf, msg->sov);
3382 msg->sov = 0;
3383 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3384 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3385 txn->req.msg_state = HTTP_MSG_CLOSED;
3386 txn->rsp.msg_state = HTTP_MSG_DONE;
3387 } else {
3388 /* keep-alive not possible */
3389 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3390 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3391 trash.len += 29;
3392 } else {
3393 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3394 trash.len += 23;
3395 }
3396 stream_int_retnclose(txn->req.chn->prod, &trash);
3397 txn->req.chn->analysers = 0;
3398 }
3399
3400 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003401 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003402 if (!(s->flags & SN_FINST_MASK))
3403 s->flags |= SN_FINST_R;
3404
3405 return 1;
3406}
3407
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003408/* This stream analyser runs all HTTP request processing which is common to
3409 * frontends and backends, which means blocking ACLs, filters, connection-close,
3410 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003411 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003412 * either needs more data or wants to immediately abort the request (eg: deny,
3413 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003414 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003415int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003416{
Willy Tarreaud787e662009-07-07 10:14:51 +02003417 struct http_txn *txn = &s->txn;
3418 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003420 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003421 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003422 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003423
Willy Tarreau655dce92009-11-08 13:10:58 +01003424 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003425 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003426 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003427 return 0;
3428 }
3429
Willy Tarreau3a816292009-07-07 10:55:49 +02003430 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003431 req->analyse_exp = TICK_ETERNITY;
3432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003433 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 +02003434 now_ms, __FUNCTION__,
3435 s,
3436 req,
3437 req->rex, req->wex,
3438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003439 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003440 req->analysers);
3441
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003442 /* first check whether we have some ACLs set to block this request */
3443 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003444 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003446 ret = acl_pass(ret);
3447 if (cond->pol == ACL_COND_UNLESS)
3448 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 if (ret) {
3451 txn->status = 403;
3452 /* let's log the request time */
3453 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003454 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003455 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003459
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003460 /* just in case we have some per-backend tracking */
3461 session_inc_be_http_req_ctr(s);
3462
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003463 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003465
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003466 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003467 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003468 if (stats_check_uri(s->rep->prod, txn, px)) {
3469 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003470 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3471 txn->status = 500;
3472 s->logs.tv_request = now;
3473 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003475 if (!(s->flags & SN_ERR_MASK))
3476 s->flags |= SN_ERR_RESOURCE;
3477 goto return_prx_cond;
3478 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003479 /* parse the whole stats request and extract the relevant information */
3480 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003481 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 +01003482 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003483 }
3484
Willy Tarreau3b44e722013-11-16 10:28:23 +01003485 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3486 * blocked by an http-request rule.
3487 */
3488 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003489 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003491 }
Willy Tarreau06619262006-12-17 08:37:22 +01003492
Willy Tarreau3b44e722013-11-16 10:28:23 +01003493 /* return a 403 if either rule has blocked */
3494 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003495 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003497 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003498 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003499 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003500 s->fe->fe_counters.denied_req++;
3501 if (s->fe != s->be)
3502 s->be->be_counters.denied_req++;
3503 if (s->listener->counters)
3504 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 goto return_prx_cond;
3506 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003507
3508 /* When a connection is tarpitted, we use the tarpit timeout,
3509 * which may be the same as the connect timeout if unspecified.
3510 * If unset, then set it to zero because we really want it to
3511 * eventually expire. We build the tarpit as an analyser.
3512 */
3513 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003514 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003515 /* wipe the request out so that we can drop the connection early
3516 * if the client closes first.
3517 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003518 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003519 req->analysers = 0; /* remove switching rules etc... */
3520 req->analysers |= AN_REQ_HTTP_TARPIT;
3521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3522 if (!req->analyse_exp)
3523 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003524 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003525 s->fe->fe_counters.denied_req++;
3526 if (s->fe != s->be)
3527 s->be->be_counters.denied_req++;
3528 if (s->listener->counters)
3529 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003530 return 1;
3531 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003532 }
Willy Tarreau06619262006-12-17 08:37:22 +01003533
Willy Tarreau5b154472009-12-21 20:11:07 +01003534 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3535 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003536 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3537 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003538 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3539 * avoid to redo the same work if FE and BE have the same settings (common).
3540 * The method consists in checking if options changed between the two calls
3541 * (implying that either one is non-null, or one of them is non-null and we
3542 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003543 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003544
Willy Tarreaudc008c52010-02-01 16:20:08 +01003545 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3546 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3547 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3548 (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 +01003549 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003550
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003551 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3552 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003553 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003554 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3555 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003556 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003557 tmp = TX_CON_WANT_CLO;
3558
Willy Tarreau5b154472009-12-21 20:11:07 +01003559 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003561
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003562 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3563 /* parse the Connection header and possibly clean it */
3564 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003565 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003566 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3567 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003568 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003569 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003571 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003572 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003573
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003574 /* check if client or config asks for explicit close in KAL/SCL */
3575 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3576 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3577 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003578 (!(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 +01003579 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003580 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003581 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3583 }
Willy Tarreau78599912009-10-17 20:12:21 +02003584
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003585 /* we can be blocked here because the request needs to be authenticated,
3586 * either to pass or to access stats.
3587 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003588 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003589 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003590
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003591 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003592 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003593
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003594 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003595 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003597 /* on 401 we still count one error, because normal browsing
3598 * won't significantly increase the counter but brute force
3599 * attempts will.
3600 */
3601 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003602 goto return_prx_cond;
3603 }
3604
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003605 /* add request headers from the rule sets in the same order */
3606 list_for_each_entry(wl, &px->req_add, list) {
3607 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003608 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003609 ret = acl_pass(ret);
3610 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3611 ret = !ret;
3612 if (!ret)
3613 continue;
3614 }
3615
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003616 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003618 }
3619
3620 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3621 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3622 goto return_bad_req;
3623 req->analyse_exp = TICK_ETERNITY;
3624 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003625 }
3626
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003627 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003628 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003629 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3630 s->fe->fe_counters.intercepted_req++;
3631
3632 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3633 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3634 if (!(s->flags & SN_FINST_MASK))
3635 s->flags |= SN_FINST_R;
3636
3637 req->analyse_exp = TICK_ETERNITY;
3638 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003639 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003641
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 /* check whether we have some ACLs set to redirect this request */
3643 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003644 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003645 int ret;
3646
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003647 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003648 ret = acl_pass(ret);
3649 if (rule->cond->pol == ACL_COND_UNLESS)
3650 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003651 if (!ret)
3652 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003653 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003654 if (!http_apply_redirect_rule(rule, s, txn))
3655 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003656
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 req->analyse_exp = TICK_ETERNITY;
3658 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003660
Willy Tarreau2be39392010-01-03 17:24:51 +01003661 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3662 * If this happens, then the data will not come immediately, so we must
3663 * send all what we have without waiting. Note that due to the small gain
3664 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003665 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003666 * itself once used.
3667 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003668 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003669
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 /* that's OK for us now, let's move on to next analysers */
3671 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 return_bad_req:
3674 /* We centralize bad requests processing here */
3675 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3676 /* we detected a parsing error. We want to archive this request
3677 * in the dedicated proxy area for later troubleshooting.
3678 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003679 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003681
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003682 txn->req.msg_state = HTTP_MSG_ERROR;
3683 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003684 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003685
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003686 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003687 if (s->listener->counters)
3688 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003689
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003690 return_prx_cond:
3691 if (!(s->flags & SN_ERR_MASK))
3692 s->flags |= SN_ERR_PRXCOND;
3693 if (!(s->flags & SN_FINST_MASK))
3694 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003696 req->analysers = 0;
3697 req->analyse_exp = TICK_ETERNITY;
3698 return 0;
3699}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003700
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701/* This function performs all the processing enabled for the current request.
3702 * It returns 1 if the processing can continue on next analysers, or zero if it
3703 * needs more data, encounters an error, or wants to immediately abort the
3704 * request. It relies on buffers flags, and updates s->req->analysers.
3705 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003706int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707{
3708 struct http_txn *txn = &s->txn;
3709 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003710 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003711
Willy Tarreau655dce92009-11-08 13:10:58 +01003712 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003713 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003714 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003715 return 0;
3716 }
3717
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003718 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 +02003719 now_ms, __FUNCTION__,
3720 s,
3721 req,
3722 req->rex, req->wex,
3723 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003724 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003725 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003726
William Lallemand82fe75c2012-10-23 10:25:10 +02003727 if (s->fe->comp || s->be->comp)
3728 select_compression_request_header(s, req->buf);
3729
Willy Tarreau59234e92008-11-30 23:51:27 +01003730 /*
3731 * Right now, we know that we have processed the entire headers
3732 * and that unwanted requests have been filtered out. We can do
3733 * whatever we want with the remaining request. Also, now we
3734 * may have separate values for ->fe, ->be.
3735 */
Willy Tarreau06619262006-12-17 08:37:22 +01003736
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003738 * If HTTP PROXY is set we simply get remote server address parsing
3739 * incoming request. Note that this requires that a connection is
3740 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 */
3742 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003743 struct connection *conn;
3744
3745 if (unlikely((conn = si_alloc_conn(req->cons)) == NULL)) {
3746 txn->req.msg_state = HTTP_MSG_ERROR;
3747 txn->status = 500;
3748 req->analysers = 0;
3749 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3750
3751 if (!(s->flags & SN_ERR_MASK))
3752 s->flags |= SN_ERR_RESOURCE;
3753 if (!(s->flags & SN_FINST_MASK))
3754 s->flags |= SN_FINST_R;
3755
3756 return 0;
3757 }
3758 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003760
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003762 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 * Note that doing so might move headers in the request, but
3764 * the fields will stay coherent and the URI will not move.
3765 * This should only be performed in the backend.
3766 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003767 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003768 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3769 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003770
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003772 * 8: the appsession cookie was looked up very early in 1.2,
3773 * so let's do the same now.
3774 */
3775
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003776 /* It needs to look into the URI unless persistence must be ignored */
3777 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003778 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 +01003779 }
3780
William Lallemanda73203e2012-03-12 12:48:57 +01003781 /* add unique-id if "header-unique-id" is specified */
3782
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003783 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3784 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3785 goto return_bad_req;
3786 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003787 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003788 }
William Lallemanda73203e2012-03-12 12:48:57 +01003789
3790 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003791 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3792 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003793 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003794 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003795 goto return_bad_req;
3796 }
3797
Cyril Bontéb21570a2009-11-29 20:04:48 +01003798 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 * 9: add X-Forwarded-For if either the frontend or the backend
3800 * asks for it.
3801 */
3802 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003803 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003804 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003805 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3806 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003807 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003808 /* The header is set to be added only if none is present
3809 * and we found it, so don't do anything.
3810 */
3811 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003813 /* Add an X-Forwarded-For header unless the source IP is
3814 * in the 'except' network range.
3815 */
3816 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003817 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 != s->fe->except_net.s_addr) &&
3819 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003820 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003821 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003822 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003824 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003825
3826 /* Note: we rely on the backend to get the header name to be used for
3827 * x-forwarded-for, because the header is really meant for the backends.
3828 * However, if the backend did not specify any option, we have to rely
3829 * on the frontend's header name.
3830 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003831 if (s->be->fwdfor_hdr_len) {
3832 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003833 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003834 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003836 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003837 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003838 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003839
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003840 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003841 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003842 }
3843 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003844 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003845 /* FIXME: for the sake of completeness, we should also support
3846 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003847 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003848 int len;
3849 char pn[INET6_ADDRSTRLEN];
3850 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003851 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003853
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 /* Note: we rely on the backend to get the header name to be used for
3855 * x-forwarded-for, because the header is really meant for the backends.
3856 * However, if the backend did not specify any option, we have to rely
3857 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003858 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003859 if (s->be->fwdfor_hdr_len) {
3860 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003861 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003862 } else {
3863 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003864 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003865 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003866 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003867
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003868 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003869 goto return_bad_req;
3870 }
3871 }
3872
3873 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003874 * 10: add X-Original-To if either the frontend or the backend
3875 * asks for it.
3876 */
3877 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3878
3879 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003880 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003881 /* Add an X-Original-To header unless the destination IP is
3882 * in the 'except' network range.
3883 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003884 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003885
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003886 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003887 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003888 (((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 +02003889 != s->fe->except_to.s_addr) &&
3890 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003891 (((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 +02003892 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003893 int len;
3894 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003895 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003896
3897 /* Note: we rely on the backend to get the header name to be used for
3898 * x-original-to, because the header is really meant for the backends.
3899 * However, if the backend did not specify any option, we have to rely
3900 * on the frontend's header name.
3901 */
3902 if (s->be->orgto_hdr_len) {
3903 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003904 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003905 } else {
3906 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003907 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003908 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003909 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003912 goto return_bad_req;
3913 }
3914 }
3915 }
3916
Willy Tarreau50fc7772012-11-11 22:19:57 +01003917 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3918 * If an "Upgrade" token is found, the header is left untouched in order not to have
3919 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3920 * "Upgrade" is present in the Connection header.
3921 */
3922 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3923 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3924 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003925 unsigned int want_flags = 0;
3926
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003927 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003928 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3929 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3930 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003931 want_flags |= TX_CON_CLO_SET;
3932 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003933 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3934 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3935 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003936 want_flags |= TX_CON_KAL_SET;
3937 }
3938
3939 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003940 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003943
Willy Tarreau522d6c02009-12-06 18:49:18 +01003944 /* If we have no server assigned yet and we're balancing on url_param
3945 * with a POST request, we may be interested in checking the body for
3946 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003947 */
3948 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3949 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003950 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003951 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003952 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003953 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003954 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003955
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003956 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003957 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003958#ifdef TCP_QUICKACK
3959 /* We expect some data from the client. Unless we know for sure
3960 * we already have a full request, we have to re-enable quick-ack
3961 * in case we previously disabled it, otherwise we might cause
3962 * the client to delay further data.
3963 */
3964 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003965 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003966 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003967 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003968 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003969#endif
3970 }
Willy Tarreau03945942009-12-22 16:50:27 +01003971
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 /*************************************************************
3973 * OK, that's finished for the headers. We have done what we *
3974 * could. Let's switch to the DATA state. *
3975 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003976 req->analyse_exp = TICK_ETERNITY;
3977 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003978
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003979 /* if the server closes the connection, we want to immediately react
3980 * and close the socket to save packets and syscalls.
3981 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003982 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3983 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003984
Willy Tarreau59234e92008-11-30 23:51:27 +01003985 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 /* OK let's go on with the BODY now */
3987 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003988
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003990 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003991 /* we detected a parsing error. We want to archive this request
3992 * in the dedicated proxy area for later troubleshooting.
3993 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003994 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003995 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003996
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 txn->req.msg_state = HTTP_MSG_ERROR;
3998 txn->status = 400;
3999 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004000 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004001
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004002 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004003 if (s->listener->counters)
4004 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004005
Willy Tarreau59234e92008-11-30 23:51:27 +01004006 if (!(s->flags & SN_ERR_MASK))
4007 s->flags |= SN_ERR_PRXCOND;
4008 if (!(s->flags & SN_FINST_MASK))
4009 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004010 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004011}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004012
Willy Tarreau60b85b02008-11-30 23:28:40 +01004013/* This function is an analyser which processes the HTTP tarpit. It always
4014 * returns zero, at the beginning because it prevents any other processing
4015 * from occurring, and at the end because it terminates the request.
4016 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004017int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004018{
4019 struct http_txn *txn = &s->txn;
4020
4021 /* This connection is being tarpitted. The CLIENT side has
4022 * already set the connect expiration date to the right
4023 * timeout. We just have to check that the client is still
4024 * there and that the timeout has not expired.
4025 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004026 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004027 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004028 !tick_is_expired(req->analyse_exp, now_ms))
4029 return 0;
4030
4031 /* We will set the queue timer to the time spent, just for
4032 * logging purposes. We fake a 500 server error, so that the
4033 * attacker will not suspect his connection has been tarpitted.
4034 * It will not cause trouble to the logs because we can exclude
4035 * the tarpitted connections by filtering on the 'PT' status flags.
4036 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004037 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4038
4039 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004040 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004041 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004042
4043 req->analysers = 0;
4044 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004045
Willy Tarreau60b85b02008-11-30 23:28:40 +01004046 if (!(s->flags & SN_ERR_MASK))
4047 s->flags |= SN_ERR_PRXCOND;
4048 if (!(s->flags & SN_FINST_MASK))
4049 s->flags |= SN_FINST_T;
4050 return 0;
4051}
4052
Willy Tarreaud34af782008-11-30 23:36:37 +01004053/* This function is an analyser which processes the HTTP request body. It looks
4054 * for parameters to be used for the load balancing algorithm (url_param). It
4055 * must only be called after the standard HTTP request processing has occurred,
4056 * because it expects the request to be parsed. It returns zero if it needs to
4057 * read more data, or 1 once it has completed its analysis.
4058 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004059int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004060{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004061 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004062 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004064
4065 /* We have to parse the HTTP request body to find any required data.
4066 * "balance url_param check_post" should have been the only way to get
4067 * into this. We were brought here after HTTP header analysis, so all
4068 * related structures are ready.
4069 */
4070
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4072 goto missing_data;
4073
4074 if (msg->msg_state < HTTP_MSG_100_SENT) {
4075 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4076 * send an HTTP/1.1 100 Continue intermediate response.
4077 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004078 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004079 struct hdr_ctx ctx;
4080 ctx.idx = 0;
4081 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004082 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004083 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004084 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085 }
4086 }
4087 msg->msg_state = HTTP_MSG_100_SENT;
4088 }
4089
4090 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004091 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004092 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004093 * is still null. We must save the body in msg->next because it
4094 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004095 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004096 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004097
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004098 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004099 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4100 else
4101 msg->msg_state = HTTP_MSG_DATA;
4102 }
4103
4104 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004105 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004106 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004107 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004108 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004109 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004110
Willy Tarreau115acb92009-12-26 13:56:06 +01004111 if (!ret)
4112 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004113 else if (ret < 0) {
4114 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004116 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004117 }
4118
Willy Tarreaud98cf932009-12-27 22:54:55 +01004119 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004120 * We have the first data byte is in msg->sov. We're waiting for at
4121 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004122 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004123
Willy Tarreau124d9912011-03-01 20:30:48 +01004124 if (msg->body_len < limit)
4125 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004126
Willy Tarreau9b28e032012-10-12 23:49:43 +02004127 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004128 goto http_end;
4129
4130 missing_data:
4131 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004132 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004133 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004134 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004135 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004136
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004137 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004139 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004140
4141 if (!(s->flags & SN_ERR_MASK))
4142 s->flags |= SN_ERR_CLITO;
4143 if (!(s->flags & SN_FINST_MASK))
4144 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004145 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004146 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004147
4148 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004149 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004150 /* Not enough data. We'll re-use the http-request
4151 * timeout here. Ideally, we should set the timeout
4152 * relative to the accept() date. We just set the
4153 * request timeout once at the beginning of the
4154 * request.
4155 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004156 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004157 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004158 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004159 return 0;
4160 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004161
4162 http_end:
4163 /* The situation will not evolve, so let's give up on the analysis. */
4164 s->logs.tv_request = now; /* update the request timer to reflect full request */
4165 req->analysers &= ~an_bit;
4166 req->analyse_exp = TICK_ETERNITY;
4167 return 1;
4168
4169 return_bad_req: /* let's centralize all bad requests */
4170 txn->req.msg_state = HTTP_MSG_ERROR;
4171 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004172 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004173
Willy Tarreau79ebac62010-06-07 13:47:49 +02004174 if (!(s->flags & SN_ERR_MASK))
4175 s->flags |= SN_ERR_PRXCOND;
4176 if (!(s->flags & SN_FINST_MASK))
4177 s->flags |= SN_FINST_R;
4178
Willy Tarreau522d6c02009-12-06 18:49:18 +01004179 return_err_msg:
4180 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004181 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004182 if (s->listener->counters)
4183 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004185}
4186
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004187/* send a server's name with an outgoing request over an established connection.
4188 * Note: this function is designed to be called once the request has been scheduled
4189 * for being forwarded. This is the reason why it rewinds the buffer before
4190 * proceeding.
4191 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004192int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004193
4194 struct hdr_ctx ctx;
4195
Mark Lamourinec2247f02012-01-04 13:02:01 -05004196 char *hdr_name = be->server_id_hdr_name;
4197 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004198 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004199 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004200 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004201
William Lallemandd9e90662012-01-30 17:27:17 +01004202 ctx.idx = 0;
4203
Willy Tarreau9b28e032012-10-12 23:49:43 +02004204 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205 if (old_o) {
4206 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004207 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004208 }
4209
Willy Tarreau9b28e032012-10-12 23:49:43 +02004210 old_i = chn->buf->i;
4211 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 -05004212 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004213 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004214 }
4215
4216 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004217 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004218 memcpy(hdr_val, hdr_name, hdr_name_len);
4219 hdr_val += hdr_name_len;
4220 *hdr_val++ = ':';
4221 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004222 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4223 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004224
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004225 if (old_o) {
4226 /* If this was a forwarded request, we must readjust the amount of
4227 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004228 * variations. Note that if the request was already scheduled for
4229 * forwarding, it had its req->sol pointing to the body, which
4230 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004231 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004232 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004233 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004234 }
4235
Mark Lamourinec2247f02012-01-04 13:02:01 -05004236 return 0;
4237}
4238
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239/* Terminate current transaction and prepare a new one. This is very tricky
4240 * right now but it works.
4241 */
4242void http_end_txn_clean_session(struct session *s)
4243{
4244 /* FIXME: We need a more portable way of releasing a backend's and a
4245 * server's connections. We need a safer way to reinitialize buffer
4246 * flags. We also need a more accurate method for computing per-request
4247 * data.
4248 */
4249 http_silent_debug(__LINE__, s);
4250
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004251 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004252 si_shutr(s->req->cons);
4253 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254
4255 http_silent_debug(__LINE__, s);
4256
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004257 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004259 if (unlikely(s->srv_conn))
4260 sess_change_server(s, NULL);
4261 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262
4263 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4264 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004265 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266
4267 if (s->txn.status) {
4268 int n;
4269
4270 n = s->txn.status / 100;
4271 if (n < 1 || n > 5)
4272 n = 0;
4273
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004274 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004275 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004276 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004277 s->fe->fe_counters.p.http.comp_rsp++;
4278 }
Willy Tarreau24657792010-02-26 10:30:28 +01004279 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004280 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004281 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004282 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004283 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004284 s->be->be_counters.p.http.comp_rsp++;
4285 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 }
4287
4288 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004289 s->logs.bytes_in -= s->req->buf->i;
4290 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291
4292 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004293 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 !(s->flags & SN_MONITOR) &&
4295 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4296 s->do_log(s);
4297 }
4298
4299 s->logs.accept_date = date; /* user-visible date for logging */
4300 s->logs.tv_accept = now; /* corrected date for internal use */
4301 tv_zero(&s->logs.tv_request);
4302 s->logs.t_queue = -1;
4303 s->logs.t_connect = -1;
4304 s->logs.t_data = -1;
4305 s->logs.t_close = 0;
4306 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4307 s->logs.srv_queue_size = 0; /* we will get this number soon */
4308
Willy Tarreau9b28e032012-10-12 23:49:43 +02004309 s->logs.bytes_in = s->req->total = s->req->buf->i;
4310 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311
4312 if (s->pend_pos)
4313 pendconn_free(s->pend_pos);
4314
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004315 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 if (s->flags & SN_CURR_SESS) {
4317 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004318 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004320 if (may_dequeue_tasks(objt_server(s->target), s->be))
4321 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 }
4323
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004324 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325
4326 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004327 si_release_endpoint(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004328 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004329 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 s->req->cons->exp = TICK_ETERNITY;
4331 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004332 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4333 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 +02004334 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 +01004335 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004336
4337 if (s->flags & SN_COMP_READY)
4338 s->comp_algo->end(&s->comp_ctx);
4339 s->comp_algo = NULL;
4340 s->flags &= ~SN_COMP_READY;
4341
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 s->txn.meth = 0;
4343 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004344 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004345 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346 s->req->cons->flags |= SI_FL_INDEP_STR;
4347
Willy Tarreau96e31212011-05-30 18:10:30 +02004348 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004349 s->req->flags |= CF_NEVER_WAIT;
4350 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004351 }
4352
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353 /* if the request buffer is not empty, it means we're
4354 * about to process another request, so send pending
4355 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004356 * Just don't do this if the buffer is close to be full,
4357 * because the request will wait for it to flush a little
4358 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004359 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004360 if (s->req->buf->i) {
4361 if (s->rep->buf->o &&
4362 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4363 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004364 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004365 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004366
4367 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004368 channel_auto_read(s->req);
4369 channel_auto_close(s->req);
4370 channel_auto_read(s->rep);
4371 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372
Willy Tarreau342b11c2010-11-24 16:22:09 +01004373 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004374 s->rep->analysers = 0;
4375
4376 http_silent_debug(__LINE__, s);
4377}
4378
4379
4380/* This function updates the request state machine according to the response
4381 * state machine and buffer flags. It returns 1 if it changes anything (flag
4382 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4383 * it is only used to find when a request/response couple is complete. Both
4384 * this function and its equivalent should loop until both return zero. It
4385 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4386 */
4387int http_sync_req_state(struct session *s)
4388{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004389 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004391 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 unsigned int old_state = txn->req.msg_state;
4393
4394 http_silent_debug(__LINE__, s);
4395 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4396 return 0;
4397
4398 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004399 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004400 * We can shut the read side unless we want to abort_on_close,
4401 * or we have a POST request. The issue with POST requests is
4402 * that some browsers still send a CRLF after the request, and
4403 * this CRLF must be read so that it does not remain in the kernel
4404 * buffers, otherwise a close could cause an RST on some systems
4405 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004406 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004407 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004408 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409
Willy Tarreau40f151a2012-12-20 12:10:09 +01004410 /* if the server closes the connection, we want to immediately react
4411 * and close the socket to save packets and syscalls.
4412 */
4413 chn->cons->flags |= SI_FL_NOHALF;
4414
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4416 goto wait_other_side;
4417
4418 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4419 /* The server has not finished to respond, so we
4420 * don't want to move in order not to upset it.
4421 */
4422 goto wait_other_side;
4423 }
4424
4425 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4426 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004427 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004429 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430 goto wait_other_side;
4431 }
4432
4433 /* When we get here, it means that both the request and the
4434 * response have finished receiving. Depending on the connection
4435 * mode, we'll have to wait for the last bytes to leave in either
4436 * direction, and sometimes for a close to be effective.
4437 */
4438
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004439 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4440 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4442 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004443 }
4444 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4445 /* Option forceclose is set, or either side wants to close,
4446 * let's enforce it now that we're not expecting any new
4447 * data to come. The caller knows the session is complete
4448 * once both states are CLOSED.
4449 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004450 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4451 channel_shutr_now(chn);
4452 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004454 }
4455 else {
4456 /* The last possible modes are keep-alive and tunnel. Since tunnel
4457 * mode does not set the body analyser, we can't reach this place
4458 * in tunnel mode, so we're left with keep-alive only.
4459 * This mode is currently not implemented, we switch to tunnel mode.
4460 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004462 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004463 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 }
4465
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004466 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004468 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004469
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004470 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471 txn->req.msg_state = HTTP_MSG_CLOSING;
4472 goto http_msg_closing;
4473 }
4474 else {
4475 txn->req.msg_state = HTTP_MSG_CLOSED;
4476 goto http_msg_closed;
4477 }
4478 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004479 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 }
4481
4482 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4483 http_msg_closing:
4484 /* nothing else to forward, just waiting for the output buffer
4485 * to be empty and for the shutw_now to take effect.
4486 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004487 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 txn->req.msg_state = HTTP_MSG_CLOSED;
4489 goto http_msg_closed;
4490 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004491 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 txn->req.msg_state = HTTP_MSG_ERROR;
4493 goto wait_other_side;
4494 }
4495 }
4496
4497 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4498 http_msg_closed:
4499 goto wait_other_side;
4500 }
4501
4502 wait_other_side:
4503 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004504 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505}
4506
4507
4508/* This function updates the response state machine according to the request
4509 * state machine and buffer flags. It returns 1 if it changes anything (flag
4510 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4511 * it is only used to find when a request/response couple is complete. Both
4512 * this function and its equivalent should loop until both return zero. It
4513 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4514 */
4515int http_sync_res_state(struct session *s)
4516{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004517 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004518 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004519 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 unsigned int old_state = txn->rsp.msg_state;
4521
4522 http_silent_debug(__LINE__, s);
4523 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4524 return 0;
4525
4526 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4527 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004528 * still monitor the server connection for a possible close
4529 * while the request is being uploaded, so we don't disable
4530 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004532 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533
4534 if (txn->req.msg_state == HTTP_MSG_ERROR)
4535 goto wait_other_side;
4536
4537 if (txn->req.msg_state < HTTP_MSG_DONE) {
4538 /* The client seems to still be sending data, probably
4539 * because we got an error response during an upload.
4540 * We have the choice of either breaking the connection
4541 * or letting it pass through. Let's do the later.
4542 */
4543 goto wait_other_side;
4544 }
4545
4546 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4547 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004550 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 goto wait_other_side;
4552 }
4553
4554 /* When we get here, it means that both the request and the
4555 * response have finished receiving. Depending on the connection
4556 * mode, we'll have to wait for the last bytes to leave in either
4557 * direction, and sometimes for a close to be effective.
4558 */
4559
4560 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4561 /* Server-close mode : shut read and wait for the request
4562 * side to close its output buffer. The caller will detect
4563 * when we're in DONE and the other is in CLOSED and will
4564 * catch that for the final cleanup.
4565 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4567 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004569 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4570 /* Option forceclose is set, or either side wants to close,
4571 * let's enforce it now that we're not expecting any new
4572 * data to come. The caller knows the session is complete
4573 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004575 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4576 channel_shutr_now(chn);
4577 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004578 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579 }
4580 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004581 /* The last possible modes are keep-alive and tunnel. Since tunnel
4582 * mode does not set the body analyser, we can't reach this place
4583 * in tunnel mode, so we're left with keep-alive only.
4584 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004585 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004586 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004588 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 }
4590
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004591 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004593 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004594 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4595 goto http_msg_closing;
4596 }
4597 else {
4598 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4599 goto http_msg_closed;
4600 }
4601 }
4602 goto wait_other_side;
4603 }
4604
4605 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4606 http_msg_closing:
4607 /* nothing else to forward, just waiting for the output buffer
4608 * to be empty and for the shutw_now to take effect.
4609 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4612 goto http_msg_closed;
4613 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004614 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004616 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004617 if (objt_server(s->target))
4618 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 goto wait_other_side;
4620 }
4621 }
4622
4623 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4624 http_msg_closed:
4625 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 bi_erase(chn);
4627 channel_auto_close(chn);
4628 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 goto wait_other_side;
4630 }
4631
4632 wait_other_side:
4633 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004634 /* We force the response to leave immediately if we're waiting for the
4635 * other side, since there is no pending shutdown to push it out.
4636 */
4637 if (!channel_is_empty(chn))
4638 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004639 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004640}
4641
4642
4643/* Resync the request and response state machines. Return 1 if either state
4644 * changes.
4645 */
4646int http_resync_states(struct session *s)
4647{
4648 struct http_txn *txn = &s->txn;
4649 int old_req_state = txn->req.msg_state;
4650 int old_res_state = txn->rsp.msg_state;
4651
4652 http_silent_debug(__LINE__, s);
4653 http_sync_req_state(s);
4654 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004655 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 if (!http_sync_res_state(s))
4657 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004658 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 if (!http_sync_req_state(s))
4660 break;
4661 }
4662 http_silent_debug(__LINE__, s);
4663 /* OK, both state machines agree on a compatible state.
4664 * There are a few cases we're interested in :
4665 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4666 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4667 * directions, so let's simply disable both analysers.
4668 * - HTTP_MSG_CLOSED on the response only means we must abort the
4669 * request.
4670 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4671 * with server-close mode means we've completed one request and we
4672 * must re-initialize the server connection.
4673 */
4674
4675 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4676 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4677 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4678 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4679 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004680 channel_auto_close(s->req);
4681 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004683 channel_auto_close(s->rep);
4684 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004686 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4687 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004688 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004689 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004690 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004691 channel_auto_close(s->rep);
4692 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004693 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004694 channel_abort(s->req);
4695 channel_auto_close(s->req);
4696 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004697 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004698 }
4699 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4700 txn->rsp.msg_state == HTTP_MSG_DONE &&
4701 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4702 /* server-close: terminate this server connection and
4703 * reinitialize a fresh-new transaction.
4704 */
4705 http_end_txn_clean_session(s);
4706 }
4707
4708 http_silent_debug(__LINE__, s);
4709 return txn->req.msg_state != old_req_state ||
4710 txn->rsp.msg_state != old_res_state;
4711}
4712
Willy Tarreaud98cf932009-12-27 22:54:55 +01004713/* This function is an analyser which forwards request body (including chunk
4714 * sizes if any). It is called as soon as we must forward, even if we forward
4715 * zero byte. The only situation where it must not be called is when we're in
4716 * tunnel mode and we want to forward till the close. It's used both to forward
4717 * remaining data and to resync after end of body. It expects the msg_state to
4718 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4719 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004720 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004721 * bytes of pending data + the headers if not already done (between sol and sov).
4722 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004723 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004724int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004725{
4726 struct http_txn *txn = &s->txn;
4727 struct http_msg *msg = &s->txn.req;
4728
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004729 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4730 return 0;
4731
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004732 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004733 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004734 /* Output closed while we were sending data. We must abort and
4735 * wake the other side up.
4736 */
4737 msg->msg_state = HTTP_MSG_ERROR;
4738 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004739 return 1;
4740 }
4741
Willy Tarreau4fe41902010-06-07 22:27:41 +02004742 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004743 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744
4745 /* Note that we don't have to send 100-continue back because we don't
4746 * need the data to complete our job, and it's up to the server to
4747 * decide whether to return 100, 417 or anything else in return of
4748 * an "Expect: 100-continue" header.
4749 */
4750
4751 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004752 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004753 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004754 * is still null. We must save the body in msg->next because it
4755 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004756 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004757 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004758
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004759 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004760 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004761 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004762 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 }
4764
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004766 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004767
Willy Tarreau610ecce2010-01-04 21:15:02 +01004768 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004769 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004770 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004771 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004772 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004773 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004774 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004775 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004776 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004777
Willy Tarreaucaabe412010-01-03 23:08:28 +01004778 if (msg->msg_state == HTTP_MSG_DATA) {
4779 /* must still forward */
4780 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004781 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004782
4783 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004784 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004785 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004786 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004787 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004788 }
4789 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004790 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004791 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004792 * TRAILERS state.
4793 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004794 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004795
Willy Tarreau54d23df2012-10-25 19:04:45 +02004796 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004797 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004798 else if (ret < 0) {
4799 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004800 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004801 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004803 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004805 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004806 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004808 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004809
4810 if (ret == 0)
4811 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004812 else if (ret < 0) {
4813 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004814 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004815 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004817 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004818 /* we're in MSG_CHUNK_SIZE now */
4819 }
4820 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004821 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822
4823 if (ret == 0)
4824 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004825 else if (ret < 0) {
4826 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004827 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004828 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004830 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 /* we're in HTTP_MSG_DONE now */
4832 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004834 int old_state = msg->msg_state;
4835
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004837 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004838 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4839 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004840 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 if (http_resync_states(s)) {
4842 /* some state changes occurred, maybe the analyser
4843 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004844 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004846 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004847 /* request errors are most likely due to
4848 * the server aborting the transfer.
4849 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004850 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004851 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004852 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004853 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004855 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004857 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004858
4859 /* If "option abortonclose" is set on the backend, we
4860 * want to monitor the client's connection and forward
4861 * any shutdown notification to the server, which will
4862 * decide whether to close or to go on processing the
4863 * request.
4864 */
4865 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004866 channel_auto_read(req);
4867 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004868 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004869 else if (s->txn.meth == HTTP_METH_POST) {
4870 /* POST requests may require to read extra CRLF
4871 * sent by broken browsers and which could cause
4872 * an RST to be sent upon close on some systems
4873 * (eg: Linux).
4874 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004875 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004876 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004877
Willy Tarreau610ecce2010-01-04 21:15:02 +01004878 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004879 }
4880 }
4881
Willy Tarreaud98cf932009-12-27 22:54:55 +01004882 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004883 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004884 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004885 if (!(s->flags & SN_ERR_MASK))
4886 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004887 if (!(s->flags & SN_FINST_MASK)) {
4888 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4889 s->flags |= SN_FINST_H;
4890 else
4891 s->flags |= SN_FINST_D;
4892 }
4893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004894 s->fe->fe_counters.cli_aborts++;
4895 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004896 if (objt_server(s->target))
4897 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004898
4899 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004900 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004901
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004902 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004903 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004904 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004906 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004907 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004908 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004909 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004910 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004911
Willy Tarreau5c620922011-05-11 19:56:11 +02004912 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004913 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004914 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004915 * modes are already handled by the stream sock layer. We must not do
4916 * this in content-length mode because it could present the MSG_MORE
4917 * flag with the last block of forwarded data, which would cause an
4918 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004919 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004920 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004921 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004922
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004924 return 0;
4925
Willy Tarreaud98cf932009-12-27 22:54:55 +01004926 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004927 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004928 if (s->listener->counters)
4929 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004930 return_bad_req_stats_ok:
4931 txn->req.msg_state = HTTP_MSG_ERROR;
4932 if (txn->status) {
4933 /* Note: we don't send any error if some data were already sent */
4934 stream_int_retnclose(req->prod, NULL);
4935 } else {
4936 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004937 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004938 }
4939 req->analysers = 0;
4940 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004941
4942 if (!(s->flags & SN_ERR_MASK))
4943 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004944 if (!(s->flags & SN_FINST_MASK)) {
4945 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4946 s->flags |= SN_FINST_H;
4947 else
4948 s->flags |= SN_FINST_D;
4949 }
4950 return 0;
4951
4952 aborted_xfer:
4953 txn->req.msg_state = HTTP_MSG_ERROR;
4954 if (txn->status) {
4955 /* Note: we don't send any error if some data were already sent */
4956 stream_int_retnclose(req->prod, NULL);
4957 } else {
4958 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004959 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004960 }
4961 req->analysers = 0;
4962 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4963
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004964 s->fe->fe_counters.srv_aborts++;
4965 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004966 if (objt_server(s->target))
4967 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004968
4969 if (!(s->flags & SN_ERR_MASK))
4970 s->flags |= SN_ERR_SRVCL;
4971 if (!(s->flags & SN_FINST_MASK)) {
4972 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4973 s->flags |= SN_FINST_H;
4974 else
4975 s->flags |= SN_FINST_D;
4976 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004977 return 0;
4978}
4979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4981 * processing can continue on next analysers, or zero if it either needs more
4982 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4983 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4984 * when it has nothing left to do, and may remove any analyser when it wants to
4985 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004986 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004987int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004988{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004989 struct http_txn *txn = &s->txn;
4990 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004991 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004992 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004994 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004995
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004996 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 +02004997 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004998 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004999 rep,
5000 rep->rex, rep->wex,
5001 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005002 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005003 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005004
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005005 /*
5006 * Now parse the partial (or complete) lines.
5007 * We will check the response syntax, and also join multi-line
5008 * headers. An index of all the lines will be elaborated while
5009 * parsing.
5010 *
5011 * For the parsing, we use a 28 states FSM.
5012 *
5013 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005014 * rep->buf->p = beginning of response
5015 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5016 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005017 * msg->eol = end of current header or line (LF or CRLF)
5018 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005019 */
5020
Willy Tarreau83e3af02009-12-28 17:39:57 +01005021 /* There's a protected area at the end of the buffer for rewriting
5022 * purposes. We don't want to start to parse the request if the
5023 * protected area is affected, because we may have to move processed
5024 * data later, which is much more complicated.
5025 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005026 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005027 if (unlikely(!channel_reserved(rep))) {
5028 /* some data has still not left the buffer, wake us once that's done */
5029 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5030 goto abort_response;
5031 channel_dont_close(rep);
5032 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5033 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005034 }
5035
Willy Tarreau379357a2013-06-08 12:55:46 +02005036 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5037 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5038 buffer_slow_realign(rep->buf);
5039
Willy Tarreau9b28e032012-10-12 23:49:43 +02005040 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005041 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005042 }
5043
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005044 /* 1: we might have to print this header in debug mode */
5045 if (unlikely((global.mode & MODE_DEBUG) &&
5046 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005047 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005048 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005049
Willy Tarreau9b28e032012-10-12 23:49:43 +02005050 sol = rep->buf->p;
5051 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 sol += hdr_idx_first_pos(&txn->hdr_idx);
5055 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 while (cur_idx) {
5058 eol = sol + txn->hdr_idx.v[cur_idx].len;
5059 debug_hdr("srvhdr", s, sol, eol);
5060 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5061 cur_idx = txn->hdr_idx.v[cur_idx].next;
5062 }
5063 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 /*
5066 * Now we quickly check if we have found a full valid response.
5067 * If not so, we check the FD and buffer states before leaving.
5068 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005069 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 * responses are checked first.
5071 *
5072 * Depending on whether the client is still there or not, we
5073 * may send an error response back or not. Note that normally
5074 * we should only check for HTTP status there, and check I/O
5075 * errors somewhere else.
5076 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077
Willy Tarreau655dce92009-11-08 13:10:58 +01005078 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 /* Invalid response */
5080 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5081 /* we detected a parsing error. We want to archive this response
5082 * in the dedicated proxy area for later troubleshooting.
5083 */
5084 hdr_response_bad:
5085 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005086 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005088 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005089 if (objt_server(s->target)) {
5090 objt_server(s->target)->counters.failed_resp++;
5091 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005092 }
Willy Tarreau64648412010-03-05 10:41:54 +01005093 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005094 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 rep->analysers = 0;
5096 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005097 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005098 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005099 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100
5101 if (!(s->flags & SN_ERR_MASK))
5102 s->flags |= SN_ERR_PRXCOND;
5103 if (!(s->flags & SN_FINST_MASK))
5104 s->flags |= SN_FINST_H;
5105
5106 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005108
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005109 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005110 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005111 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005112 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005113 goto hdr_response_bad;
5114 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005117 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005119 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005120
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005121 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005122 if (objt_server(s->target)) {
5123 objt_server(s->target)->counters.failed_resp++;
5124 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005125 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005126
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005127 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 rep->analysers = 0;
5129 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005130 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005131 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005132 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 if (!(s->flags & SN_ERR_MASK))
5135 s->flags |= SN_ERR_SRVCL;
5136 if (!(s->flags & SN_FINST_MASK))
5137 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005138 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005142 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005144 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005145
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005146 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005147 if (objt_server(s->target)) {
5148 objt_server(s->target)->counters.failed_resp++;
5149 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005150 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005151
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005152 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 rep->analysers = 0;
5154 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005155 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005156 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005157 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005158
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 if (!(s->flags & SN_ERR_MASK))
5160 s->flags |= SN_ERR_SRVTO;
5161 if (!(s->flags & SN_FINST_MASK))
5162 s->flags |= SN_FINST_H;
5163 return 0;
5164 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005165
Willy Tarreauf003d372012-11-26 13:35:37 +01005166 /* client abort with an abortonclose */
5167 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5168 s->fe->fe_counters.cli_aborts++;
5169 s->be->be_counters.cli_aborts++;
5170 if (objt_server(s->target))
5171 objt_server(s->target)->counters.cli_aborts++;
5172
5173 rep->analysers = 0;
5174 channel_auto_close(rep);
5175
5176 txn->status = 400;
5177 bi_erase(rep);
5178 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5179
5180 if (!(s->flags & SN_ERR_MASK))
5181 s->flags |= SN_ERR_CLICL;
5182 if (!(s->flags & SN_FINST_MASK))
5183 s->flags |= SN_FINST_H;
5184
5185 /* process_session() will take care of the error */
5186 return 0;
5187 }
5188
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005189 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005190 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005191 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005192 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005193
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005194 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 if (objt_server(s->target)) {
5196 objt_server(s->target)->counters.failed_resp++;
5197 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005198 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005199
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005200 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201 rep->analysers = 0;
5202 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005203 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005204 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005205 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005206
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207 if (!(s->flags & SN_ERR_MASK))
5208 s->flags |= SN_ERR_SRVCL;
5209 if (!(s->flags & SN_FINST_MASK))
5210 s->flags |= SN_FINST_H;
5211 return 0;
5212 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005213
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005214 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005215 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005216 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005217 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005218
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005219 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005221 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005222
5223 if (!(s->flags & SN_ERR_MASK))
5224 s->flags |= SN_ERR_CLICL;
5225 if (!(s->flags & SN_FINST_MASK))
5226 s->flags |= SN_FINST_H;
5227
5228 /* process_session() will take care of the error */
5229 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005230 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005231
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005232 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 return 0;
5234 }
5235
5236 /* More interesting part now : we know that we have a complete
5237 * response which at least looks like HTTP. We have an indicator
5238 * of each header's length, so we can parse them quickly.
5239 */
5240
5241 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005242 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005243
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005244 /*
5245 * 1: get the status code
5246 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005247 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005248 if (n < 1 || n > 5)
5249 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005250 /* when the client triggers a 4xx from the server, it's most often due
5251 * to a missing object or permission. These events should be tracked
5252 * because if they happen often, it may indicate a brute force or a
5253 * vulnerability scan.
5254 */
5255 if (n == 4)
5256 session_inc_http_err_ctr(s);
5257
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005258 if (objt_server(s->target))
5259 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005260
Willy Tarreau5b154472009-12-21 20:11:07 +01005261 /* check if the response is HTTP/1.1 or above */
5262 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005263 ((rep->buf->p[5] > '1') ||
5264 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005265 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005266
5267 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005268 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 +01005269
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005270 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005271 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005272
Willy Tarreau9b28e032012-10-12 23:49:43 +02005273 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274
Willy Tarreau39650402010-03-15 19:44:39 +01005275 /* Adjust server's health based on status code. Note: status codes 501
5276 * and 505 are triggered on demand by client request, so we must not
5277 * count them as server failures.
5278 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005279 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005280 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005281 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005282 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005283 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005284 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005285
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 /*
5287 * 2: check for cacheability.
5288 */
5289
5290 switch (txn->status) {
5291 case 200:
5292 case 203:
5293 case 206:
5294 case 300:
5295 case 301:
5296 case 410:
5297 /* RFC2616 @13.4:
5298 * "A response received with a status code of
5299 * 200, 203, 206, 300, 301 or 410 MAY be stored
5300 * by a cache (...) unless a cache-control
5301 * directive prohibits caching."
5302 *
5303 * RFC2616 @9.5: POST method :
5304 * "Responses to this method are not cacheable,
5305 * unless the response includes appropriate
5306 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005309 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5311 break;
5312 default:
5313 break;
5314 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005315
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005316 /*
5317 * 3: we may need to capture headers
5318 */
5319 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005320 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005321 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005322 txn->rsp.cap, s->fe->rsp_cap);
5323
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005324 /* 4: determine the transfer-length.
5325 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5326 * the presence of a message-body in a RESPONSE and its transfer length
5327 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005328 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005329 * All responses to the HEAD request method MUST NOT include a
5330 * message-body, even though the presence of entity-header fields
5331 * might lead one to believe they do. All 1xx (informational), 204
5332 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5333 * message-body. All other responses do include a message-body,
5334 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005335 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005336 * 1. Any response which "MUST NOT" include a message-body (such as the
5337 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5338 * always terminated by the first empty line after the header fields,
5339 * regardless of the entity-header fields present in the message.
5340 *
5341 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5342 * the "chunked" transfer-coding (Section 6.2) is used, the
5343 * transfer-length is defined by the use of this transfer-coding.
5344 * If a Transfer-Encoding header field is present and the "chunked"
5345 * transfer-coding is not present, the transfer-length is defined by
5346 * the sender closing the connection.
5347 *
5348 * 3. If a Content-Length header field is present, its decimal value in
5349 * OCTETs represents both the entity-length and the transfer-length.
5350 * If a message is received with both a Transfer-Encoding header
5351 * field and a Content-Length header field, the latter MUST be ignored.
5352 *
5353 * 4. If the message uses the media type "multipart/byteranges", and
5354 * the transfer-length is not otherwise specified, then this self-
5355 * delimiting media type defines the transfer-length. This media
5356 * type MUST NOT be used unless the sender knows that the recipient
5357 * can parse it; the presence in a request of a Range header with
5358 * multiple byte-range specifiers from a 1.1 client implies that the
5359 * client can parse multipart/byteranges responses.
5360 *
5361 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005362 */
5363
5364 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005365 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005366 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005367 * FIXME: should we parse anyway and return an error on chunked encoding ?
5368 */
5369 if (txn->meth == HTTP_METH_HEAD ||
5370 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005371 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005372 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005373 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005374 goto skip_content_length;
5375 }
5376
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005377 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005378 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005379 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005380 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005381 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005382 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5383 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005384 /* bad transfer-encoding (chunked followed by something else) */
5385 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005387 break;
5388 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005389 }
5390
5391 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5392 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005393 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005394 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005395 signed long long cl;
5396
Willy Tarreauad14f752011-09-02 20:33:27 +02005397 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005398 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005399 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005400 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005401
Willy Tarreauad14f752011-09-02 20:33:27 +02005402 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005403 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005404 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005405 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005406
Willy Tarreauad14f752011-09-02 20:33:27 +02005407 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005408 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005410 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005411
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005412 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005413 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005414 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005415 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005416
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005417 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005418 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005419 }
5420
William Lallemand82fe75c2012-10-23 10:25:10 +02005421 if (s->fe->comp || s->be->comp)
5422 select_compression_response_header(s, rep->buf);
5423
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005424 /* FIXME: we should also implement the multipart/byterange method.
5425 * For now on, we resort to close mode in this case (unknown length).
5426 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005427skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005428
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429 /* end of job, return OK */
5430 rep->analysers &= ~an_bit;
5431 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005432 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 return 1;
5434}
5435
5436/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005437 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5438 * and updates t->rep->analysers. It might make sense to explode it into several
5439 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005440 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005441int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442{
5443 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 struct http_msg *msg = &txn->rsp;
5445 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005446 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005447 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005448
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005449 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 +02005450 now_ms, __FUNCTION__,
5451 t,
5452 rep,
5453 rep->rex, rep->wex,
5454 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005455 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005456 rep->analysers);
5457
Willy Tarreau655dce92009-11-08 13:10:58 +01005458 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005459 return 0;
5460
5461 rep->analysers &= ~an_bit;
5462 rep->analyse_exp = TICK_ETERNITY;
5463
Willy Tarreau5b154472009-12-21 20:11:07 +01005464 /* Now we have to check if we need to modify the Connection header.
5465 * This is more difficult on the response than it is on the request,
5466 * because we can have two different HTTP versions and we don't know
5467 * how the client will interprete a response. For instance, let's say
5468 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5469 * HTTP/1.1 response without any header. Maybe it will bound itself to
5470 * HTTP/1.0 because it only knows about it, and will consider the lack
5471 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5472 * the lack of header as a keep-alive. Thus we will use two flags
5473 * indicating how a request MAY be understood by the client. In case
5474 * of multiple possibilities, we'll fix the header to be explicit. If
5475 * ambiguous cases such as both close and keepalive are seen, then we
5476 * will fall back to explicit close. Note that we won't take risks with
5477 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005478 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005479 */
5480
Willy Tarreaudc008c52010-02-01 16:20:08 +01005481 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5482 txn->status == 101)) {
5483 /* Either we've established an explicit tunnel, or we're
5484 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005485 * to understand the next protocols. We have to switch to tunnel
5486 * mode, so that we transfer the request and responses then let
5487 * this protocol pass unmodified. When we later implement specific
5488 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005489 * header which contains information about that protocol for
5490 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005491 */
5492 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5493 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005494 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5495 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5496 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005497 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005498
Willy Tarreau60466522010-01-18 19:08:45 +01005499 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005500 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005501 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5502 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005503
Willy Tarreau60466522010-01-18 19:08:45 +01005504 /* now adjust header transformations depending on current state */
5505 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5506 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5507 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005508 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005509 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005510 }
Willy Tarreau60466522010-01-18 19:08:45 +01005511 else { /* SCL / KAL */
5512 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005513 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005514 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005515 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005516
Willy Tarreau60466522010-01-18 19:08:45 +01005517 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005518 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005519
Willy Tarreau60466522010-01-18 19:08:45 +01005520 /* Some keep-alive responses are converted to Server-close if
5521 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005522 */
Willy Tarreau60466522010-01-18 19:08:45 +01005523 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5524 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005525 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005527 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005528 }
5529
Willy Tarreau7959a552013-09-23 16:44:27 +02005530 /* we want to have the response time before we start processing it */
5531 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5532
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005533 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005534 /*
5535 * 3: we will have to evaluate the filters.
5536 * As opposed to version 1.2, now they will be evaluated in the
5537 * filters order and not in the header order. This means that
5538 * each filter has to be validated among all headers.
5539 *
5540 * Filters are tried with ->be first, then with ->fe if it is
5541 * different from ->be.
5542 */
5543
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005544 cur_proxy = t->be;
5545 while (1) {
5546 struct proxy *rule_set = cur_proxy;
5547
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005548 /* evaluate http-response rules */
5549 if (!http_res_last_rule)
5550 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5551
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005552 /* try headers filters */
5553 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005554 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005555 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005556 if (objt_server(t->target)) {
5557 objt_server(t->target)->counters.failed_resp++;
5558 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005559 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005560 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005561 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005562 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005563 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005564 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005565 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005566 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005567 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005568 if (!(t->flags & SN_ERR_MASK))
5569 t->flags |= SN_ERR_PRXCOND;
5570 if (!(t->flags & SN_FINST_MASK))
5571 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005572 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005573 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005574 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005575
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005576 /* has the response been denied ? */
5577 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005578 if (objt_server(t->target))
5579 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005580
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005581 t->be->be_counters.denied_resp++;
5582 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005583 if (t->listener->counters)
5584 t->listener->counters->denied_resp++;
5585
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005586 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005587 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005588
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005589 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005590 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005591 if (txn->status < 200)
5592 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005593 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005594 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005595 ret = acl_pass(ret);
5596 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5597 ret = !ret;
5598 if (!ret)
5599 continue;
5600 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005601 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005602 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005603 }
5604
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005605 /* check whether we're already working on the frontend */
5606 if (cur_proxy == t->fe)
5607 break;
5608 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005609 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005610
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005611 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005612 * We may be facing a 100-continue response, in which case this
5613 * is not the right response, and we're waiting for the next one.
5614 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005615 * next one.
5616 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005617 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005618 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005619 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005620 msg->msg_state = HTTP_MSG_RPBEFORE;
5621 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005622 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005623 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5624 return 1;
5625 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005626 else if (unlikely(txn->status < 200))
5627 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005628
5629 /* we don't have any 1xx status code now */
5630
5631 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005632 * 4: check for server cookie.
5633 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005634 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5635 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005636 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005637
Willy Tarreaubaaee002006-06-26 02:48:02 +02005638
Willy Tarreaua15645d2007-03-18 16:22:39 +01005639 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005640 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005641 */
Willy Tarreau67402132012-05-31 20:40:20 +02005642 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005643 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005645 /*
5646 * 6: add server cookie in the response if needed
5647 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005648 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005649 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005650 (!(t->flags & SN_DIRECT) ||
5651 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5652 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5653 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5654 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005655 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005656 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005657 /* the server is known, it's not the one the client requested, or the
5658 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005659 * insert a set-cookie here, except if we want to insert only on POST
5660 * requests and this one isn't. Note that servers which don't have cookies
5661 * (eg: some backup servers) will return a full cookie removal request.
5662 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005663 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005664 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005665 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5666 t->be->cookie_name);
5667 }
5668 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005669 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005670
5671 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5672 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005673 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5674 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5675 trash.len += 5;
5676
Willy Tarreauef4f3912010-10-07 21:00:29 +02005677 if (t->be->cookie_maxlife) {
5678 /* emit first_date, which is either the original one or
5679 * the current date.
5680 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005681 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005682 s30tob64(txn->cookie_first_date ?
5683 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005684 (date.tv_sec+3) >> 2, trash.str + trash.len);
5685 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005686 }
5687 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005688 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005691 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005692 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005693
Willy Tarreau4992dd22012-05-31 21:02:17 +02005694 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005695 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005696
5697 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005698 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005699
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005700 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005701 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005702
Willy Tarreauf1348312010-10-07 15:54:11 +02005703 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005704 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005705 /* the server did not change, only the date was updated */
5706 txn->flags |= TX_SCK_UPDATED;
5707 else
5708 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005709
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005710 /* Here, we will tell an eventual cache on the client side that we don't
5711 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5712 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5713 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5714 */
Willy Tarreau67402132012-05-31 20:40:20 +02005715 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005716
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005717 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5718
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005719 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005720 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005721 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005722 }
5723 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005724
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005725 /*
5726 * 7: check if result will be cacheable with a cookie.
5727 * We'll block the response if security checks have caught
5728 * nasty things such as a cacheable cookie.
5729 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005730 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5731 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005732 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005733
5734 /* we're in presence of a cacheable response containing
5735 * a set-cookie header. We'll block it as requested by
5736 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005737 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005738 if (objt_server(t->target))
5739 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005740
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005741 t->be->be_counters.denied_resp++;
5742 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005743 if (t->listener->counters)
5744 t->listener->counters->denied_resp++;
5745
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005747 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 send_log(t->be, LOG_ALERT,
5749 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005750 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 goto return_srv_prx_502;
5752 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005753
5754 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005755 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005756 * If an "Upgrade" token is found, the header is left untouched in order
5757 * not to have to deal with some client bugs : some of them fail an upgrade
5758 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005759 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005760 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5761 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5762 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005763 unsigned int want_flags = 0;
5764
5765 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5766 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5767 /* we want a keep-alive response here. Keep-alive header
5768 * required if either side is not 1.1.
5769 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005770 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005771 want_flags |= TX_CON_KAL_SET;
5772 }
5773 else {
5774 /* we want a close response here. Close header required if
5775 * the server is 1.1, regardless of the client.
5776 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005777 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005778 want_flags |= TX_CON_CLO_SET;
5779 }
5780
5781 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005782 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005785 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005786 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005787 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005788 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005789
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005790 /*************************************************************
5791 * OK, that's finished for the headers. We have done what we *
5792 * could. Let's switch to the DATA state. *
5793 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005794
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005795 /* if the user wants to log as soon as possible, without counting
5796 * bytes from the server, then this is the right moment. We have
5797 * to temporarily assign bytes_out to log what we currently have.
5798 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005799 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005800 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5801 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005802 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005803 t->logs.bytes_out = 0;
5804 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005805
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 /* Note: we must not try to cheat by jumping directly to DATA,
5807 * otherwise we would not let the client side wake up.
5808 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005809
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005810 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005811 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005812 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005813}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005814
Willy Tarreaud98cf932009-12-27 22:54:55 +01005815/* This function is an analyser which forwards response body (including chunk
5816 * sizes if any). It is called as soon as we must forward, even if we forward
5817 * zero byte. The only situation where it must not be called is when we're in
5818 * tunnel mode and we want to forward till the close. It's used both to forward
5819 * remaining data and to resync after end of body. It expects the msg_state to
5820 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5821 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005822 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005823 * bytes of pending data + the headers if not already done (between sol and sov).
5824 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005825 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005826int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005827{
5828 struct http_txn *txn = &s->txn;
5829 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005830 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005831 static struct buffer *tmpbuf = NULL;
5832 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005833 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005834 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005835
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005836 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5837 return 0;
5838
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005839 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005840 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005841 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005842 /* Output closed while we were sending data. We must abort and
5843 * wake the other side up.
5844 */
5845 msg->msg_state = HTTP_MSG_ERROR;
5846 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005847 return 1;
5848 }
5849
Willy Tarreau4fe41902010-06-07 22:27:41 +02005850 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005851 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005852
William Lallemand82fe75c2012-10-23 10:25:10 +02005853 /* this is the first time we need the compression buffer */
5854 if (s->comp_algo != NULL && tmpbuf == NULL) {
5855 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5856 goto aborted_xfer; /* no memory */
5857 }
5858
Willy Tarreaud98cf932009-12-27 22:54:55 +01005859 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005860 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005861 * rep->buf.p still points to the beginning of the message and msg->sol
5862 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005863 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005864 channel_forward(res, msg->sov);
5865 msg->next = 0;
5866 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005867
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005868 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005869 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005870 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005871 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005872 }
5873
William Lallemand82fe75c2012-10-23 10:25:10 +02005874 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005875 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005876 if (ret < 0)
5877 goto missing_data; /* not enough spaces in buffers */
5878 compressing = 1;
5879 }
5880
Willy Tarreaud98cf932009-12-27 22:54:55 +01005881 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005882 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005883 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005884 if (s->comp_algo == NULL) {
5885 bytes = msg->sov - msg->sol;
5886 if (msg->chunk_len || bytes) {
5887 msg->sol = msg->sov;
5888 msg->next -= bytes; /* will be forwarded */
5889 msg->chunk_len += bytes;
5890 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5891 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005892 }
5893
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005894 switch (msg->msg_state - HTTP_MSG_DATA) {
5895 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005896 if (compressing) {
5897 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5898 if (ret < 0)
5899 goto aborted_xfer;
5900 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005901
5902 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005903 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005904
5905 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005906 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005907 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005908 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005909 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005910 if (compressing && consumed_data) {
5911 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5912 compressing = 0;
5913 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005914 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005915 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005916 /* fall through for HTTP_MSG_CHUNK_CRLF */
5917
5918 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5919 /* we want the CRLF after the data */
5920
5921 ret = http_skip_chunk_crlf(msg);
5922 if (ret == 0)
5923 goto missing_data;
5924 else if (ret < 0) {
5925 if (msg->err_pos >= 0)
5926 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5927 goto return_bad_res;
5928 }
5929 /* skipping data in buffer for compression */
5930 if (compressing) {
5931 b_adv(res->buf, msg->next);
5932 msg->next = 0;
5933 msg->sov = 0;
5934 msg->sol = 0;
5935 }
5936 /* we're in MSG_CHUNK_SIZE now, fall through */
5937
5938 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005939 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005940 * set ->sov and ->next to point to the body and switch to DATA or
5941 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005943
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005944 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005945 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005947 else if (ret < 0) {
5948 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005949 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005950 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005951 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005952 if (compressing) {
5953 if (likely(msg->chunk_len > 0)) {
5954 /* skipping data if we are in compression mode */
5955 b_adv(res->buf, msg->next);
5956 msg->next = 0;
5957 msg->sov = 0;
5958 msg->sol = 0;
5959 } else {
5960 if (consumed_data) {
5961 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5962 compressing = 0;
5963 }
5964 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005965 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005966 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005967 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005968
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005969 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5970 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005971 if (ret == 0)
5972 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005973 else if (ret < 0) {
5974 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005975 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005976 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005977 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005978 if (s->comp_algo != NULL) {
5979 /* forwarding trailers */
5980 channel_forward(res, msg->next);
5981 msg->next = 0;
5982 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005983 /* we're in HTTP_MSG_DONE now, but we might still have
5984 * some data pending, so let's loop over once.
5985 */
5986 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005987
5988 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005989 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005990
5991 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005992 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005993 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5994 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005995 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005996 if (http_resync_states(s)) {
5997 http_silent_debug(__LINE__, s);
5998 /* some state changes occurred, maybe the analyser
5999 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006000 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006001 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006002 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006003 /* response errors are most likely due to
6004 * the client aborting the transfer.
6005 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006006 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006007 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006008 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006009 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006010 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006011 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006012 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006013 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006014 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006015 }
6016 }
6017
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006019 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006020 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6021 compressing = 0;
6022 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006023
6024 if (res->flags & CF_SHUTW)
6025 goto aborted_xfer;
6026
6027 /* stop waiting for data if the input is closed before the end. If the
6028 * client side was already closed, it means that the client has aborted,
6029 * so we don't want to count this as a server abort. Otherwise it's a
6030 * server abort.
6031 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006032 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006033 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6034 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006035 if (!(s->flags & SN_ERR_MASK))
6036 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006037 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006038 if (objt_server(s->target))
6039 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006040 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006041 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006042
Willy Tarreau40dba092010-03-04 18:14:51 +01006043 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006044 if (!s->req->analysers)
6045 goto return_bad_res;
6046
Willy Tarreauea953162012-05-18 23:41:28 +02006047 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006048 if (s->comp_algo == NULL) {
6049 bytes = msg->sov - msg->sol;
6050 if (msg->chunk_len || bytes) {
6051 msg->sol = msg->sov;
6052 msg->next -= bytes; /* will be forwarded */
6053 msg->chunk_len += bytes;
6054 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6055 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006056 }
6057
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006058 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006059 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006060 * Similarly, with keep-alive on the client side, we don't want to forward a
6061 * close.
6062 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006063 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006064 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6065 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006066 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006067
Willy Tarreau5c620922011-05-11 19:56:11 +02006068 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006069 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006070 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006071 * modes are already handled by the stream sock layer. We must not do
6072 * this in content-length mode because it could present the MSG_MORE
6073 * flag with the last block of forwarded data, which would cause an
6074 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006075 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006076 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006077 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006078
Willy Tarreaud98cf932009-12-27 22:54:55 +01006079 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006080 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006081 return 0;
6082
Willy Tarreau40dba092010-03-04 18:14:51 +01006083 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006084 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006085 if (objt_server(s->target))
6086 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006087
6088 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006089 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006090 /* don't send any error message as we're in the body */
6091 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006092 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006093 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006094 if (objt_server(s->target))
6095 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006096
6097 if (!(s->flags & SN_ERR_MASK))
6098 s->flags |= SN_ERR_PRXCOND;
6099 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006100 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006101 return 0;
6102
6103 aborted_xfer:
6104 txn->rsp.msg_state = HTTP_MSG_ERROR;
6105 /* don't send any error message as we're in the body */
6106 stream_int_retnclose(res->cons, NULL);
6107 res->analysers = 0;
6108 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6109
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006110 s->fe->fe_counters.cli_aborts++;
6111 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006112 if (objt_server(s->target))
6113 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006114
6115 if (!(s->flags & SN_ERR_MASK))
6116 s->flags |= SN_ERR_CLICL;
6117 if (!(s->flags & SN_FINST_MASK))
6118 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006119 return 0;
6120}
6121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122/* Iterate the same filter through all request headers.
6123 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006124 * Since it can manage the switch to another backend, it updates the per-proxy
6125 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006127int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006129 char term;
6130 char *cur_ptr, *cur_end, *cur_next;
6131 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006132 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006134 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 last_hdr = 0;
6137
Willy Tarreau9b28e032012-10-12 23:49:43 +02006138 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 old_idx = 0;
6140
6141 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006142 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006143 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006144 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006145 (exp->action == ACT_ALLOW ||
6146 exp->action == ACT_DENY ||
6147 exp->action == ACT_TARPIT))
6148 return 0;
6149
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006150 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 if (!cur_idx)
6152 break;
6153
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006154 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 cur_ptr = cur_next;
6156 cur_end = cur_ptr + cur_hdr->len;
6157 cur_next = cur_end + cur_hdr->cr + 1;
6158
6159 /* Now we have one header between cur_ptr and cur_end,
6160 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006161 */
6162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 /* The annoying part is that pattern matching needs
6164 * that we modify the contents to null-terminate all
6165 * strings before testing them.
6166 */
6167
6168 term = *cur_end;
6169 *cur_end = '\0';
6170
6171 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6172 switch (exp->action) {
6173 case ACT_SETBE:
6174 /* It is not possible to jump a second time.
6175 * FIXME: should we return an HTTP/500 here so that
6176 * the admin knows there's a problem ?
6177 */
6178 if (t->be != t->fe)
6179 break;
6180
6181 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006182 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183 last_hdr = 1;
6184 break;
6185
6186 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006187 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006188 last_hdr = 1;
6189 break;
6190
6191 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006192 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006193 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006194 break;
6195
6196 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006197 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006198 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006199 break;
6200
6201 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006202 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6203 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006204 /* FIXME: if the user adds a newline in the replacement, the
6205 * index will not be recalculated for now, and the new line
6206 * will not be counted as a new header.
6207 */
6208
6209 cur_end += delta;
6210 cur_next += delta;
6211 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006212 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006213 break;
6214
6215 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006216 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 cur_next += delta;
6218
Willy Tarreaufa355d42009-11-29 18:12:29 +01006219 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006220 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6221 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006222 cur_hdr->len = 0;
6223 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006224 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006225 break;
6226
6227 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006228 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006229 if (cur_end)
6230 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006232 /* keep the link from this header to next one in case of later
6233 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006235 old_idx = cur_idx;
6236 }
6237 return 0;
6238}
6239
6240
6241/* Apply the filter to the request line.
6242 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6243 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006244 * Since it can manage the switch to another backend, it updates the per-proxy
6245 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006246 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006247int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006248{
6249 char term;
6250 char *cur_ptr, *cur_end;
6251 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006252 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006253 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreau3d300592007-03-18 18:34:41 +01006255 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006256 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006257 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 (exp->action == ACT_ALLOW ||
6259 exp->action == ACT_DENY ||
6260 exp->action == ACT_TARPIT))
6261 return 0;
6262 else if (exp->action == ACT_REMOVE)
6263 return 0;
6264
6265 done = 0;
6266
Willy Tarreau9b28e032012-10-12 23:49:43 +02006267 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006268 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006269
6270 /* Now we have the request line between cur_ptr and cur_end */
6271
6272 /* The annoying part is that pattern matching needs
6273 * that we modify the contents to null-terminate all
6274 * strings before testing them.
6275 */
6276
6277 term = *cur_end;
6278 *cur_end = '\0';
6279
6280 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6281 switch (exp->action) {
6282 case ACT_SETBE:
6283 /* It is not possible to jump a second time.
6284 * FIXME: should we return an HTTP/500 here so that
6285 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006287 if (t->be != t->fe)
6288 break;
6289
6290 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006291 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006292 done = 1;
6293 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006295 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006296 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006297 done = 1;
6298 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006301 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006302 done = 1;
6303 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006306 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006307 done = 1;
6308 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310 case ACT_REPLACE:
6311 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006312 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6313 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006314 /* FIXME: if the user adds a newline in the replacement, the
6315 * index will not be recalculated for now, and the new line
6316 * will not be counted as a new header.
6317 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006318
Willy Tarreaufa355d42009-11-29 18:12:29 +01006319 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006320 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006321 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006322 HTTP_MSG_RQMETH,
6323 cur_ptr, cur_end + 1,
6324 NULL, NULL);
6325 if (unlikely(!cur_end))
6326 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006328 /* we have a full request and we know that we have either a CR
6329 * or an LF at <ptr>.
6330 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006331 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6332 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006333 /* there is no point trying this regex on headers */
6334 return 1;
6335 }
6336 }
6337 *cur_end = term; /* restore the string terminator */
6338 return done;
6339}
Willy Tarreau97de6242006-12-27 17:18:38 +01006340
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006343/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006344 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346 * unparsable request. Since it can manage the switch to another backend, it
6347 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006349int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006351 struct http_txn *txn = &s->txn;
6352 struct hdr_exp *exp;
6353
6354 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006355 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 /*
6358 * The interleaving of transformations and verdicts
6359 * makes it difficult to decide to continue or stop
6360 * the evaluation.
6361 */
6362
Willy Tarreau6c123b12010-01-28 20:22:06 +01006363 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6364 break;
6365
Willy Tarreau3d300592007-03-18 18:34:41 +01006366 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006368 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006369 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006370
6371 /* if this filter had a condition, evaluate it now and skip to
6372 * next filter if the condition does not match.
6373 */
6374 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006375 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006376 ret = acl_pass(ret);
6377 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6378 ret = !ret;
6379
6380 if (!ret)
6381 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 }
6383
6384 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006385 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006386 if (unlikely(ret < 0))
6387 return -1;
6388
6389 if (likely(ret == 0)) {
6390 /* The filter did not match the request, it can be
6391 * iterated through all headers.
6392 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006393 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006394 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006396 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397}
6398
6399
Willy Tarreaua15645d2007-03-18 16:22:39 +01006400
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006402 * Try to retrieve the server associated to the appsession.
6403 * If the server is found, it's assigned to the session.
6404 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006405void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006406 struct http_txn *txn = &t->txn;
6407 appsess *asession = NULL;
6408 char *sessid_temp = NULL;
6409
Cyril Bontéb21570a2009-11-29 20:04:48 +01006410 if (len > t->be->appsession_len) {
6411 len = t->be->appsession_len;
6412 }
6413
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006414 if (t->be->options2 & PR_O2_AS_REQL) {
6415 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006416 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006417 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006418 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006419 }
6420
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006421 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006422 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6423 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6424 return;
6425 }
6426
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006427 memcpy(txn->sessid, buf, len);
6428 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006429 }
6430
6431 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6432 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6433 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6434 return;
6435 }
6436
Cyril Bontéb21570a2009-11-29 20:04:48 +01006437 memcpy(sessid_temp, buf, len);
6438 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006439
6440 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6441 /* free previously allocated memory */
6442 pool_free2(apools.sessid, sessid_temp);
6443
6444 if (asession != NULL) {
6445 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6446 if (!(t->be->options2 & PR_O2_AS_REQL))
6447 asession->request_count++;
6448
6449 if (asession->serverid != NULL) {
6450 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006451
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006452 while (srv) {
6453 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006454 if ((srv->state & SRV_RUNNING) ||
6455 (t->be->options & PR_O_PERSIST) ||
6456 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006457 /* we found the server and it's usable */
6458 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006459 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006460 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006461 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006462
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006463 break;
6464 } else {
6465 txn->flags &= ~TX_CK_MASK;
6466 txn->flags |= TX_CK_DOWN;
6467 }
6468 }
6469 srv = srv->next;
6470 }
6471 }
6472 }
6473}
6474
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475/* Find the end of a cookie value contained between <s> and <e>. It works the
6476 * same way as with headers above except that the semi-colon also ends a token.
6477 * See RFC2965 for more information. Note that it requires a valid header to
6478 * return a valid result.
6479 */
6480char *find_cookie_value_end(char *s, const char *e)
6481{
6482 int quoted, qdpair;
6483
6484 quoted = qdpair = 0;
6485 for (; s < e; s++) {
6486 if (qdpair) qdpair = 0;
6487 else if (quoted) {
6488 if (*s == '\\') qdpair = 1;
6489 else if (*s == '"') quoted = 0;
6490 }
6491 else if (*s == '"') quoted = 1;
6492 else if (*s == ',' || *s == ';') return s;
6493 }
6494 return s;
6495}
6496
6497/* Delete a value in a header between delimiters <from> and <next> in buffer
6498 * <buf>. The number of characters displaced is returned, and the pointer to
6499 * the first delimiter is updated if required. The function tries as much as
6500 * possible to respect the following principles :
6501 * - replace <from> delimiter by the <next> one unless <from> points to a
6502 * colon, in which case <next> is simply removed
6503 * - set exactly one space character after the new first delimiter, unless
6504 * there are not enough characters in the block being moved to do so.
6505 * - remove unneeded spaces before the previous delimiter and after the new
6506 * one.
6507 *
6508 * It is the caller's responsibility to ensure that :
6509 * - <from> points to a valid delimiter or the colon ;
6510 * - <next> points to a valid delimiter or the final CR/LF ;
6511 * - there are non-space chars before <from> ;
6512 * - there is a CR/LF at or after <next>.
6513 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006514int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515{
6516 char *prev = *from;
6517
6518 if (*prev == ':') {
6519 /* We're removing the first value, preserve the colon and add a
6520 * space if possible.
6521 */
6522 if (!http_is_crlf[(unsigned char)*next])
6523 next++;
6524 prev++;
6525 if (prev < next)
6526 *prev++ = ' ';
6527
6528 while (http_is_spht[(unsigned char)*next])
6529 next++;
6530 } else {
6531 /* Remove useless spaces before the old delimiter. */
6532 while (http_is_spht[(unsigned char)*(prev-1)])
6533 prev--;
6534 *from = prev;
6535
6536 /* copy the delimiter and if possible a space if we're
6537 * not at the end of the line.
6538 */
6539 if (!http_is_crlf[(unsigned char)*next]) {
6540 *prev++ = *next++;
6541 if (prev + 1 < next)
6542 *prev++ = ' ';
6543 while (http_is_spht[(unsigned char)*next])
6544 next++;
6545 }
6546 }
6547 return buffer_replace2(buf, prev, next, NULL, 0);
6548}
6549
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006550/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006551 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 * desirable to call it only when needed. This code is quite complex because
6553 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6554 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006556void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006558 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006561 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6562 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006564 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006565 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006566 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006568 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006570 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006572 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 hdr_beg = hdr_next;
6574 hdr_end = hdr_beg + cur_hdr->len;
6575 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 /* We have one full header between hdr_beg and hdr_end, and the
6578 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579 * "Cookie:" headers.
6580 */
6581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006582 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006583 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584 old_idx = cur_idx;
6585 continue;
6586 }
6587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006588 del_from = NULL; /* nothing to be deleted */
6589 preserve_hdr = 0; /* assume we may kill the whole header */
6590
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591 /* Now look for cookies. Conforming to RFC2109, we have to support
6592 * attributes whose name begin with a '$', and associate them with
6593 * the right cookie, if we want to delete this cookie.
6594 * So there are 3 cases for each cookie read :
6595 * 1) it's a special attribute, beginning with a '$' : ignore it.
6596 * 2) it's a server id cookie that we *MAY* want to delete : save
6597 * some pointers on it (last semi-colon, beginning of cookie...)
6598 * 3) it's an application cookie : we *MAY* have to delete a previous
6599 * "special" cookie.
6600 * At the end of loop, if a "special" cookie remains, we may have to
6601 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 * *MUST* delete it.
6603 *
6604 * Note: RFC2965 is unclear about the processing of spaces around
6605 * the equal sign in the ATTR=VALUE form. A careful inspection of
6606 * the RFC explicitly allows spaces before it, and not within the
6607 * tokens (attrs or values). An inspection of RFC2109 allows that
6608 * too but section 10.1.3 lets one think that spaces may be allowed
6609 * after the equal sign too, resulting in some (rare) buggy
6610 * implementations trying to do that. So let's do what servers do.
6611 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6612 * allowed quoted strings in values, with any possible character
6613 * after a backslash, including control chars and delimitors, which
6614 * causes parsing to become ambiguous. Browsers also allow spaces
6615 * within values even without quotes.
6616 *
6617 * We have to keep multiple pointers in order to support cookie
6618 * removal at the beginning, middle or end of header without
6619 * corrupting the header. All of these headers are valid :
6620 *
6621 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6622 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6623 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6624 * | | | | | | | | |
6625 * | | | | | | | | hdr_end <--+
6626 * | | | | | | | +--> next
6627 * | | | | | | +----> val_end
6628 * | | | | | +-----------> val_beg
6629 * | | | | +--------------> equal
6630 * | | | +----------------> att_end
6631 * | | +---------------------> att_beg
6632 * | +--------------------------> prev
6633 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634 */
6635
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006636 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6637 /* Iterate through all cookies on this line */
6638
6639 /* find att_beg */
6640 att_beg = prev + 1;
6641 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6642 att_beg++;
6643
6644 /* find att_end : this is the first character after the last non
6645 * space before the equal. It may be equal to hdr_end.
6646 */
6647 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006648
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006649 while (equal < hdr_end) {
6650 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006652 if (http_is_spht[(unsigned char)*equal++])
6653 continue;
6654 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 }
6656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006657 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6658 * is between <att_beg> and <equal>, both may be identical.
6659 */
6660
6661 /* look for end of cookie if there is an equal sign */
6662 if (equal < hdr_end && *equal == '=') {
6663 /* look for the beginning of the value */
6664 val_beg = equal + 1;
6665 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6666 val_beg++;
6667
6668 /* find the end of the value, respecting quotes */
6669 next = find_cookie_value_end(val_beg, hdr_end);
6670
6671 /* make val_end point to the first white space or delimitor after the value */
6672 val_end = next;
6673 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6674 val_end--;
6675 } else {
6676 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006677 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006679 /* We have nothing to do with attributes beginning with '$'. However,
6680 * they will automatically be removed if a header before them is removed,
6681 * since they're supposed to be linked together.
6682 */
6683 if (*att_beg == '$')
6684 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006686 /* Ignore cookies with no equal sign */
6687 if (equal == next) {
6688 /* This is not our cookie, so we must preserve it. But if we already
6689 * scheduled another cookie for removal, we cannot remove the
6690 * complete header, but we can remove the previous block itself.
6691 */
6692 preserve_hdr = 1;
6693 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006694 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 val_end += delta;
6696 next += delta;
6697 hdr_end += delta;
6698 hdr_next += delta;
6699 cur_hdr->len += delta;
6700 http_msg_move_end(&txn->req, delta);
6701 prev = del_from;
6702 del_from = NULL;
6703 }
6704 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 /* if there are spaces around the equal sign, we need to
6708 * strip them otherwise we'll get trouble for cookie captures,
6709 * or even for rewrites. Since this happens extremely rarely,
6710 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006711 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006712 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6713 int stripped_before = 0;
6714 int stripped_after = 0;
6715
6716 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006717 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006718 equal += stripped_before;
6719 val_beg += stripped_before;
6720 }
6721
6722 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006723 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006724 val_beg += stripped_after;
6725 stripped_before += stripped_after;
6726 }
6727
6728 val_end += stripped_before;
6729 next += stripped_before;
6730 hdr_end += stripped_before;
6731 hdr_next += stripped_before;
6732 cur_hdr->len += stripped_before;
6733 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 /* First, let's see if we want to capture this cookie. We check
6738 * that we don't already have a client side cookie, because we
6739 * can only capture one. Also as an optimisation, we ignore
6740 * cookies shorter than the declared name.
6741 */
6742 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6743 (val_end - att_beg >= t->fe->capture_namelen) &&
6744 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6745 int log_len = val_end - att_beg;
6746
6747 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6748 Alert("HTTP logging : out of memory.\n");
6749 } else {
6750 if (log_len > t->fe->capture_len)
6751 log_len = t->fe->capture_len;
6752 memcpy(txn->cli_cookie, att_beg, log_len);
6753 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756
Willy Tarreaubca99692010-10-06 19:25:55 +02006757 /* Persistence cookies in passive, rewrite or insert mode have the
6758 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006760 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006762 * For cookies in prefix mode, the form is :
6763 *
6764 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 */
6766 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6767 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6768 struct server *srv = t->be->srv;
6769 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6772 * have the server ID between val_beg and delim, and the original cookie between
6773 * delim+1 and val_end. Otherwise, delim==val_end :
6774 *
6775 * Cookie: NAME=SRV; # in all but prefix modes
6776 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6777 * | || || | |+-> next
6778 * | || || | +--> val_end
6779 * | || || +---------> delim
6780 * | || |+------------> val_beg
6781 * | || +-------------> att_end = equal
6782 * | |+-----------------> att_beg
6783 * | +------------------> prev
6784 * +-------------------------> hdr_beg
6785 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786
Willy Tarreau67402132012-05-31 20:40:20 +02006787 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006788 for (delim = val_beg; delim < val_end; delim++)
6789 if (*delim == COOKIE_DELIM)
6790 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006791 } else {
6792 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006794 /* Now check if the cookie contains a date field, which would
6795 * appear after a vertical bar ('|') just after the server name
6796 * and before the delimiter.
6797 */
6798 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6799 if (vbar1) {
6800 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006801 * right is the last seen date. It is a base64 encoded
6802 * 30-bit value representing the UNIX date since the
6803 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006804 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006805 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006806 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006807 if (val_end - vbar1 >= 5) {
6808 val = b64tos30(vbar1);
6809 if (val > 0)
6810 txn->cookie_last_date = val << 2;
6811 }
6812 /* look for a second vertical bar */
6813 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6814 if (vbar1 && (val_end - vbar1 > 5)) {
6815 val = b64tos30(vbar1 + 1);
6816 if (val > 0)
6817 txn->cookie_first_date = val << 2;
6818 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006819 }
6820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006821
Willy Tarreauf64d1412010-10-07 20:06:11 +02006822 /* if the cookie has an expiration date and the proxy wants to check
6823 * it, then we do that now. We first check if the cookie is too old,
6824 * then only if it has expired. We detect strict overflow because the
6825 * time resolution here is not great (4 seconds). Cookies with dates
6826 * in the future are ignored if their offset is beyond one day. This
6827 * allows an admin to fix timezone issues without expiring everyone
6828 * and at the same time avoids keeping unwanted side effects for too
6829 * long.
6830 */
6831 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006832 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6833 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006834 txn->flags &= ~TX_CK_MASK;
6835 txn->flags |= TX_CK_OLD;
6836 delim = val_beg; // let's pretend we have not found the cookie
6837 txn->cookie_first_date = 0;
6838 txn->cookie_last_date = 0;
6839 }
6840 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006841 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6842 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006843 txn->flags &= ~TX_CK_MASK;
6844 txn->flags |= TX_CK_EXPIRED;
6845 delim = val_beg; // let's pretend we have not found the cookie
6846 txn->cookie_first_date = 0;
6847 txn->cookie_last_date = 0;
6848 }
6849
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 /* Here, we'll look for the first running server which supports the cookie.
6851 * This allows to share a same cookie between several servers, for example
6852 * to dedicate backup servers to specific servers only.
6853 * However, to prevent clients from sticking to cookie-less backup server
6854 * when they have incidentely learned an empty cookie, we simply ignore
6855 * empty cookies and mark them as invalid.
6856 * The same behaviour is applied when persistence must be ignored.
6857 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006858 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006859 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 while (srv) {
6862 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6863 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6864 if ((srv->state & SRV_RUNNING) ||
6865 (t->be->options & PR_O_PERSIST) ||
6866 (t->flags & SN_FORCE_PRST)) {
6867 /* we found the server and we can use it */
6868 txn->flags &= ~TX_CK_MASK;
6869 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6870 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006871 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 break;
6873 } else {
6874 /* we found a server, but it's down,
6875 * mark it as such and go on in case
6876 * another one is available.
6877 */
6878 txn->flags &= ~TX_CK_MASK;
6879 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006881 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006882 srv = srv->next;
6883 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884
Willy Tarreauf64d1412010-10-07 20:06:11 +02006885 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006886 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006887 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006888 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6889 txn->flags |= TX_CK_UNUSED;
6890 else
6891 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006893
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006894 /* depending on the cookie mode, we may have to either :
6895 * - delete the complete cookie if we're in insert+indirect mode, so that
6896 * the server never sees it ;
6897 * - remove the server id from the cookie value, and tag the cookie as an
6898 * application cookie so that it does not get accidentely removed later,
6899 * if we're in cookie prefix mode
6900 */
Willy Tarreau67402132012-05-31 20:40:20 +02006901 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006903
Willy Tarreau9b28e032012-10-12 23:49:43 +02006904 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 val_end += delta;
6906 next += delta;
6907 hdr_end += delta;
6908 hdr_next += delta;
6909 cur_hdr->len += delta;
6910 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 del_from = NULL;
6913 preserve_hdr = 1; /* we want to keep this cookie */
6914 }
6915 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006916 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006917 del_from = prev;
6918 }
6919 } else {
6920 /* This is not our cookie, so we must preserve it. But if we already
6921 * scheduled another cookie for removal, we cannot remove the
6922 * complete header, but we can remove the previous block itself.
6923 */
6924 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006927 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006928 if (att_beg >= del_from)
6929 att_beg += delta;
6930 if (att_end >= del_from)
6931 att_end += delta;
6932 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 val_end += delta;
6934 next += delta;
6935 hdr_end += delta;
6936 hdr_next += delta;
6937 cur_hdr->len += delta;
6938 http_msg_move_end(&txn->req, delta);
6939 prev = del_from;
6940 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006941 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 /* Look for the appsession cookie unless persistence must be ignored */
6945 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6946 int cmp_len, value_len;
6947 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006948
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006949 if (t->be->options2 & PR_O2_AS_PFX) {
6950 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6951 value_begin = att_beg + t->be->appsession_name_len;
6952 value_len = val_end - att_beg - t->be->appsession_name_len;
6953 } else {
6954 cmp_len = att_end - att_beg;
6955 value_begin = val_beg;
6956 value_len = val_end - val_beg;
6957 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006958
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006959 /* let's see if the cookie is our appcookie */
6960 if (cmp_len == t->be->appsession_name_len &&
6961 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6962 manage_client_side_appsession(t, value_begin, value_len);
6963 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964 }
6965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006966 /* continue with next cookie on this header line */
6967 att_beg = next;
6968 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006970 /* There are no more cookies on this line.
6971 * We may still have one (or several) marked for deletion at the
6972 * end of the line. We must do this now in two ways :
6973 * - if some cookies must be preserved, we only delete from the
6974 * mark to the end of line ;
6975 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006977 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006980 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982 cur_hdr->len += delta;
6983 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006984 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985
6986 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006987 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6988 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006989 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006990 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006992 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006993 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 }
6995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006996 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006997 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006998 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999}
7000
7001
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002/* Iterate the same filter through all response headers contained in <rtr>.
7003 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7004 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007005int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006{
7007 char term;
7008 char *cur_ptr, *cur_end, *cur_next;
7009 int cur_idx, old_idx, last_hdr;
7010 struct http_txn *txn = &t->txn;
7011 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007012 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
7014 last_hdr = 0;
7015
Willy Tarreau9b28e032012-10-12 23:49:43 +02007016 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017 old_idx = 0;
7018
7019 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007020 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 (exp->action == ACT_ALLOW ||
7024 exp->action == ACT_DENY))
7025 return 0;
7026
7027 cur_idx = txn->hdr_idx.v[old_idx].next;
7028 if (!cur_idx)
7029 break;
7030
7031 cur_hdr = &txn->hdr_idx.v[cur_idx];
7032 cur_ptr = cur_next;
7033 cur_end = cur_ptr + cur_hdr->len;
7034 cur_next = cur_end + cur_hdr->cr + 1;
7035
7036 /* Now we have one header between cur_ptr and cur_end,
7037 * and the next header starts at cur_next.
7038 */
7039
7040 /* The annoying part is that pattern matching needs
7041 * that we modify the contents to null-terminate all
7042 * strings before testing them.
7043 */
7044
7045 term = *cur_end;
7046 *cur_end = '\0';
7047
7048 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7049 switch (exp->action) {
7050 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007051 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 last_hdr = 1;
7053 break;
7054
7055 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007056 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 last_hdr = 1;
7058 break;
7059
7060 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007061 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7062 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063 /* FIXME: if the user adds a newline in the replacement, the
7064 * index will not be recalculated for now, and the new line
7065 * will not be counted as a new header.
7066 */
7067
7068 cur_end += delta;
7069 cur_next += delta;
7070 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007071 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007072 break;
7073
7074 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007075 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 cur_next += delta;
7077
Willy Tarreaufa355d42009-11-29 18:12:29 +01007078 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7080 txn->hdr_idx.used--;
7081 cur_hdr->len = 0;
7082 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007083 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007084 break;
7085
7086 }
7087 }
7088 if (cur_end)
7089 *cur_end = term; /* restore the string terminator */
7090
7091 /* keep the link from this header to next one in case of later
7092 * removal of next header.
7093 */
7094 old_idx = cur_idx;
7095 }
7096 return 0;
7097}
7098
7099
7100/* Apply the filter to the status line in the response buffer <rtr>.
7101 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7102 * or -1 if a replacement resulted in an invalid status line.
7103 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007104int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105{
7106 char term;
7107 char *cur_ptr, *cur_end;
7108 int done;
7109 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007110 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111
7112
Willy Tarreau3d300592007-03-18 18:34:41 +01007113 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007115 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 (exp->action == ACT_ALLOW ||
7117 exp->action == ACT_DENY))
7118 return 0;
7119 else if (exp->action == ACT_REMOVE)
7120 return 0;
7121
7122 done = 0;
7123
Willy Tarreau9b28e032012-10-12 23:49:43 +02007124 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007125 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126
7127 /* Now we have the status line between cur_ptr and cur_end */
7128
7129 /* The annoying part is that pattern matching needs
7130 * that we modify the contents to null-terminate all
7131 * strings before testing them.
7132 */
7133
7134 term = *cur_end;
7135 *cur_end = '\0';
7136
7137 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7138 switch (exp->action) {
7139 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007140 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007141 done = 1;
7142 break;
7143
7144 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007145 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 done = 1;
7147 break;
7148
7149 case ACT_REPLACE:
7150 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007151 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7152 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 /* FIXME: if the user adds a newline in the replacement, the
7154 * index will not be recalculated for now, and the new line
7155 * will not be counted as a new header.
7156 */
7157
Willy Tarreaufa355d42009-11-29 18:12:29 +01007158 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007160 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007161 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 cur_ptr, cur_end + 1,
7163 NULL, NULL);
7164 if (unlikely(!cur_end))
7165 return -1;
7166
7167 /* we have a full respnse and we know that we have either a CR
7168 * or an LF at <ptr>.
7169 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007170 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007171 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 /* there is no point trying this regex on headers */
7173 return 1;
7174 }
7175 }
7176 *cur_end = term; /* restore the string terminator */
7177 return done;
7178}
7179
7180
7181
7182/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007183 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7185 * unparsable response.
7186 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007187int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007189 struct http_txn *txn = &s->txn;
7190 struct hdr_exp *exp;
7191
7192 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 int ret;
7194
7195 /*
7196 * The interleaving of transformations and verdicts
7197 * makes it difficult to decide to continue or stop
7198 * the evaluation.
7199 */
7200
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007201 if (txn->flags & TX_SVDENY)
7202 break;
7203
Willy Tarreau3d300592007-03-18 18:34:41 +01007204 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7206 exp->action == ACT_PASS)) {
7207 exp = exp->next;
7208 continue;
7209 }
7210
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007211 /* if this filter had a condition, evaluate it now and skip to
7212 * next filter if the condition does not match.
7213 */
7214 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007215 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007216 ret = acl_pass(ret);
7217 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7218 ret = !ret;
7219 if (!ret)
7220 continue;
7221 }
7222
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007224 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 if (unlikely(ret < 0))
7226 return -1;
7227
7228 if (likely(ret == 0)) {
7229 /* The filter did not match the response, it can be
7230 * iterated through all headers.
7231 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007232 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 }
7235 return 0;
7236}
7237
7238
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007240 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007241 * desirable to call it only when needed. This function is also used when we
7242 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007244void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245{
7246 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007247 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007248 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007250 char *hdr_beg, *hdr_end, *hdr_next;
7251 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007252
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 /* Iterate through the headers.
7254 * we start with the start line.
7255 */
7256 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007257 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258
7259 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7260 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007261 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262
7263 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007264 hdr_beg = hdr_next;
7265 hdr_end = hdr_beg + cur_hdr->len;
7266 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267
Willy Tarreau24581ba2010-08-31 22:39:35 +02007268 /* We have one full header between hdr_beg and hdr_end, and the
7269 * next header starts at hdr_next. We're only interested in
7270 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 */
7272
Willy Tarreau24581ba2010-08-31 22:39:35 +02007273 is_cookie2 = 0;
7274 prev = hdr_beg + 10;
7275 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007276 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007277 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7278 if (!val) {
7279 old_idx = cur_idx;
7280 continue;
7281 }
7282 is_cookie2 = 1;
7283 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 }
7285
Willy Tarreau24581ba2010-08-31 22:39:35 +02007286 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7287 * <prev> points to the colon.
7288 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007289 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007290
Willy Tarreau24581ba2010-08-31 22:39:35 +02007291 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7292 * check-cache is enabled) and we are not interested in checking
7293 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007294 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007295 if (t->be->cookie_name == NULL &&
7296 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007297 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 return;
7299
Willy Tarreau24581ba2010-08-31 22:39:35 +02007300 /* OK so now we know we have to process this response cookie.
7301 * The format of the Set-Cookie header is slightly different
7302 * from the format of the Cookie header in that it does not
7303 * support the comma as a cookie delimiter (thus the header
7304 * cannot be folded) because the Expires attribute described in
7305 * the original Netscape's spec may contain an unquoted date
7306 * with a comma inside. We have to live with this because
7307 * many browsers don't support Max-Age and some browsers don't
7308 * support quoted strings. However the Set-Cookie2 header is
7309 * clean.
7310 *
7311 * We have to keep multiple pointers in order to support cookie
7312 * removal at the beginning, middle or end of header without
7313 * corrupting the header (in case of set-cookie2). A special
7314 * pointer, <scav> points to the beginning of the set-cookie-av
7315 * fields after the first semi-colon. The <next> pointer points
7316 * either to the end of line (set-cookie) or next unquoted comma
7317 * (set-cookie2). All of these headers are valid :
7318 *
7319 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7320 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7321 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7322 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7323 * | | | | | | | | | |
7324 * | | | | | | | | +-> next hdr_end <--+
7325 * | | | | | | | +------------> scav
7326 * | | | | | | +--------------> val_end
7327 * | | | | | +--------------------> val_beg
7328 * | | | | +----------------------> equal
7329 * | | | +------------------------> att_end
7330 * | | +----------------------------> att_beg
7331 * | +------------------------------> prev
7332 * +-----------------------------------------> hdr_beg
7333 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334
Willy Tarreau24581ba2010-08-31 22:39:35 +02007335 for (; prev < hdr_end; prev = next) {
7336 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337
Willy Tarreau24581ba2010-08-31 22:39:35 +02007338 /* find att_beg */
7339 att_beg = prev + 1;
7340 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7341 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342
Willy Tarreau24581ba2010-08-31 22:39:35 +02007343 /* find att_end : this is the first character after the last non
7344 * space before the equal. It may be equal to hdr_end.
7345 */
7346 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347
Willy Tarreau24581ba2010-08-31 22:39:35 +02007348 while (equal < hdr_end) {
7349 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7350 break;
7351 if (http_is_spht[(unsigned char)*equal++])
7352 continue;
7353 att_end = equal;
7354 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355
Willy Tarreau24581ba2010-08-31 22:39:35 +02007356 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7357 * is between <att_beg> and <equal>, both may be identical.
7358 */
7359
7360 /* look for end of cookie if there is an equal sign */
7361 if (equal < hdr_end && *equal == '=') {
7362 /* look for the beginning of the value */
7363 val_beg = equal + 1;
7364 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7365 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366
Willy Tarreau24581ba2010-08-31 22:39:35 +02007367 /* find the end of the value, respecting quotes */
7368 next = find_cookie_value_end(val_beg, hdr_end);
7369
7370 /* make val_end point to the first white space or delimitor after the value */
7371 val_end = next;
7372 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7373 val_end--;
7374 } else {
7375 /* <equal> points to next comma, semi-colon or EOL */
7376 val_beg = val_end = next = equal;
7377 }
7378
7379 if (next < hdr_end) {
7380 /* Set-Cookie2 supports multiple cookies, and <next> points to
7381 * a colon or semi-colon before the end. So skip all attr-value
7382 * pairs and look for the next comma. For Set-Cookie, since
7383 * commas are permitted in values, skip to the end.
7384 */
7385 if (is_cookie2)
7386 next = find_hdr_value_end(next, hdr_end);
7387 else
7388 next = hdr_end;
7389 }
7390
7391 /* Now everything is as on the diagram above */
7392
7393 /* Ignore cookies with no equal sign */
7394 if (equal == val_end)
7395 continue;
7396
7397 /* If there are spaces around the equal sign, we need to
7398 * strip them otherwise we'll get trouble for cookie captures,
7399 * or even for rewrites. Since this happens extremely rarely,
7400 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7403 int stripped_before = 0;
7404 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405
Willy Tarreau24581ba2010-08-31 22:39:35 +02007406 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007407 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007408 equal += stripped_before;
7409 val_beg += stripped_before;
7410 }
7411
7412 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007413 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007414 val_beg += stripped_after;
7415 stripped_before += stripped_after;
7416 }
7417
7418 val_end += stripped_before;
7419 next += stripped_before;
7420 hdr_end += stripped_before;
7421 hdr_next += stripped_before;
7422 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007423 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007424 }
7425
7426 /* First, let's see if we want to capture this cookie. We check
7427 * that we don't already have a server side cookie, because we
7428 * can only capture one. Also as an optimisation, we ignore
7429 * cookies shorter than the declared name.
7430 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007431 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007432 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 (val_end - att_beg >= t->fe->capture_namelen) &&
7434 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7435 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007436 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437 Alert("HTTP logging : out of memory.\n");
7438 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007439 else {
7440 if (log_len > t->fe->capture_len)
7441 log_len = t->fe->capture_len;
7442 memcpy(txn->srv_cookie, att_beg, log_len);
7443 txn->srv_cookie[log_len] = 0;
7444 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445 }
7446
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007447 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007449 if (!(t->flags & SN_IGNORE_PRST) &&
7450 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7451 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007452 /* assume passive cookie by default */
7453 txn->flags &= ~TX_SCK_MASK;
7454 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455
7456 /* If the cookie is in insert mode on a known server, we'll delete
7457 * this occurrence because we'll insert another one later.
7458 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 * a direct access.
7460 */
Willy Tarreau67402132012-05-31 20:40:20 +02007461 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007462 /* The "preserve" flag was set, we don't want to touch the
7463 * server's cookie.
7464 */
7465 }
Willy Tarreau67402132012-05-31 20:40:20 +02007466 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7467 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007468 /* this cookie must be deleted */
7469 if (*prev == ':' && next == hdr_end) {
7470 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7473 txn->hdr_idx.used--;
7474 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007475 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007476 hdr_next += delta;
7477 http_msg_move_end(&txn->rsp, delta);
7478 /* note: while both invalid now, <next> and <hdr_end>
7479 * are still equal, so the for() will stop as expected.
7480 */
7481 } else {
7482 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007483 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007484 next = prev;
7485 hdr_end += delta;
7486 hdr_next += delta;
7487 cur_hdr->len += delta;
7488 http_msg_move_end(&txn->rsp, delta);
7489 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007490 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007491 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 }
Willy Tarreau67402132012-05-31 20:40:20 +02007494 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007495 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496 * with this server since we know it.
7497 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007498 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 next += delta;
7500 hdr_end += delta;
7501 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007502 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007503 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504
Willy Tarreauf1348312010-10-07 15:54:11 +02007505 txn->flags &= ~TX_SCK_MASK;
7506 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007507 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007508 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007512 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 next += delta;
7514 hdr_end += delta;
7515 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007516 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007517 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
Willy Tarreau827aee92011-03-10 16:55:02 +01007519 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007520 txn->flags &= ~TX_SCK_MASK;
7521 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007522 }
7523 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007524 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7525 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007526 int cmp_len, value_len;
7527 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007528
Cyril Bontéb21570a2009-11-29 20:04:48 +01007529 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007530 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7531 value_begin = att_beg + t->be->appsession_name_len;
7532 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007533 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 cmp_len = att_end - att_beg;
7535 value_begin = val_beg;
7536 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007537 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007538
Cyril Bonté17530c32010-04-06 21:11:10 +02007539 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007540 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7541 /* free a possibly previously allocated memory */
7542 pool_free2(apools.sessid, txn->sessid);
7543
Cyril Bontéb21570a2009-11-29 20:04:48 +01007544 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007545 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007546 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7547 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7548 return;
7549 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007550 memcpy(txn->sessid, value_begin, value_len);
7551 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007553 }
7554 /* that's done for this cookie, check the next one on the same
7555 * line when next != hdr_end (only if is_cookie2).
7556 */
7557 }
7558 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007561
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007562 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007563 appsess *asession = NULL;
7564 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007565 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007566 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007567 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007568 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7569 Alert("Not enough Memory process_srv():asession:calloc().\n");
7570 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7571 return;
7572 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007573 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7574
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007575 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7576 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7577 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007578 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007579 return;
7580 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007581 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007582 asession->sessid[t->be->appsession_len] = 0;
7583
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007584 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007585 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007586 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007587 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007588 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007589 return;
7590 }
7591 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007592 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007593
7594 asession->request_count = 0;
7595 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7596 }
7597
7598 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7599 asession->request_count++;
7600 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601}
7602
7603
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604/*
7605 * Check if response is cacheable or not. Updates t->flags.
7606 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007607void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608{
7609 struct http_txn *txn = &t->txn;
7610 char *p1, *p2;
7611
7612 char *cur_ptr, *cur_end, *cur_next;
7613 int cur_idx;
7614
Willy Tarreau5df51872007-11-25 16:20:08 +01007615 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 return;
7617
7618 /* Iterate through the headers.
7619 * we start with the start line.
7620 */
7621 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007622 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623
7624 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7625 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007626 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627
7628 cur_hdr = &txn->hdr_idx.v[cur_idx];
7629 cur_ptr = cur_next;
7630 cur_end = cur_ptr + cur_hdr->len;
7631 cur_next = cur_end + cur_hdr->cr + 1;
7632
7633 /* We have one full header between cur_ptr and cur_end, and the
7634 * next header starts at cur_next. We're only interested in
7635 * "Cookie:" headers.
7636 */
7637
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007638 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7639 if (val) {
7640 if ((cur_end - (cur_ptr + val) >= 8) &&
7641 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7642 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7643 return;
7644 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645 }
7646
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007647 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7648 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 continue;
7650
7651 /* OK, right now we know we have a cache-control header at cur_ptr */
7652
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007653 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654
7655 if (p1 >= cur_end) /* no more info */
7656 continue;
7657
7658 /* p1 is at the beginning of the value */
7659 p2 = p1;
7660
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007661 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662 p2++;
7663
7664 /* we have a complete value between p1 and p2 */
7665 if (p2 < cur_end && *p2 == '=') {
7666 /* we have something of the form no-cache="set-cookie" */
7667 if ((cur_end - p1 >= 21) &&
7668 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7669 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007670 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 continue;
7672 }
7673
7674 /* OK, so we know that either p2 points to the end of string or to a comma */
7675 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007676 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7678 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7679 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007680 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681 return;
7682 }
7683
7684 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007685 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 continue;
7687 }
7688 }
7689}
7690
7691
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692/*
7693 * Try to retrieve a known appsession in the URI, then the associated server.
7694 * If the server is found, it's assigned to the session.
7695 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007696void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007698 char *end_params, *first_param, *cur_param, *next_param;
7699 char separator;
7700 int value_len;
7701
7702 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007704 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007705 (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 +01007706 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708
Cyril Bontéb21570a2009-11-29 20:04:48 +01007709 first_param = NULL;
7710 switch (mode) {
7711 case PR_O2_AS_M_PP:
7712 first_param = memchr(begin, ';', len);
7713 break;
7714 case PR_O2_AS_M_QS:
7715 first_param = memchr(begin, '?', len);
7716 break;
7717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718
Cyril Bontéb21570a2009-11-29 20:04:48 +01007719 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007720 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Cyril Bontéb21570a2009-11-29 20:04:48 +01007723 switch (mode) {
7724 case PR_O2_AS_M_PP:
7725 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7726 end_params = (char *) begin + len;
7727 }
7728 separator = ';';
7729 break;
7730 case PR_O2_AS_M_QS:
7731 end_params = (char *) begin + len;
7732 separator = '&';
7733 break;
7734 default:
7735 /* unknown mode, shouldn't happen */
7736 return;
7737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738
Cyril Bontéb21570a2009-11-29 20:04:48 +01007739 cur_param = next_param = end_params;
7740 while (cur_param > first_param) {
7741 cur_param--;
7742 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7743 /* let's see if this is the appsession parameter */
7744 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7745 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7746 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7747 /* Cool... it's the right one */
7748 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7749 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7750 if (value_len > 0) {
7751 manage_client_side_appsession(t, cur_param, value_len);
7752 }
7753 break;
7754 }
7755 next_param = cur_param;
7756 }
7757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007759 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007760 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762}
7763
Willy Tarreaub2513902006-12-17 14:52:38 +01007764/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007765 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007766 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007767 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007768 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007769 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007770 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007771 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007772 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007773int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007774{
7775 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007776 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007777 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007778
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007779 if (!uri_auth)
7780 return 0;
7781
Cyril Bonté70be45d2010-10-12 00:14:35 +02007782 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007783 return 0;
7784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007785 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007786 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007787 return 0;
7788
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007789 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007790 return 0;
7791
Willy Tarreaub2513902006-12-17 14:52:38 +01007792 return 1;
7793}
7794
Willy Tarreau4076a152009-04-02 15:18:36 +02007795/*
7796 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007797 * By default it tries to report the error position as msg->err_pos. However if
7798 * this one is not set, it will then report msg->next, which is the last known
7799 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007800 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007801 */
7802void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007803 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007804 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007805{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007806 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007807 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007808
Willy Tarreau9b28e032012-10-12 23:49:43 +02007809 es->len = MIN(chn->buf->i, sizeof(es->buf));
7810 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007811 len1 = MIN(len1, es->len);
7812 len2 = es->len - len1; /* remaining data if buffer wraps */
7813
Willy Tarreau9b28e032012-10-12 23:49:43 +02007814 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007815 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007816 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007817
Willy Tarreau4076a152009-04-02 15:18:36 +02007818 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007819 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007820 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007821 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007822
Willy Tarreau4076a152009-04-02 15:18:36 +02007823 es->when = date; // user-visible date
7824 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007825 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007826 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007827 if (objt_conn(s->req->prod->end))
7828 es->src = __objt_conn(s->req->prod->end)->addr.from;
7829 else
7830 memset(&es->src, 0, sizeof(es->src));
7831
Willy Tarreau078272e2010-12-12 12:46:33 +01007832 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007833 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007834 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007835 es->s_flags = s->flags;
7836 es->t_flags = s->txn.flags;
7837 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007838 es->b_out = chn->buf->o;
7839 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007840 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007841 es->m_clen = msg->chunk_len;
7842 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007843}
Willy Tarreaub2513902006-12-17 14:52:38 +01007844
Willy Tarreau294c4732011-12-16 21:35:50 +01007845/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7846 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7847 * performed over the whole headers. Otherwise it must contain a valid header
7848 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7849 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7850 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7851 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007852 * -1. The value fetch stops at commas, so this function is suited for use with
7853 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007854 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007855 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007856unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007857 struct hdr_idx *idx, int occ,
7858 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007859{
Willy Tarreau294c4732011-12-16 21:35:50 +01007860 struct hdr_ctx local_ctx;
7861 char *ptr_hist[MAX_HDR_HISTORY];
7862 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007863 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007864 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007865
Willy Tarreau294c4732011-12-16 21:35:50 +01007866 if (!ctx) {
7867 local_ctx.idx = 0;
7868 ctx = &local_ctx;
7869 }
7870
Willy Tarreaubce70882009-09-07 11:51:47 +02007871 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007872 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007873 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007874 occ--;
7875 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007876 *vptr = ctx->line + ctx->val;
7877 *vlen = ctx->vlen;
7878 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007879 }
7880 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007881 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007882 }
7883
7884 /* negative occurrence, we scan all the list then walk back */
7885 if (-occ > MAX_HDR_HISTORY)
7886 return 0;
7887
Willy Tarreau294c4732011-12-16 21:35:50 +01007888 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007889 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007890 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7891 len_hist[hist_ptr] = ctx->vlen;
7892 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007893 hist_ptr = 0;
7894 found++;
7895 }
7896 if (-occ > found)
7897 return 0;
7898 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007899 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7900 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7901 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007902 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007903 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007904 if (hist_ptr >= MAX_HDR_HISTORY)
7905 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007906 *vptr = ptr_hist[hist_ptr];
7907 *vlen = len_hist[hist_ptr];
7908 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007909}
7910
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007911/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7912 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7913 * performed over the whole headers. Otherwise it must contain a valid header
7914 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7915 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7916 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7917 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7918 * -1. This function differs from http_get_hdr() in that it only returns full
7919 * line header values and does not stop at commas.
7920 * The return value is 0 if nothing was found, or non-zero otherwise.
7921 */
7922unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7923 struct hdr_idx *idx, int occ,
7924 struct hdr_ctx *ctx, char **vptr, int *vlen)
7925{
7926 struct hdr_ctx local_ctx;
7927 char *ptr_hist[MAX_HDR_HISTORY];
7928 int len_hist[MAX_HDR_HISTORY];
7929 unsigned int hist_ptr;
7930 int found;
7931
7932 if (!ctx) {
7933 local_ctx.idx = 0;
7934 ctx = &local_ctx;
7935 }
7936
7937 if (occ >= 0) {
7938 /* search from the beginning */
7939 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7940 occ--;
7941 if (occ <= 0) {
7942 *vptr = ctx->line + ctx->val;
7943 *vlen = ctx->vlen;
7944 return 1;
7945 }
7946 }
7947 return 0;
7948 }
7949
7950 /* negative occurrence, we scan all the list then walk back */
7951 if (-occ > MAX_HDR_HISTORY)
7952 return 0;
7953
7954 found = hist_ptr = 0;
7955 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7956 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7957 len_hist[hist_ptr] = ctx->vlen;
7958 if (++hist_ptr >= MAX_HDR_HISTORY)
7959 hist_ptr = 0;
7960 found++;
7961 }
7962 if (-occ > found)
7963 return 0;
7964 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7965 * find occurrence -occ, so we have to check [hist_ptr+occ].
7966 */
7967 hist_ptr += occ;
7968 if (hist_ptr >= MAX_HDR_HISTORY)
7969 hist_ptr -= MAX_HDR_HISTORY;
7970 *vptr = ptr_hist[hist_ptr];
7971 *vlen = len_hist[hist_ptr];
7972 return 1;
7973}
7974
Willy Tarreaubaaee002006-06-26 02:48:02 +02007975/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007976 * Print a debug line with a header. Always stop at the first CR or LF char,
7977 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7978 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 */
7980void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7981{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007982 int max;
7983 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007984 dir,
7985 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
7986 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 +02007987
7988 for (max = 0; start + max < end; max++)
7989 if (start[max] == '\r' || start[max] == '\n')
7990 break;
7991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007992 UBOUND(max, trash.size - trash.len - 3);
7993 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7994 trash.str[trash.len++] = '\n';
7995 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007996}
7997
Willy Tarreau0937bc42009-12-22 15:03:09 +01007998/*
7999 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8000 * the required fields are properly allocated and that we only need to (re)init
8001 * them. This should be used before processing any new request.
8002 */
8003void http_init_txn(struct session *s)
8004{
8005 struct http_txn *txn = &s->txn;
8006 struct proxy *fe = s->fe;
8007
8008 txn->flags = 0;
8009 txn->status = -1;
8010
Willy Tarreauf64d1412010-10-07 20:06:11 +02008011 txn->cookie_first_date = 0;
8012 txn->cookie_last_date = 0;
8013
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008014 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008015 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008016 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008017 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008018 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008019 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008020 txn->req.chunk_len = 0LL;
8021 txn->req.body_len = 0LL;
8022 txn->rsp.chunk_len = 0LL;
8023 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008024 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8025 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008026 txn->req.chn = s->req;
8027 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008028
8029 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008030
8031 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8032 if (fe->options2 & PR_O2_REQBUG_OK)
8033 txn->req.err_pos = -1; /* let buggy requests pass */
8034
Willy Tarreau46023632010-01-07 22:51:47 +01008035 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008036 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8037
Willy Tarreau46023632010-01-07 22:51:47 +01008038 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008039 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8040
8041 if (txn->hdr_idx.v)
8042 hdr_idx_init(&txn->hdr_idx);
8043}
8044
8045/* to be used at the end of a transaction */
8046void http_end_txn(struct session *s)
8047{
8048 struct http_txn *txn = &s->txn;
8049
8050 /* these ones will have been dynamically allocated */
8051 pool_free2(pool2_requri, txn->uri);
8052 pool_free2(pool2_capture, txn->cli_cookie);
8053 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008054 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008055 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008056
William Lallemanda73203e2012-03-12 12:48:57 +01008057 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008058 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008059 txn->uri = NULL;
8060 txn->srv_cookie = NULL;
8061 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008062
8063 if (txn->req.cap) {
8064 struct cap_hdr *h;
8065 for (h = s->fe->req_cap; h; h = h->next)
8066 pool_free2(h->pool, txn->req.cap[h->index]);
8067 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8068 }
8069
8070 if (txn->rsp.cap) {
8071 struct cap_hdr *h;
8072 for (h = s->fe->rsp_cap; h; h = h->next)
8073 pool_free2(h->pool, txn->rsp.cap[h->index]);
8074 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8075 }
8076
Willy Tarreau0937bc42009-12-22 15:03:09 +01008077}
8078
8079/* to be used at the end of a transaction to prepare a new one */
8080void http_reset_txn(struct session *s)
8081{
8082 http_end_txn(s);
8083 http_init_txn(s);
8084
8085 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008086 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008087 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008088 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008089 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008090 /* re-init store persistence */
8091 s->store_count = 0;
8092
Willy Tarreau0937bc42009-12-22 15:03:09 +01008093 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008094
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008095 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008096
Willy Tarreau739cfba2010-01-25 23:11:14 +01008097 /* We must trim any excess data from the response buffer, because we
8098 * may have blocked an invalid response from a server that we don't
8099 * want to accidentely forward once we disable the analysers, nor do
8100 * we want those data to come along with next response. A typical
8101 * example of such data would be from a buggy server responding to
8102 * a HEAD with some data, or sending more than the advertised
8103 * content-length.
8104 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008105 if (unlikely(s->rep->buf->i))
8106 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008107
Willy Tarreau0937bc42009-12-22 15:03:09 +01008108 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008109 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008110
Willy Tarreaud04e8582010-05-31 12:31:35 +02008111 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008112 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008113
8114 s->req->rex = TICK_ETERNITY;
8115 s->req->wex = TICK_ETERNITY;
8116 s->req->analyse_exp = TICK_ETERNITY;
8117 s->rep->rex = TICK_ETERNITY;
8118 s->rep->wex = TICK_ETERNITY;
8119 s->rep->analyse_exp = TICK_ETERNITY;
8120}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008121
Willy Tarreauff011f22011-01-06 17:51:27 +01008122void free_http_req_rules(struct list *r) {
8123 struct http_req_rule *tr, *pr;
8124
8125 list_for_each_entry_safe(pr, tr, r, list) {
8126 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008127 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008128 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008129
8130 free(pr);
8131 }
8132}
8133
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008134/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008135struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8136{
8137 struct http_req_rule *rule;
8138 int cur_arg;
8139
8140 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8141 if (!rule) {
8142 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008143 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008144 }
8145
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008146 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008147 rule->action = HTTP_REQ_ACT_ALLOW;
8148 cur_arg = 1;
8149 } else if (!strcmp(args[0], "deny")) {
8150 rule->action = HTTP_REQ_ACT_DENY;
8151 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008152 } else if (!strcmp(args[0], "tarpit")) {
8153 rule->action = HTTP_REQ_ACT_TARPIT;
8154 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008155 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008156 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008157 cur_arg = 1;
8158
8159 while(*args[cur_arg]) {
8160 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008161 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008162 cur_arg+=2;
8163 continue;
8164 } else
8165 break;
8166 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008167 } else if (!strcmp(args[0], "set-nice")) {
8168 rule->action = HTTP_REQ_ACT_SET_NICE;
8169 cur_arg = 1;
8170
8171 if (!*args[cur_arg] ||
8172 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8173 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8174 file, linenum, args[0]);
8175 goto out_err;
8176 }
8177 rule->arg.nice = atoi(args[cur_arg]);
8178 if (rule->arg.nice < -1024)
8179 rule->arg.nice = -1024;
8180 else if (rule->arg.nice > 1024)
8181 rule->arg.nice = 1024;
8182 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008183 } else if (!strcmp(args[0], "set-tos")) {
8184#ifdef IP_TOS
8185 char *err;
8186 rule->action = HTTP_REQ_ACT_SET_TOS;
8187 cur_arg = 1;
8188
8189 if (!*args[cur_arg] ||
8190 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8191 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8192 file, linenum, args[0]);
8193 goto out_err;
8194 }
8195
8196 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8197 if (err && *err != '\0') {
8198 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8199 file, linenum, err, args[0]);
8200 goto out_err;
8201 }
8202 cur_arg++;
8203#else
8204 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8205 goto out_err;
8206#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008207 } else if (!strcmp(args[0], "set-mark")) {
8208#ifdef SO_MARK
8209 char *err;
8210 rule->action = HTTP_REQ_ACT_SET_MARK;
8211 cur_arg = 1;
8212
8213 if (!*args[cur_arg] ||
8214 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8215 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8216 file, linenum, args[0]);
8217 goto out_err;
8218 }
8219
8220 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8221 if (err && *err != '\0') {
8222 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8223 file, linenum, err, args[0]);
8224 goto out_err;
8225 }
8226 cur_arg++;
8227 global.last_checks |= LSTCHK_NETADM;
8228#else
8229 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8230 goto out_err;
8231#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008232 } else if (!strcmp(args[0], "set-log-level")) {
8233 rule->action = HTTP_REQ_ACT_SET_LOGL;
8234 cur_arg = 1;
8235
8236 if (!*args[cur_arg] ||
8237 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8238 bad_log_level:
8239 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8240 file, linenum, args[0]);
8241 goto out_err;
8242 }
8243 if (strcmp(args[cur_arg], "silent") == 0)
8244 rule->arg.loglevel = -1;
8245 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8246 goto bad_log_level;
8247 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008248 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8249 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8250 cur_arg = 1;
8251
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008252 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8253 (*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 +01008254 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8255 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008256 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008257 }
8258
8259 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8260 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8261 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008262
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008263 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008264 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8265 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008266 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008267 } else if (strcmp(args[0], "redirect") == 0) {
8268 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008269 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008270
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008271 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008272 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8273 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8274 goto out_err;
8275 }
8276
8277 /* this redirect rule might already contain a parsed condition which
8278 * we'll pass to the http-request rule.
8279 */
8280 rule->action = HTTP_REQ_ACT_REDIR;
8281 rule->arg.redir = redir;
8282 rule->cond = redir->cond;
8283 redir->cond = NULL;
8284 cur_arg = 2;
8285 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008286 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008287 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 +01008288 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008289 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008290 }
8291
8292 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8293 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008294 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008295
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008296 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8297 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8298 file, linenum, args[0], errmsg);
8299 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008300 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008301 }
8302 rule->cond = cond;
8303 }
8304 else if (*args[cur_arg]) {
8305 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8306 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8307 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008308 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008309 }
8310
8311 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008312 out_err:
8313 free(rule);
8314 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008315}
8316
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008317/* parse an "http-respose" rule */
8318struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8319{
8320 struct http_res_rule *rule;
8321 int cur_arg;
8322
8323 rule = calloc(1, sizeof(*rule));
8324 if (!rule) {
8325 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8326 goto out_err;
8327 }
8328
8329 if (!strcmp(args[0], "allow")) {
8330 rule->action = HTTP_RES_ACT_ALLOW;
8331 cur_arg = 1;
8332 } else if (!strcmp(args[0], "deny")) {
8333 rule->action = HTTP_RES_ACT_DENY;
8334 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008335 } else if (!strcmp(args[0], "set-nice")) {
8336 rule->action = HTTP_RES_ACT_SET_NICE;
8337 cur_arg = 1;
8338
8339 if (!*args[cur_arg] ||
8340 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8341 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8342 file, linenum, args[0]);
8343 goto out_err;
8344 }
8345 rule->arg.nice = atoi(args[cur_arg]);
8346 if (rule->arg.nice < -1024)
8347 rule->arg.nice = -1024;
8348 else if (rule->arg.nice > 1024)
8349 rule->arg.nice = 1024;
8350 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008351 } else if (!strcmp(args[0], "set-tos")) {
8352#ifdef IP_TOS
8353 char *err;
8354 rule->action = HTTP_RES_ACT_SET_TOS;
8355 cur_arg = 1;
8356
8357 if (!*args[cur_arg] ||
8358 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8359 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8360 file, linenum, args[0]);
8361 goto out_err;
8362 }
8363
8364 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8365 if (err && *err != '\0') {
8366 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8367 file, linenum, err, args[0]);
8368 goto out_err;
8369 }
8370 cur_arg++;
8371#else
8372 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8373 goto out_err;
8374#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008375 } else if (!strcmp(args[0], "set-mark")) {
8376#ifdef SO_MARK
8377 char *err;
8378 rule->action = HTTP_RES_ACT_SET_MARK;
8379 cur_arg = 1;
8380
8381 if (!*args[cur_arg] ||
8382 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8383 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8384 file, linenum, args[0]);
8385 goto out_err;
8386 }
8387
8388 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8389 if (err && *err != '\0') {
8390 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8391 file, linenum, err, args[0]);
8392 goto out_err;
8393 }
8394 cur_arg++;
8395 global.last_checks |= LSTCHK_NETADM;
8396#else
8397 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8398 goto out_err;
8399#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008400 } else if (!strcmp(args[0], "set-log-level")) {
8401 rule->action = HTTP_RES_ACT_SET_LOGL;
8402 cur_arg = 1;
8403
8404 if (!*args[cur_arg] ||
8405 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8406 bad_log_level:
8407 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8408 file, linenum, args[0]);
8409 goto out_err;
8410 }
8411 if (strcmp(args[cur_arg], "silent") == 0)
8412 rule->arg.loglevel = -1;
8413 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8414 goto bad_log_level;
8415 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008416 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8417 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8418 cur_arg = 1;
8419
8420 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8421 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8422 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8423 file, linenum, args[0]);
8424 goto out_err;
8425 }
8426
8427 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8428 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8429 LIST_INIT(&rule->arg.hdr_add.fmt);
8430
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008431 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008432 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8433 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8434 cur_arg += 2;
8435 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008436 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 +02008437 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8438 goto out_err;
8439 }
8440
8441 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8442 struct acl_cond *cond;
8443 char *errmsg = NULL;
8444
8445 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8446 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8447 file, linenum, args[0], errmsg);
8448 free(errmsg);
8449 goto out_err;
8450 }
8451 rule->cond = cond;
8452 }
8453 else if (*args[cur_arg]) {
8454 Alert("parsing [%s:%d]: 'http-response %s' expects"
8455 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8456 file, linenum, args[0], args[cur_arg]);
8457 goto out_err;
8458 }
8459
8460 return rule;
8461 out_err:
8462 free(rule);
8463 return NULL;
8464}
8465
Willy Tarreau4baae242012-12-27 12:00:31 +01008466/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008467 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8468 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008469 */
8470struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008471 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008472{
8473 struct redirect_rule *rule;
8474 int cur_arg;
8475 int type = REDIRECT_TYPE_NONE;
8476 int code = 302;
8477 const char *destination = NULL;
8478 const char *cookie = NULL;
8479 int cookie_set = 0;
8480 unsigned int flags = REDIRECT_FLAG_NONE;
8481 struct acl_cond *cond = NULL;
8482
8483 cur_arg = 0;
8484 while (*(args[cur_arg])) {
8485 if (strcmp(args[cur_arg], "location") == 0) {
8486 if (!*args[cur_arg + 1])
8487 goto missing_arg;
8488
8489 type = REDIRECT_TYPE_LOCATION;
8490 cur_arg++;
8491 destination = args[cur_arg];
8492 }
8493 else if (strcmp(args[cur_arg], "prefix") == 0) {
8494 if (!*args[cur_arg + 1])
8495 goto missing_arg;
8496
8497 type = REDIRECT_TYPE_PREFIX;
8498 cur_arg++;
8499 destination = args[cur_arg];
8500 }
8501 else if (strcmp(args[cur_arg], "scheme") == 0) {
8502 if (!*args[cur_arg + 1])
8503 goto missing_arg;
8504
8505 type = REDIRECT_TYPE_SCHEME;
8506 cur_arg++;
8507 destination = args[cur_arg];
8508 }
8509 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8510 if (!*args[cur_arg + 1])
8511 goto missing_arg;
8512
8513 cur_arg++;
8514 cookie = args[cur_arg];
8515 cookie_set = 1;
8516 }
8517 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8518 if (!*args[cur_arg + 1])
8519 goto missing_arg;
8520
8521 cur_arg++;
8522 cookie = args[cur_arg];
8523 cookie_set = 0;
8524 }
8525 else if (strcmp(args[cur_arg], "code") == 0) {
8526 if (!*args[cur_arg + 1])
8527 goto missing_arg;
8528
8529 cur_arg++;
8530 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008531 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008532 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008533 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008534 args[cur_arg - 1], args[cur_arg]);
8535 return NULL;
8536 }
8537 }
8538 else if (!strcmp(args[cur_arg],"drop-query")) {
8539 flags |= REDIRECT_FLAG_DROP_QS;
8540 }
8541 else if (!strcmp(args[cur_arg],"append-slash")) {
8542 flags |= REDIRECT_FLAG_APPEND_SLASH;
8543 }
8544 else if (strcmp(args[cur_arg], "if") == 0 ||
8545 strcmp(args[cur_arg], "unless") == 0) {
8546 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8547 if (!cond) {
8548 memprintf(errmsg, "error in condition: %s", *errmsg);
8549 return NULL;
8550 }
8551 break;
8552 }
8553 else {
8554 memprintf(errmsg,
8555 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8556 args[cur_arg]);
8557 return NULL;
8558 }
8559 cur_arg++;
8560 }
8561
8562 if (type == REDIRECT_TYPE_NONE) {
8563 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8564 return NULL;
8565 }
8566
8567 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8568 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008569 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008570
8571 if (!use_fmt) {
8572 /* old-style static redirect rule */
8573 rule->rdr_str = strdup(destination);
8574 rule->rdr_len = strlen(destination);
8575 }
8576 else {
8577 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008578
8579 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8580 * if prefix == "/", we don't want to add anything, otherwise it
8581 * makes it hard for the user to configure a self-redirection.
8582 */
8583 proxy->conf.args.ctx = ARGC_RDR;
8584 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8585 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8586 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8587 }
8588 }
8589
Willy Tarreau4baae242012-12-27 12:00:31 +01008590 if (cookie) {
8591 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8592 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8593 */
8594 rule->cookie_len = strlen(cookie);
8595 if (cookie_set) {
8596 rule->cookie_str = malloc(rule->cookie_len + 10);
8597 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8598 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8599 rule->cookie_len += 9;
8600 } else {
8601 rule->cookie_str = malloc(rule->cookie_len + 21);
8602 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8603 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8604 rule->cookie_len += 20;
8605 }
8606 }
8607 rule->type = type;
8608 rule->code = code;
8609 rule->flags = flags;
8610 LIST_INIT(&rule->list);
8611 return rule;
8612
8613 missing_arg:
8614 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8615 return NULL;
8616}
8617
Willy Tarreau8797c062007-05-07 00:55:35 +02008618/************************************************************************/
8619/* The code below is dedicated to ACL parsing and matching */
8620/************************************************************************/
8621
8622
Willy Tarreau14174bc2012-04-16 14:34:04 +02008623/* This function ensures that the prerequisites for an L7 fetch are ready,
8624 * which means that a request or response is ready. If some data is missing,
8625 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008626 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8627 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008628 *
8629 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008630 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8631 * decide whether or not an HTTP message is present ;
8632 * 0 if the requested data cannot be fetched or if it is certain that
8633 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008634 * 1 if an HTTP message is ready
8635 */
8636static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008637smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008638 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008639{
8640 struct http_txn *txn = l7;
8641 struct http_msg *msg = &txn->req;
8642
8643 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8644 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8645 */
8646
8647 if (unlikely(!s || !txn))
8648 return 0;
8649
8650 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008651 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008652
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008653 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008654 if (unlikely(!s->req))
8655 return 0;
8656
Willy Tarreauaae75e32013-03-29 12:31:49 +01008657 /* If the buffer does not leave enough free space at the end,
8658 * we must first realign it.
8659 */
8660 if (s->req->buf->p > s->req->buf->data &&
8661 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8662 buffer_slow_realign(s->req->buf);
8663
Willy Tarreau14174bc2012-04-16 14:34:04 +02008664 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008665 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008666 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008667
8668 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008669 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008670 http_msg_analyzer(msg, &txn->hdr_idx);
8671
8672 /* Still no valid request ? */
8673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008674 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008675 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008676 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008677 }
8678 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008679 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008680 return 0;
8681 }
8682
8683 /* OK we just got a valid HTTP request. We have some minor
8684 * preparation to perform so that further checks can rely
8685 * on HTTP tests.
8686 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008687
8688 /* If the request was parsed but was too large, we must absolutely
8689 * return an error so that it is not processed. At the moment this
8690 * cannot happen, but if the parsers are to change in the future,
8691 * we want this check to be maintained.
8692 */
8693 if (unlikely(s->req->buf->i + s->req->buf->p >
8694 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8695 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008696 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008697 return 1;
8698 }
8699
Willy Tarreau9b28e032012-10-12 23:49:43 +02008700 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008701 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8702 s->flags |= SN_REDIRECTABLE;
8703
Willy Tarreau506d0502013-07-06 13:29:24 +02008704 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8705 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008706 }
8707
Willy Tarreau506d0502013-07-06 13:29:24 +02008708 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008709 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008710 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008711
8712 /* otherwise everything's ready for the request */
8713 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008714 else {
8715 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008716 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8717 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008718 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008719 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008720 }
8721
8722 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008723 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008724 return 1;
8725}
Willy Tarreau8797c062007-05-07 00:55:35 +02008726
Willy Tarreauc0239e02012-04-16 14:42:55 +02008727#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008728 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 +02008729
Willy Tarreau24e32d82012-04-23 23:55:44 +02008730#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008731 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 +02008732
Willy Tarreau8797c062007-05-07 00:55:35 +02008733
8734/* 1. Check on METHOD
8735 * We use the pre-parsed method if it is known, and store its number as an
8736 * integer. If it is unknown, we use the pointer and the length.
8737 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008738static 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 +02008739{
8740 int len, meth;
8741
Willy Tarreauae8b7962007-06-09 23:10:04 +02008742 len = strlen(*text);
8743 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008744
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008745 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008746 pattern->val.i = meth;
8747 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008748 pattern->ptr.str = strdup(*text);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008749 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008750 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008751 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008752 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008753 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008754 pattern->len = len;
8755 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008756 else
8757 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008758 return 1;
8759}
8760
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008761/* This function fetches the method of current HTTP request and stores
8762 * it in the global pattern struct as a chunk. There are two possibilities :
8763 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8764 * in <len> and <ptr> is NULL ;
8765 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8766 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008767 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008768 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008769static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008770smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008771 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008772{
8773 int meth;
8774 struct http_txn *txn = l7;
8775
Willy Tarreau24e32d82012-04-23 23:55:44 +02008776 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008777
Willy Tarreau8797c062007-05-07 00:55:35 +02008778 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008779 smp->type = SMP_T_UINT;
8780 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008781 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008782 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8783 /* ensure the indexes are not affected */
8784 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008785 smp->type = SMP_T_CSTR;
8786 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008787 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008788 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008789 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008790 return 1;
8791}
8792
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008793/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008794static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008795{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008796 int icase;
8797
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008798
Willy Tarreauf853c462012-04-23 18:53:56 +02008799 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008800 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008801 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008802 return PAT_MATCH;
8803 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008804 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008805
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008806 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8807 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008808 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008809
8810 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008811 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008812 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008813
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008814 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008815 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8816 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008817 return PAT_NOMATCH;
8818 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008819}
8820
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008821static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008822smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008823 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008824{
8825 struct http_txn *txn = l7;
8826 char *ptr;
8827 int len;
8828
Willy Tarreauc0239e02012-04-16 14:42:55 +02008829 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008830
Willy Tarreau8797c062007-05-07 00:55:35 +02008831 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008832 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008833
8834 while ((len-- > 0) && (*ptr++ != '/'));
8835 if (len <= 0)
8836 return 0;
8837
Willy Tarreauf853c462012-04-23 18:53:56 +02008838 smp->type = SMP_T_CSTR;
8839 smp->data.str.str = ptr;
8840 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008841
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008842 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008843 return 1;
8844}
8845
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008846static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008847smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008848 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008849{
8850 struct http_txn *txn = l7;
8851 char *ptr;
8852 int len;
8853
Willy Tarreauc0239e02012-04-16 14:42:55 +02008854 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008855
Willy Tarreauf26b2522012-12-14 08:33:14 +01008856 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8857 return 0;
8858
Willy Tarreau8797c062007-05-07 00:55:35 +02008859 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008860 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008861
8862 while ((len-- > 0) && (*ptr++ != '/'));
8863 if (len <= 0)
8864 return 0;
8865
Willy Tarreauf853c462012-04-23 18:53:56 +02008866 smp->type = SMP_T_CSTR;
8867 smp->data.str.str = ptr;
8868 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008869
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008870 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008871 return 1;
8872}
8873
8874/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008875static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008876smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008877 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008878{
8879 struct http_txn *txn = l7;
8880 char *ptr;
8881 int len;
8882
Willy Tarreauc0239e02012-04-16 14:42:55 +02008883 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008884
Willy Tarreauf26b2522012-12-14 08:33:14 +01008885 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8886 return 0;
8887
Willy Tarreau8797c062007-05-07 00:55:35 +02008888 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008889 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008890
Willy Tarreauf853c462012-04-23 18:53:56 +02008891 smp->type = SMP_T_UINT;
8892 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008893 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008894 return 1;
8895}
8896
8897/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008898static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008899smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008900 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008901{
8902 struct http_txn *txn = l7;
8903
Willy Tarreauc0239e02012-04-16 14:42:55 +02008904 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008905
Willy Tarreauf853c462012-04-23 18:53:56 +02008906 smp->type = SMP_T_CSTR;
8907 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008908 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008909 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008910 return 1;
8911}
8912
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008913static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008914smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008915 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008916{
8917 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008918 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008919
Willy Tarreauc0239e02012-04-16 14:42:55 +02008920 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008921
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008922 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8923 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008924 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008925
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008926 smp->type = SMP_T_IPV4;
8927 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008928 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008929 return 1;
8930}
8931
8932static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008933smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008934 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008935{
8936 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008937 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008938
Willy Tarreauc0239e02012-04-16 14:42:55 +02008939 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008940
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008941 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8942 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
8943 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008944
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008945 smp->type = SMP_T_UINT;
8946 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008947 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008948 return 1;
8949}
8950
Willy Tarreau185b5c42012-04-26 15:11:51 +02008951/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8952 * Accepts an optional argument of type string containing the header field name,
8953 * and an optional argument of type signed or unsigned integer to request an
8954 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008955 * headers are considered from the first one. It does not stop on commas and
8956 * returns full lines instead (useful for User-Agent or Date for example).
8957 */
8958static int
8959smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008960 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008961{
8962 struct http_txn *txn = l7;
8963 struct hdr_idx *idx = &txn->hdr_idx;
8964 struct hdr_ctx *ctx = smp->ctx.a[0];
8965 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8966 int occ = 0;
8967 const char *name_str = NULL;
8968 int name_len = 0;
8969
8970 if (!ctx) {
8971 /* first call */
8972 ctx = &static_hdr_ctx;
8973 ctx->idx = 0;
8974 smp->ctx.a[0] = ctx;
8975 }
8976
8977 if (args) {
8978 if (args[0].type != ARGT_STR)
8979 return 0;
8980 name_str = args[0].data.str.str;
8981 name_len = args[0].data.str.len;
8982
8983 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8984 occ = args[1].data.uint;
8985 }
8986
8987 CHECK_HTTP_MESSAGE_FIRST();
8988
8989 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
8990 /* search for header from the beginning */
8991 ctx->idx = 0;
8992
8993 if (!occ && !(opt & SMP_OPT_ITERATE))
8994 /* no explicit occurrence and single fetch => last header by default */
8995 occ = -1;
8996
8997 if (!occ)
8998 /* prepare to report multiple occurrences for ACL fetches */
8999 smp->flags |= SMP_F_NOT_LAST;
9000
9001 smp->type = SMP_T_CSTR;
9002 smp->flags |= SMP_F_VOL_HDR;
9003 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9004 return 1;
9005
9006 smp->flags &= ~SMP_F_NOT_LAST;
9007 return 0;
9008}
9009
9010/* 6. Check on HTTP header count. The number of occurrences is returned.
9011 * Accepts exactly 1 argument of type string. It does not stop on commas and
9012 * returns full lines instead (useful for User-Agent or Date for example).
9013 */
9014static int
9015smp_fetch_fhdr_cnt(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 Tarreau04ff9f12013-06-10 18:39:42 +02009017{
9018 struct http_txn *txn = l7;
9019 struct hdr_idx *idx = &txn->hdr_idx;
9020 struct hdr_ctx ctx;
9021 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9022 int cnt;
9023
9024 if (!args || args->type != ARGT_STR)
9025 return 0;
9026
9027 CHECK_HTTP_MESSAGE_FIRST();
9028
9029 ctx.idx = 0;
9030 cnt = 0;
9031 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9032 cnt++;
9033
9034 smp->type = SMP_T_UINT;
9035 smp->data.uint = cnt;
9036 smp->flags = SMP_F_VOL_HDR;
9037 return 1;
9038}
9039
9040/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9041 * Accepts an optional argument of type string containing the header field name,
9042 * and an optional argument of type signed or unsigned integer to request an
9043 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009044 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009045 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009046static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009047smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009048 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009049{
9050 struct http_txn *txn = l7;
9051 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009052 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009053 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009054 int occ = 0;
9055 const char *name_str = NULL;
9056 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009057
Willy Tarreaua890d072013-04-02 12:01:06 +02009058 if (!ctx) {
9059 /* first call */
9060 ctx = &static_hdr_ctx;
9061 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009062 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009063 }
9064
Willy Tarreau185b5c42012-04-26 15:11:51 +02009065 if (args) {
9066 if (args[0].type != ARGT_STR)
9067 return 0;
9068 name_str = args[0].data.str.str;
9069 name_len = args[0].data.str.len;
9070
9071 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9072 occ = args[1].data.uint;
9073 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009074
Willy Tarreaue333ec92012-04-16 16:26:40 +02009075 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009076
Willy Tarreau185b5c42012-04-26 15:11:51 +02009077 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009078 /* search for header from the beginning */
9079 ctx->idx = 0;
9080
Willy Tarreau185b5c42012-04-26 15:11:51 +02009081 if (!occ && !(opt & SMP_OPT_ITERATE))
9082 /* no explicit occurrence and single fetch => last header by default */
9083 occ = -1;
9084
9085 if (!occ)
9086 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009087 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009088
Willy Tarreau185b5c42012-04-26 15:11:51 +02009089 smp->type = SMP_T_CSTR;
9090 smp->flags |= SMP_F_VOL_HDR;
9091 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 +02009092 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009093
Willy Tarreau37406352012-04-23 16:16:37 +02009094 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009095 return 0;
9096}
9097
Willy Tarreauc11416f2007-06-17 16:58:38 +02009098/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009099 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009100 */
9101static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009102smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009103 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009104{
9105 struct http_txn *txn = l7;
9106 struct hdr_idx *idx = &txn->hdr_idx;
9107 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009108 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009109 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009110
Willy Tarreau24e32d82012-04-23 23:55:44 +02009111 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009112 return 0;
9113
Willy Tarreaue333ec92012-04-16 16:26:40 +02009114 CHECK_HTTP_MESSAGE_FIRST();
9115
Willy Tarreau33a7e692007-06-10 19:45:56 +02009116 ctx.idx = 0;
9117 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009118 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 +02009119 cnt++;
9120
Willy Tarreauf853c462012-04-23 18:53:56 +02009121 smp->type = SMP_T_UINT;
9122 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009123 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009124 return 1;
9125}
9126
Willy Tarreau185b5c42012-04-26 15:11:51 +02009127/* Fetch an HTTP header's integer value. The integer value is returned. It
9128 * takes a mandatory argument of type string and an optional one of type int
9129 * to designate a specific occurrence. It returns an unsigned integer, which
9130 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009131 */
9132static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009133smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009134 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009135{
Willy Tarreauef38c392013-07-22 16:29:32 +02009136 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009137
Willy Tarreauf853c462012-04-23 18:53:56 +02009138 if (ret > 0) {
9139 smp->type = SMP_T_UINT;
9140 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9141 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009142
Willy Tarreaud53e2422012-04-16 17:21:11 +02009143 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009144}
9145
Cyril Bonté69fa9922012-10-25 00:01:06 +02009146/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9147 * and an optional one of type int to designate a specific occurrence.
9148 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009149 */
9150static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009151smp_fetch_hdr_ip(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 Tarreau106f9792009-09-19 07:54:16 +02009153{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009154 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009155
Willy Tarreauef38c392013-07-22 16:29:32 +02009156 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009157 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9158 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009159 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009160 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009161 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009162 if (smp->data.str.len < temp->size - 1) {
9163 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9164 temp->str[smp->data.str.len] = '\0';
9165 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9166 smp->type = SMP_T_IPV6;
9167 break;
9168 }
9169 }
9170 }
9171
Willy Tarreaud53e2422012-04-16 17:21:11 +02009172 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009173 if (!(smp->flags & SMP_F_NOT_LAST))
9174 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009175 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009176 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009177}
9178
Willy Tarreau737b0c12007-06-10 21:28:46 +02009179/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9180 * the first '/' after the possible hostname, and ends before the possible '?'.
9181 */
9182static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009183smp_fetch_path(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 Tarreau737b0c12007-06-10 21:28:46 +02009185{
9186 struct http_txn *txn = l7;
9187 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009188
Willy Tarreauc0239e02012-04-16 14:42:55 +02009189 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009190
Willy Tarreau9b28e032012-10-12 23:49:43 +02009191 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009192 ptr = http_get_path(txn);
9193 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009194 return 0;
9195
9196 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009197 smp->type = SMP_T_CSTR;
9198 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009199
9200 while (ptr < end && *ptr != '?')
9201 ptr++;
9202
Willy Tarreauf853c462012-04-23 18:53:56 +02009203 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009204 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009205 return 1;
9206}
9207
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009208/* This produces a concatenation of the first occurrence of the Host header
9209 * followed by the path component if it begins with a slash ('/'). This means
9210 * that '*' will not be added, resulting in exactly the first Host entry.
9211 * If no Host header is found, then the path is returned as-is. The returned
9212 * value is stored in the trash so it does not need to be marked constant.
9213 */
9214static int
9215smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009216 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009217{
9218 struct http_txn *txn = l7;
9219 char *ptr, *end, *beg;
9220 struct hdr_ctx ctx;
9221
9222 CHECK_HTTP_MESSAGE_FIRST();
9223
9224 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009225 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 +02009226 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009227 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009228
9229 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009230 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009231 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009232 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009233 smp->data.str.len = ctx.vlen;
9234
9235 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009236 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 +02009237 beg = http_get_path(txn);
9238 if (!beg)
9239 beg = end;
9240
9241 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9242
9243 if (beg < ptr && *beg == '/') {
9244 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9245 smp->data.str.len += ptr - beg;
9246 }
9247
9248 smp->flags = SMP_F_VOL_1ST;
9249 return 1;
9250}
9251
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009252/* This produces a 32-bit hash of the concatenation of the first occurrence of
9253 * the Host header followed by the path component if it begins with a slash ('/').
9254 * This means that '*' will not be added, resulting in exactly the first Host
9255 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009256 * is hashed using the path hash followed by a full avalanche hash and provides a
9257 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009258 * high-traffic sites without having to store whole paths.
9259 */
9260static int
9261smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009262 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009263{
9264 struct http_txn *txn = l7;
9265 struct hdr_ctx ctx;
9266 unsigned int hash = 0;
9267 char *ptr, *beg, *end;
9268 int len;
9269
9270 CHECK_HTTP_MESSAGE_FIRST();
9271
9272 ctx.idx = 0;
9273 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9274 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9275 ptr = ctx.line + ctx.val;
9276 len = ctx.vlen;
9277 while (len--)
9278 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9279 }
9280
9281 /* now retrieve the path */
9282 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9283 beg = http_get_path(txn);
9284 if (!beg)
9285 beg = end;
9286
9287 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9288
9289 if (beg < ptr && *beg == '/') {
9290 while (beg < ptr)
9291 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9292 }
9293 hash = full_hash(hash);
9294
9295 smp->type = SMP_T_UINT;
9296 smp->data.uint = hash;
9297 smp->flags = SMP_F_VOL_1ST;
9298 return 1;
9299}
9300
Willy Tarreau4a550602012-12-09 14:53:32 +01009301/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009302 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9303 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9304 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009305 * that in environments where IPv6 is insignificant, truncating the output to
9306 * 8 bytes would still work.
9307 */
9308static int
9309smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009310 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009311{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009312 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009313 struct connection *cli_conn = objt_conn(l4->si[0].end);
9314
9315 if (!cli_conn)
9316 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009317
Willy Tarreauef38c392013-07-22 16:29:32 +02009318 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009319 return 0;
9320
Willy Tarreau47ca5452012-12-23 20:22:19 +01009321 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009322 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9323 temp->len += sizeof(smp->data.uint);
9324
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009325 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009326 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009327 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009328 temp->len += 4;
9329 break;
9330 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009331 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009332 temp->len += 16;
9333 break;
9334 default:
9335 return 0;
9336 }
9337
9338 smp->data.str = *temp;
9339 smp->type = SMP_T_BIN;
9340 return 1;
9341}
9342
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009343static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009344smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009345 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009346{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009347 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9348 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9349 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009350
Willy Tarreau24e32d82012-04-23 23:55:44 +02009351 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009352
Willy Tarreauf853c462012-04-23 18:53:56 +02009353 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009354 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009355 return 1;
9356}
9357
Willy Tarreau7f18e522010-10-22 20:04:13 +02009358/* return a valid test if the current request is the first one on the connection */
9359static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009360smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009361 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009362{
9363 if (!s)
9364 return 0;
9365
Willy Tarreauf853c462012-04-23 18:53:56 +02009366 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009367 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009368 return 1;
9369}
9370
Willy Tarreau34db1082012-04-19 17:16:54 +02009371/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009372static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009373smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009374 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009375{
9376
Willy Tarreau24e32d82012-04-23 23:55:44 +02009377 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009378 return 0;
9379
Willy Tarreauc0239e02012-04-16 14:42:55 +02009380 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009381
Willy Tarreauc0239e02012-04-16 14:42:55 +02009382 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009383 return 0;
9384
Willy Tarreauf853c462012-04-23 18:53:56 +02009385 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009386 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 +01009387 return 1;
9388}
Willy Tarreau8797c062007-05-07 00:55:35 +02009389
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009390/* Accepts exactly 1 argument of type userlist */
9391static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009392smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009393 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009394{
9395
9396 if (!args || args->type != ARGT_USR)
9397 return 0;
9398
9399 CHECK_HTTP_MESSAGE_FIRST();
9400
9401 if (!get_http_auth(l4))
9402 return 0;
9403
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009404 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009405 smp->ctx.a[0] = args->data.usr; /* user list */
9406 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9407 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9408
9409 /* if the user does not belong to the userlist or has a wrong password,
9410 * report that it unconditionally does not match. Otherwise we return
9411 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009412 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009413 */
9414 smp->type = SMP_T_BOOL;
9415 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9416 if (smp->data.uint)
9417 smp->type = SMP_T_UINT;
9418
9419 return 1;
9420}
9421
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009422/* Try to find the next occurrence of a cookie name in a cookie header value.
9423 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9424 * the cookie value is returned into *value and *value_l, and the function
9425 * returns a pointer to the next pointer to search from if the value was found.
9426 * Otherwise if the cookie was not found, NULL is returned and neither value
9427 * nor value_l are touched. The input <hdr> string should first point to the
9428 * header's value, and the <hdr_end> pointer must point to the first character
9429 * not part of the value. <list> must be non-zero if value may represent a list
9430 * of values (cookie headers). This makes it faster to abort parsing when no
9431 * list is expected.
9432 */
9433static char *
9434extract_cookie_value(char *hdr, const char *hdr_end,
9435 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009436 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009437{
9438 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9439 char *next;
9440
9441 /* we search at least a cookie name followed by an equal, and more
9442 * generally something like this :
9443 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9444 */
9445 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9446 /* Iterate through all cookies on this line */
9447
9448 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9449 att_beg++;
9450
9451 /* find att_end : this is the first character after the last non
9452 * space before the equal. It may be equal to hdr_end.
9453 */
9454 equal = att_end = att_beg;
9455
9456 while (equal < hdr_end) {
9457 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9458 break;
9459 if (http_is_spht[(unsigned char)*equal++])
9460 continue;
9461 att_end = equal;
9462 }
9463
9464 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9465 * is between <att_beg> and <equal>, both may be identical.
9466 */
9467
9468 /* look for end of cookie if there is an equal sign */
9469 if (equal < hdr_end && *equal == '=') {
9470 /* look for the beginning of the value */
9471 val_beg = equal + 1;
9472 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9473 val_beg++;
9474
9475 /* find the end of the value, respecting quotes */
9476 next = find_cookie_value_end(val_beg, hdr_end);
9477
9478 /* make val_end point to the first white space or delimitor after the value */
9479 val_end = next;
9480 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9481 val_end--;
9482 } else {
9483 val_beg = val_end = next = equal;
9484 }
9485
9486 /* We have nothing to do with attributes beginning with '$'. However,
9487 * they will automatically be removed if a header before them is removed,
9488 * since they're supposed to be linked together.
9489 */
9490 if (*att_beg == '$')
9491 continue;
9492
9493 /* Ignore cookies with no equal sign */
9494 if (equal == next)
9495 continue;
9496
9497 /* Now we have the cookie name between att_beg and att_end, and
9498 * its value between val_beg and val_end.
9499 */
9500
9501 if (att_end - att_beg == cookie_name_l &&
9502 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9503 /* let's return this value and indicate where to go on from */
9504 *value = val_beg;
9505 *value_l = val_end - val_beg;
9506 return next + 1;
9507 }
9508
9509 /* Set-Cookie headers only have the name in the first attr=value part */
9510 if (!list)
9511 break;
9512 }
9513
9514 return NULL;
9515}
9516
Willy Tarreaue333ec92012-04-16 16:26:40 +02009517/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009518 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009519 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009520 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009521 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009522 * Accepts exactly 1 argument of type string. If the input options indicate
9523 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009524 */
9525static int
Willy Tarreau51539362012-05-08 12:46:28 +02009526smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009527 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009528{
9529 struct http_txn *txn = l7;
9530 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009531 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009532 const struct http_msg *msg;
9533 const char *hdr_name;
9534 int hdr_name_len;
9535 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009536 int occ = 0;
9537 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009538
Willy Tarreau24e32d82012-04-23 23:55:44 +02009539 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009540 return 0;
9541
Willy Tarreaua890d072013-04-02 12:01:06 +02009542 if (!ctx) {
9543 /* first call */
9544 ctx = &static_hdr_ctx;
9545 ctx->idx = 0;
9546 smp->ctx.a[2] = ctx;
9547 }
9548
Willy Tarreaue333ec92012-04-16 16:26:40 +02009549 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009550
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009551 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009552 msg = &txn->req;
9553 hdr_name = "Cookie";
9554 hdr_name_len = 6;
9555 } else {
9556 msg = &txn->rsp;
9557 hdr_name = "Set-Cookie";
9558 hdr_name_len = 10;
9559 }
9560
Willy Tarreau28376d62012-04-26 21:26:10 +02009561 if (!occ && !(opt & SMP_OPT_ITERATE))
9562 /* no explicit occurrence and single fetch => last cookie by default */
9563 occ = -1;
9564
9565 /* OK so basically here, either we want only one value and it's the
9566 * last one, or we want to iterate over all of them and we fetch the
9567 * next one.
9568 */
9569
Willy Tarreau9b28e032012-10-12 23:49:43 +02009570 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009571 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009572 /* search for the header from the beginning, we must first initialize
9573 * the search parameters.
9574 */
Willy Tarreau37406352012-04-23 16:16:37 +02009575 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009576 ctx->idx = 0;
9577 }
9578
Willy Tarreau28376d62012-04-26 21:26:10 +02009579 smp->flags |= SMP_F_VOL_HDR;
9580
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009581 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009582 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9583 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009584 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9585 goto out;
9586
Willy Tarreau24e32d82012-04-23 23:55:44 +02009587 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009588 continue;
9589
Willy Tarreau37406352012-04-23 16:16:37 +02009590 smp->ctx.a[0] = ctx->line + ctx->val;
9591 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009592 }
9593
Willy Tarreauf853c462012-04-23 18:53:56 +02009594 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009595 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009596 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009597 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009598 &smp->data.str.str,
9599 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009600 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009601 found = 1;
9602 if (occ >= 0) {
9603 /* one value was returned into smp->data.str.{str,len} */
9604 smp->flags |= SMP_F_NOT_LAST;
9605 return 1;
9606 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009607 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009608 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009609 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009610 /* all cookie headers and values were scanned. If we're looking for the
9611 * last occurrence, we may return it now.
9612 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009613 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009614 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009615 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009616}
9617
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009618/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009619 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009620 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009621 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009622 */
9623static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009624smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009625 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009626{
9627 struct http_txn *txn = l7;
9628 struct hdr_idx *idx = &txn->hdr_idx;
9629 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009630 const struct http_msg *msg;
9631 const char *hdr_name;
9632 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009633 int cnt;
9634 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009635 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009636
Willy Tarreau24e32d82012-04-23 23:55:44 +02009637 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009638 return 0;
9639
Willy Tarreaue333ec92012-04-16 16:26:40 +02009640 CHECK_HTTP_MESSAGE_FIRST();
9641
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009642 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009643 msg = &txn->req;
9644 hdr_name = "Cookie";
9645 hdr_name_len = 6;
9646 } else {
9647 msg = &txn->rsp;
9648 hdr_name = "Set-Cookie";
9649 hdr_name_len = 10;
9650 }
9651
Willy Tarreau9b28e032012-10-12 23:49:43 +02009652 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009653 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009654 ctx.idx = 0;
9655 cnt = 0;
9656
9657 while (1) {
9658 /* Note: val_beg == NULL every time we need to fetch a new header */
9659 if (!val_beg) {
9660 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9661 break;
9662
Willy Tarreau24e32d82012-04-23 23:55:44 +02009663 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009664 continue;
9665
9666 val_beg = ctx.line + ctx.val;
9667 val_end = val_beg + ctx.vlen;
9668 }
9669
Willy Tarreauf853c462012-04-23 18:53:56 +02009670 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009671 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009672 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009673 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009674 &smp->data.str.str,
9675 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009676 cnt++;
9677 }
9678 }
9679
Willy Tarreauf853c462012-04-23 18:53:56 +02009680 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009681 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009682 return 1;
9683}
9684
Willy Tarreau51539362012-05-08 12:46:28 +02009685/* Fetch an cookie's integer value. The integer value is returned. It
9686 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9687 */
9688static int
9689smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009690 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009691{
Willy Tarreauef38c392013-07-22 16:29:32 +02009692 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009693
9694 if (ret > 0) {
9695 smp->type = SMP_T_UINT;
9696 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9697 }
9698
9699 return ret;
9700}
9701
Willy Tarreau8797c062007-05-07 00:55:35 +02009702/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009703/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009704/************************************************************************/
9705
David Cournapeau16023ee2010-12-23 20:55:41 +09009706/*
9707 * Given a path string and its length, find the position of beginning of the
9708 * query string. Returns NULL if no query string is found in the path.
9709 *
9710 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9711 *
9712 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9713 */
bedis4c75cca2012-10-05 08:38:24 +02009714static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009715{
9716 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009717
bedis4c75cca2012-10-05 08:38:24 +02009718 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009719 return p ? p + 1 : NULL;
9720}
9721
bedis4c75cca2012-10-05 08:38:24 +02009722static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009723{
bedis4c75cca2012-10-05 08:38:24 +02009724 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009725}
9726
9727/*
9728 * Given a url parameter, find the starting position of the first occurence,
9729 * or NULL if the parameter is not found.
9730 *
9731 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9732 * the function will return query_string+8.
9733 */
9734static char*
9735find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009736 char* url_param_name, size_t url_param_name_l,
9737 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009738{
9739 char *pos, *last;
9740
9741 pos = query_string;
9742 last = query_string + query_string_l - url_param_name_l - 1;
9743
9744 while (pos <= last) {
9745 if (pos[url_param_name_l] == '=') {
9746 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9747 return pos;
9748 pos += url_param_name_l + 1;
9749 }
bedis4c75cca2012-10-05 08:38:24 +02009750 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009751 pos++;
9752 pos++;
9753 }
9754 return NULL;
9755}
9756
9757/*
9758 * Given a url parameter name, returns its value and size into *value and
9759 * *value_l respectively, and returns non-zero. If the parameter is not found,
9760 * zero is returned and value/value_l are not touched.
9761 */
9762static int
9763find_url_param_value(char* path, size_t path_l,
9764 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009765 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009766{
9767 char *query_string, *qs_end;
9768 char *arg_start;
9769 char *value_start, *value_end;
9770
bedis4c75cca2012-10-05 08:38:24 +02009771 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009772 if (!query_string)
9773 return 0;
9774
9775 qs_end = path + path_l;
9776 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009777 url_param_name, url_param_name_l,
9778 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009779 if (!arg_start)
9780 return 0;
9781
9782 value_start = arg_start + url_param_name_l + 1;
9783 value_end = value_start;
9784
bedis4c75cca2012-10-05 08:38:24 +02009785 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009786 value_end++;
9787
9788 *value = value_start;
9789 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009790 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009791}
9792
9793static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009794smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009795 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009796{
bedis4c75cca2012-10-05 08:38:24 +02009797 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009798 struct http_txn *txn = l7;
9799 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009800
bedis4c75cca2012-10-05 08:38:24 +02009801 if (!args || args[0].type != ARGT_STR ||
9802 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009803 return 0;
9804
9805 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009806
bedis4c75cca2012-10-05 08:38:24 +02009807 if (args[1].type)
9808 delim = *args[1].data.str.str;
9809
Willy Tarreau9b28e032012-10-12 23:49:43 +02009810 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009811 args->data.str.str, args->data.str.len,
9812 &smp->data.str.str, &smp->data.str.len,
9813 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009814 return 0;
9815
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009816 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009817 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009818 return 1;
9819}
9820
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009821/* Return the signed integer value for the specified url parameter (see url_param
9822 * above).
9823 */
9824static int
9825smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009826 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009827{
Willy Tarreauef38c392013-07-22 16:29:32 +02009828 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009829
9830 if (ret > 0) {
9831 smp->type = SMP_T_UINT;
9832 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9833 }
9834
9835 return ret;
9836}
9837
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009838/* This produces a 32-bit hash of the concatenation of the first occurrence of
9839 * the Host header followed by the path component if it begins with a slash ('/').
9840 * This means that '*' will not be added, resulting in exactly the first Host
9841 * entry. If no Host header is found, then the path is used. The resulting value
9842 * is hashed using the url hash followed by a full avalanche hash and provides a
9843 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9844 * high-traffic sites without having to store whole paths.
9845 * this differs from the base32 functions in that it includes the url parameters
9846 * as well as the path
9847 */
9848static int
9849smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009850 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009851{
9852 struct http_txn *txn = l7;
9853 struct hdr_ctx ctx;
9854 unsigned int hash = 0;
9855 char *ptr, *beg, *end;
9856 int len;
9857
9858 CHECK_HTTP_MESSAGE_FIRST();
9859
9860 ctx.idx = 0;
9861 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9862 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9863 ptr = ctx.line + ctx.val;
9864 len = ctx.vlen;
9865 while (len--)
9866 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9867 }
9868
9869 /* now retrieve the path */
9870 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9871 beg = http_get_path(txn);
9872 if (!beg)
9873 beg = end;
9874
9875 for (ptr = beg; ptr < end ; ptr++);
9876
9877 if (beg < ptr && *beg == '/') {
9878 while (beg < ptr)
9879 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9880 }
9881 hash = full_hash(hash);
9882
9883 smp->type = SMP_T_UINT;
9884 smp->data.uint = hash;
9885 smp->flags = SMP_F_VOL_1ST;
9886 return 1;
9887}
9888
9889/* This concatenates the source address with the 32-bit hash of the Host and
9890 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9891 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9892 * on the source address length. The URL hash is stored before the address so
9893 * that in environments where IPv6 is insignificant, truncating the output to
9894 * 8 bytes would still work.
9895 */
9896static int
9897smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009898 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009899{
9900 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009901 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009902
Willy Tarreaue155ec22013-11-18 18:33:22 +01009903 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009904 return 0;
9905
9906 temp = get_trash_chunk();
9907 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9908 temp->len += sizeof(smp->data.uint);
9909
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009910 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009911 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009912 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009913 temp->len += 4;
9914 break;
9915 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009916 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009917 temp->len += 16;
9918 break;
9919 default:
9920 return 0;
9921 }
9922
9923 smp->data.str = *temp;
9924 smp->type = SMP_T_BIN;
9925 return 1;
9926}
9927
Willy Tarreau185b5c42012-04-26 15:11:51 +02009928/* This function is used to validate the arguments passed to any "hdr" fetch
9929 * keyword. These keywords support an optional positive or negative occurrence
9930 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9931 * is assumed that the types are already the correct ones. Returns 0 on error,
9932 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9933 * error message in case of error, that the caller is responsible for freeing.
9934 * The initial location must either be freeable or NULL.
9935 */
9936static int val_hdr(struct arg *arg, char **err_msg)
9937{
9938 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009939 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009940 return 0;
9941 }
9942 return 1;
9943}
9944
Willy Tarreau276fae92013-07-25 14:36:01 +02009945/* takes an UINT value on input supposed to represent the time since EPOCH,
9946 * adds an optional offset found in args[0] and emits a string representing
9947 * the date in RFC-1123/5322 format.
9948 */
9949static int sample_conv_http_date(const struct arg *args, struct sample *smp)
9950{
9951 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
9952 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
9953 struct chunk *temp;
9954 struct tm *tm;
9955 time_t curr_date = smp->data.uint;
9956
9957 /* add offset */
9958 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
9959 curr_date += args[0].data.sint;
9960
9961 tm = gmtime(&curr_date);
9962
9963 temp = get_trash_chunk();
9964 temp->len = snprintf(temp->str, temp->size - temp->len,
9965 "%s, %02d %s %04d %02d:%02d:%02d GMT",
9966 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
9967 tm->tm_hour, tm->tm_min, tm->tm_sec);
9968
9969 smp->data.str = *temp;
9970 smp->type = SMP_T_STR;
9971 return 1;
9972}
9973
Willy Tarreau4a568972010-05-12 08:08:50 +02009974/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009975/* All supported ACL keywords must be declared here. */
9976/************************************************************************/
9977
9978/* Note: must not be declared <const> as its list will be overwritten.
9979 * Please take care of keeping this list alphabetically sorted.
9980 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009981static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009982 { "base", "base", pat_parse_str, pat_match_str },
9983 { "base_beg", "base", pat_parse_str, pat_match_beg },
9984 { "base_dir", "base", pat_parse_str, pat_match_dir },
9985 { "base_dom", "base", pat_parse_str, pat_match_dom },
9986 { "base_end", "base", pat_parse_str, pat_match_end },
9987 { "base_len", "base", pat_parse_int, pat_match_len },
9988 { "base_reg", "base", pat_parse_reg, pat_match_reg },
9989 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009990
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009991 { "cook", "req.cook", pat_parse_str, pat_match_str },
9992 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
9993 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
9994 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
9995 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
9996 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
9997 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
9998 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009999
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010000 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10001 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10002 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10003 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10004 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10005 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10006 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10007 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010008
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010009 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010010
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010011 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010012
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010013 { "path", "path", pat_parse_str, pat_match_str },
10014 { "path_beg", "path", pat_parse_str, pat_match_beg },
10015 { "path_dir", "path", pat_parse_str, pat_match_dir },
10016 { "path_dom", "path", pat_parse_str, pat_match_dom },
10017 { "path_end", "path", pat_parse_str, pat_match_end },
10018 { "path_len", "path", pat_parse_int, pat_match_len },
10019 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10020 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010021
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010022 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10023 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010024
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010025 { "scook", "res.cook", pat_parse_str, pat_match_str },
10026 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10027 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10028 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10029 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10030 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10031 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10032 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010033
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010034 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10035 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10036 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10037 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10038 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10039 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10040 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10041 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010042
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010043 { "url", "url", pat_parse_str, pat_match_str },
10044 { "url_beg", "url", pat_parse_str, pat_match_beg },
10045 { "url_dir", "url", pat_parse_str, pat_match_dir },
10046 { "url_dom", "url", pat_parse_str, pat_match_dom },
10047 { "url_end", "url", pat_parse_str, pat_match_end },
10048 { "url_len", "url", pat_parse_int, pat_match_len },
10049 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10050 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010051
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010052 { "urlp", "urlp", pat_parse_str, pat_match_str },
10053 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10054 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10055 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10056 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10057 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10058 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10059 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010060
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010061 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010062}};
10063
10064/************************************************************************/
10065/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010066/************************************************************************/
10067/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010068static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010069 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10070 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10071 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10072
10073 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10074 * are only here to match the ACL's name, are request-only and are used
10075 * for ACL compatibility only.
10076 */
10077 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10078 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10079 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10080 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10081
10082 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10083 * only here to match the ACL's name, are request-only and are used for
10084 * ACL compatibility only.
10085 */
10086 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10087 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10088 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10089 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10090
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010091 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10092 { "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 +010010093 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10094 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10095 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010096
10097 /* HTTP protocol on the request path */
10098 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010099 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010100
10101 /* HTTP version on the request path */
10102 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010103 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010104
10105 /* HTTP version on the response path */
10106 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010107 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10108
Willy Tarreau18ed2562013-01-14 15:56:36 +010010109 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10110 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10111 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10112 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10113
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010114 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10115 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010116 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10117 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10118 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10119 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10120
10121 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10122 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10123 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10124 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10125
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010126 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10127 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010128 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10129 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10130 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10131 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10132
Willy Tarreau409bcde2013-01-08 00:31:00 +010010133 /* scook is valid only on the response and is used for ACL compatibility */
10134 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10135 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10136 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10137 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10138
10139 /* shdr is valid only on the response and is used for ACL compatibility */
10140 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10141 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10142 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10143 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10144
10145 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10146 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010147 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10148 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010149 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10150 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10151 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10152 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10153 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10154 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010155}};
10156
Willy Tarreau8797c062007-05-07 00:55:35 +020010157
Willy Tarreau276fae92013-07-25 14:36:01 +020010158/* Note: must not be declared <const> as its list will be overwritten */
10159static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10160 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10161 { NULL, NULL, 0, 0, 0 },
10162}};
10163
Willy Tarreau8797c062007-05-07 00:55:35 +020010164__attribute__((constructor))
10165static void __http_protocol_init(void)
10166{
10167 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010168 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010169 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010170}
10171
10172
Willy Tarreau58f10d72006-12-04 02:26:12 +010010173/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010174 * Local variables:
10175 * c-indent-level: 8
10176 * c-basic-offset: 8
10177 * End:
10178 */