blob: 7c0c9159b2e06b9bbd356c51f752f32a23070856 [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))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100355/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200356 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100357 * RFC2616 for those chars.
358 */
359
360const char http_is_spht[256] = {
361 [' '] = 1, ['\t'] = 1,
362};
363
364const char http_is_crlf[256] = {
365 ['\r'] = 1, ['\n'] = 1,
366};
367
368const char http_is_lws[256] = {
369 [' '] = 1, ['\t'] = 1,
370 ['\r'] = 1, ['\n'] = 1,
371};
372
373const char http_is_sep[256] = {
374 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
375 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
376 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
377 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
378 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
379};
380
381const char http_is_ctl[256] = {
382 [0 ... 31] = 1,
383 [127] = 1,
384};
385
386/*
387 * A token is any ASCII char that is neither a separator nor a CTL char.
388 * Do not overwrite values in assignment since gcc-2.95 will not handle
389 * them correctly. Instead, define every non-CTL char's status.
390 */
391const char http_is_token[256] = {
392 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
393 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
394 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
395 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
396 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
397 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
398 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
399 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
400 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
401 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
402 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
403 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
404 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
405 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
406 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
407 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
408 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
409 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
410 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
411 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
412 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
413 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
414 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
415 ['|'] = 1, ['}'] = 0, ['~'] = 1,
416};
417
418
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100419/*
420 * An http ver_token is any ASCII which can be found in an HTTP version,
421 * which includes 'H', 'T', 'P', '/', '.' and any digit.
422 */
423const char http_is_ver_token[256] = {
424 ['.'] = 1, ['/'] = 1,
425 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
426 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
427 ['H'] = 1, ['P'] = 1, ['T'] = 1,
428};
429
430
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100431/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100432 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
433 */
434#if defined(DEBUG_FSM)
435static void http_silent_debug(int line, struct session *s)
436{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100437 chunk_printf(&trash,
438 "[%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",
439 line,
440 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
441 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);
442 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100443
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444 chunk_printf(&trash,
445 " %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",
446 line,
447 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
448 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);
449 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100450}
451#else
452#define http_silent_debug(l,s) do { } while (0)
453#endif
454
455/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100456 * Adds a header and its CRLF at the tail of the message's buffer, just before
457 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100458 * The header is also automatically added to the index <hdr_idx>, and the end
459 * of headers is automatically adjusted. The number of bytes added is returned
460 * on success, otherwise <0 is returned indicating an error.
461 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100462int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463{
464 int bytes, len;
465
466 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200467 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100468 if (!bytes)
469 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100470 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100471 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
472}
473
474/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100475 * Adds a header and its CRLF at the tail of the message's buffer, just before
476 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100477 * the buffer is only opened and the space reserved, but nothing is copied.
478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail2(struct http_msg *msg,
483 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100484{
485 int bytes;
486
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493
494/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100495 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
496 * If so, returns the position of the first non-space character relative to
497 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
498 * to return a pointer to the place after the first space. Returns 0 if the
499 * header name does not match. Checks are case-insensitive.
500 */
501int http_header_match2(const char *hdr, const char *end,
502 const char *name, int len)
503{
504 const char *val;
505
506 if (hdr + len >= end)
507 return 0;
508 if (hdr[len] != ':')
509 return 0;
510 if (strncasecmp(hdr, name, len) != 0)
511 return 0;
512 val = hdr + len + 1;
513 while (val < end && HTTP_IS_SPHT(*val))
514 val++;
515 if ((val >= end) && (len + 2 <= end - hdr))
516 return len + 2; /* we may replace starting from second space */
517 return val - hdr;
518}
519
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200520/* Find the first or next occurrence of header <name> in message buffer <sol>
521 * using headers index <idx>, and return it in the <ctx> structure. This
522 * structure holds everything necessary to use the header and find next
523 * occurrence. If its <idx> member is 0, the header is searched from the
524 * beginning. Otherwise, the next occurrence is returned. The function returns
525 * 1 when it finds a value, and 0 when there is no more. It is very similar to
526 * http_find_header2() except that it is designed to work with full-line headers
527 * whose comma is not a delimiter but is part of the syntax. As a special case,
528 * if ctx->val is NULL when searching for a new values of a header, the current
529 * header is rescanned. This allows rescanning after a header deletion.
530 */
531int http_find_full_header2(const char *name, int len,
532 char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 char *eol, *sov;
536 int cur_idx, old_idx;
537
538 cur_idx = ctx->idx;
539 if (cur_idx) {
540 /* We have previously returned a header, let's search another one */
541 sol = ctx->line;
542 eol = sol + idx->v[cur_idx].len;
543 goto next_hdr;
544 }
545
546 /* first request for this header */
547 sol += hdr_idx_first_pos(idx);
548 old_idx = 0;
549 cur_idx = hdr_idx_first_idx(idx);
550 while (cur_idx) {
551 eol = sol + idx->v[cur_idx].len;
552
553 if (len == 0) {
554 /* No argument was passed, we want any header.
555 * To achieve this, we simply build a fake request. */
556 while (sol + len < eol && sol[len] != ':')
557 len++;
558 name = sol;
559 }
560
561 if ((len < eol - sol) &&
562 (sol[len] == ':') &&
563 (strncasecmp(sol, name, len) == 0)) {
564 ctx->del = len;
565 sov = sol + len + 1;
566 while (sov < eol && http_is_lws[(unsigned char)*sov])
567 sov++;
568
569 ctx->line = sol;
570 ctx->prev = old_idx;
571 ctx->idx = cur_idx;
572 ctx->val = sov - sol;
573 ctx->tws = 0;
574 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
575 eol--;
576 ctx->tws++;
577 }
578 ctx->vlen = eol - sov;
579 return 1;
580 }
581 next_hdr:
582 sol = eol + idx->v[cur_idx].cr + 1;
583 old_idx = cur_idx;
584 cur_idx = idx->v[cur_idx].next;
585 }
586 return 0;
587}
588
Willy Tarreau68085d82010-01-18 14:54:04 +0100589/* Find the end of the header value contained between <s> and <e>. See RFC2616,
590 * par 2.2 for more information. Note that it requires a valid header to return
591 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200592 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100593char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594{
595 int quoted, qdpair;
596
597 quoted = qdpair = 0;
598 for (; s < e; s++) {
599 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200600 else if (quoted) {
601 if (*s == '\\') qdpair = 1;
602 else if (*s == '"') quoted = 0;
603 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 else if (*s == '"') quoted = 1;
605 else if (*s == ',') return s;
606 }
607 return s;
608}
609
610/* Find the first or next occurrence of header <name> in message buffer <sol>
611 * using headers index <idx>, and return it in the <ctx> structure. This
612 * structure holds everything necessary to use the header and find next
613 * occurrence. If its <idx> member is 0, the header is searched from the
614 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 * 1 when it finds a value, and 0 when there is no more. It is designed to work
616 * with headers defined as comma-separated lists. As a special case, if ctx->val
617 * is NULL when searching for a new values of a header, the current header is
618 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200619 */
620int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200622 struct hdr_ctx *ctx)
623{
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 char *eol, *sov;
625 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 cur_idx = ctx->idx;
628 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200629 /* We have previously returned a value, let's search
630 * another one on the same line.
631 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200635 eol = sol + idx->v[cur_idx].len;
636
637 if (sov >= eol)
638 /* no more values in this header */
639 goto next_hdr;
640
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 /* values remaining for this header, skip the comma but save it
642 * for later use (eg: for header deletion).
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sov++;
645 while (sov < eol && http_is_lws[(unsigned char)*sov])
646 sov++;
647
648 goto return_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100669 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100673
Willy Tarreau33a7e692007-06-10 19:45:56 +0200674 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100675 ctx->prev = old_idx;
676 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 ctx->idx = cur_idx;
678 ctx->val = sov - sol;
679
680 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200681 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200683 eol--;
684 ctx->tws++;
685 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100691 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
697int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200699 struct hdr_ctx *ctx)
700{
701 return http_find_header2(name, strlen(name), sol, idx, ctx);
702}
703
Willy Tarreau68085d82010-01-18 14:54:04 +0100704/* Remove one value of a header. This only works on a <ctx> returned by one of
705 * the http_find_header functions. The value is removed, as well as surrounding
706 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100707 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 * message <msg>. The new index is returned. If it is zero, it means there is
709 * no more header, so any processing may stop. The ctx is always left in a form
710 * that can be handled by http_find_header2() to find next occurrence.
711 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100713{
714 int cur_idx = ctx->idx;
715 char *sol = ctx->line;
716 struct hdr_idx_elem *hdr;
717 int delta, skip_comma;
718
719 if (!cur_idx)
720 return 0;
721
722 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200725 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100726 http_msg_move_end(msg, delta);
727 idx->used--;
728 hdr->len = 0; /* unused entry */
729 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100730 if (idx->tail == ctx->idx)
731 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100732 ctx->idx = ctx->prev; /* walk back to the end of previous header */
733 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
734 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 return ctx->idx;
737 }
738
739 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200740 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
741 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100742 */
743
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200744 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200745 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100747 NULL, 0);
748 hdr->len += delta;
749 http_msg_move_end(msg, delta);
750 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200751 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100752 return ctx->idx;
753}
754
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100755/* This function handles a server error at the stream interface level. The
756 * stream interface is assumed to be already in a closed state. An optional
757 * message is copied into the input buffer, and an HTTP status code stored.
758 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100759 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100761static void http_server_error(struct session *t, struct stream_interface *si,
762 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200764 channel_auto_read(si->ob);
765 channel_abort(si->ob);
766 channel_auto_close(si->ob);
767 channel_erase(si->ob);
768 channel_auto_close(si->ib);
769 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100770 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100771 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200772 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 }
774 if (!(t->flags & SN_ERR_MASK))
775 t->flags |= err;
776 if (!(t->flags & SN_FINST_MASK))
777 t->flags |= finst;
778}
779
Willy Tarreau80587432006-12-24 17:47:20 +0100780/* This function returns the appropriate error location for the given session
781 * and message.
782 */
783
Willy Tarreau783f2582012-09-04 12:19:04 +0200784struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100785{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200786 if (s->be->errmsg[msgnum].str)
787 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100788 else if (s->fe->errmsg[msgnum].str)
789 return &s->fe->errmsg[msgnum];
790 else
791 return &http_err_chunks[msgnum];
792}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793
Willy Tarreau53b6c742006-12-17 13:37:46 +0100794/*
795 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
796 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
797 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100798static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100799{
800 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100801 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100802
803 m = ((unsigned)*str - 'A');
804
805 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100806 for (h = http_methods[m]; h->len > 0; h++) {
807 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100808 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100810 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 };
812 return HTTP_METH_OTHER;
813 }
814 return HTTP_METH_NONE;
815
816}
817
Willy Tarreau21d2af32008-02-14 20:25:24 +0100818/* Parse the URI from the given transaction (which is assumed to be in request
819 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
820 * It is returned otherwise.
821 */
822static char *
823http_get_path(struct http_txn *txn)
824{
825 char *ptr, *end;
826
Willy Tarreau9b28e032012-10-12 23:49:43 +0200827 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100828 end = ptr + txn->req.sl.rq.u_l;
829
830 if (ptr >= end)
831 return NULL;
832
833 /* RFC2616, par. 5.1.2 :
834 * Request-URI = "*" | absuri | abspath | authority
835 */
836
837 if (*ptr == '*')
838 return NULL;
839
840 if (isalpha((unsigned char)*ptr)) {
841 /* this is a scheme as described by RFC3986, par. 3.1 */
842 ptr++;
843 while (ptr < end &&
844 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
845 ptr++;
846 /* skip '://' */
847 if (ptr == end || *ptr++ != ':')
848 return NULL;
849 if (ptr == end || *ptr++ != '/')
850 return NULL;
851 if (ptr == end || *ptr++ != '/')
852 return NULL;
853 }
854 /* skip [user[:passwd]@]host[:[port]] */
855
856 while (ptr < end && *ptr != '/')
857 ptr++;
858
859 if (ptr == end)
860 return NULL;
861
862 /* OK, we got the '/' ! */
863 return ptr;
864}
865
William Lallemand65ad6e12014-01-31 15:08:02 +0100866/* Parse the URI from the given string and look for the "/" beginning the PATH.
867 * If not found, return NULL. It is returned otherwise.
868 */
869static char *
870http_get_path_from_string(char *str)
871{
872 char *ptr = str;
873
874 /* RFC2616, par. 5.1.2 :
875 * Request-URI = "*" | absuri | abspath | authority
876 */
877
878 if (*ptr == '*')
879 return NULL;
880
881 if (isalpha((unsigned char)*ptr)) {
882 /* this is a scheme as described by RFC3986, par. 3.1 */
883 ptr++;
884 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
885 ptr++;
886 /* skip '://' */
887 if (*ptr == '\0' || *ptr++ != ':')
888 return NULL;
889 if (*ptr == '\0' || *ptr++ != '/')
890 return NULL;
891 if (*ptr == '\0' || *ptr++ != '/')
892 return NULL;
893 }
894 /* skip [user[:passwd]@]host[:[port]] */
895
896 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
897 ptr++;
898
899 if (*ptr == '\0' || *ptr == ' ')
900 return NULL;
901
902 /* OK, we got the '/' ! */
903 return ptr;
904}
905
Willy Tarreau71241ab2012-12-27 11:30:54 +0100906/* Returns a 302 for a redirectable request that reaches a server working in
907 * in redirect mode. This may only be called just after the stream interface
908 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
909 * follow normal proxy processing. NOTE: this function is designed to support
910 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100911 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100912void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100913{
914 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100915 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100916 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200917 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918
919 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100920 trash.len = strlen(HTTP_302);
921 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100923 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100924
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100926 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 return;
928
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100929 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100930 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100931 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
932 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100933 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200935 /* 3: add the request URI. Since it was already forwarded, we need
936 * to temporarily rewind the buffer.
937 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100938 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200939 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200940
Willy Tarreauefb453c2008-10-26 20:49:47 +0100941 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200942 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 +0200943
Willy Tarreau9b28e032012-10-12 23:49:43 +0200944 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200945
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946 if (!path)
947 return;
948
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100949 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 return;
951
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100952 memcpy(trash.str + trash.len, path, len);
953 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100954
955 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100956 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
957 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100958 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100959 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
960 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100961 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962
963 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200964 si_shutr(si);
965 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->state = SI_ST_CLO;
968
969 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200970 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100971
972 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100973 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500974 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100975}
976
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100977/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 * that the server side is closed. Note that err_type is actually a
979 * bitmask, where almost only aborts may be cumulated with other
980 * values. We consider that aborted operations are more important
981 * than timeouts or errors due to the fact that nobody else in the
982 * logs might explain incomplete retries. All others should avoid
983 * being cumulated. It should normally not be possible to have multiple
984 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100985 * Note that connection errors appearing on the second request of a keep-alive
986 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100988void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100989{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100990 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100991
992 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100993 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200994 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100996 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
998 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001000 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001001 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001003 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001004 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001005 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001006 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001007 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1008 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001010 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001011 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001012 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001013 else if (err_type & SI_ET_CONN_RES)
1014 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001015 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1016 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001019 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001020}
1021
Willy Tarreau42250582007-04-01 01:30:43 +02001022extern const char sess_term_cond[8];
1023extern const char sess_fin_state[8];
1024extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001025struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001026struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001027struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001028
Willy Tarreau117f59e2007-03-04 18:17:17 +01001029/*
1030 * Capture headers from message starting at <som> according to header list
1031 * <cap_hdr>, and fill the <idx> structure appropriately.
1032 */
1033void capture_headers(char *som, struct hdr_idx *idx,
1034 char **cap, struct cap_hdr *cap_hdr)
1035{
1036 char *eol, *sol, *col, *sov;
1037 int cur_idx;
1038 struct cap_hdr *h;
1039 int len;
1040
1041 sol = som + hdr_idx_first_pos(idx);
1042 cur_idx = hdr_idx_first_idx(idx);
1043
1044 while (cur_idx) {
1045 eol = sol + idx->v[cur_idx].len;
1046
1047 col = sol;
1048 while (col < eol && *col != ':')
1049 col++;
1050
1051 sov = col + 1;
1052 while (sov < eol && http_is_lws[(unsigned char)*sov])
1053 sov++;
1054
1055 for (h = cap_hdr; h; h = h->next) {
1056 if ((h->namelen == col - sol) &&
1057 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1058 if (cap[h->index] == NULL)
1059 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001060 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001061
1062 if (cap[h->index] == NULL) {
1063 Alert("HTTP capture : out of memory.\n");
1064 continue;
1065 }
1066
1067 len = eol - sov;
1068 if (len > h->len)
1069 len = h->len;
1070
1071 memcpy(cap[h->index], sov, len);
1072 cap[h->index][len]=0;
1073 }
1074 }
1075 sol = eol + idx->v[cur_idx].cr + 1;
1076 cur_idx = idx->v[cur_idx].next;
1077 }
1078}
1079
1080
Willy Tarreau42250582007-04-01 01:30:43 +02001081/* either we find an LF at <ptr> or we jump to <bad>.
1082 */
1083#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1084
1085/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1086 * otherwise to <http_msg_ood> with <state> set to <st>.
1087 */
1088#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1089 ptr++; \
1090 if (likely(ptr < end)) \
1091 goto good; \
1092 else { \
1093 state = (st); \
1094 goto http_msg_ood; \
1095 } \
1096 } while (0)
1097
1098
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001100 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001101 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1102 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1103 * will give undefined results.
1104 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1105 * and that msg->sol points to the beginning of the response.
1106 * If a complete line is found (which implies that at least one CR or LF is
1107 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1108 * returned indicating an incomplete line (which does not mean that parts have
1109 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1110 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1111 * upon next call.
1112 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001113 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1115 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001116 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001117 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001118const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001119 enum ht_state state, const char *ptr, const char *end,
1120 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001121{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001122 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001123
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001126 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001127 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1129
1130 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001131 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1133 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001134 state = HTTP_MSG_ERROR;
1135 break;
1136
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001141 goto http_msg_rpcode;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr)))
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1153
1154 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001155 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001156 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1157 }
1158
1159 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 http_msg_rsp_reason:
1162 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 msg->sl.st.r_l = 0;
1165 goto http_msg_rpline_eol;
1166
Willy Tarreau8973c702007-01-21 23:58:29 +01001167 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001168 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 goto http_msg_rpreason;
1172 }
1173 if (likely(HTTP_IS_SPHT(*ptr)))
1174 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1175 /* so it's a CR/LF, so there is no reason phrase */
1176 goto http_msg_rsp_reason;
1177
Willy Tarreau8973c702007-01-21 23:58:29 +01001178 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001179 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 if (likely(!HTTP_IS_CRLF(*ptr)))
1181 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 http_msg_rpline_eol:
1184 /* We have seen the end of line. Note that we do not
1185 * necessarily have the \n yet, but at least we know that we
1186 * have EITHER \r OR \n, otherwise the response would not be
1187 * complete. We can then record the response length and return
1188 * to the caller which will be able to register it.
1189 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001190 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 return ptr;
1192
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001194#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1196 exit(1);
1197#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001198 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 }
1200
1201 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001202 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 if (ret_state)
1204 *ret_state = state;
1205 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001206 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208}
1209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001211 * This function parses a request line between <ptr> and <end>, starting with
1212 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1213 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1214 * will give undefined results.
1215 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1216 * and that msg->sol points to the beginning of the request.
1217 * If a complete line is found (which implies that at least one CR or LF is
1218 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1219 * returned indicating an incomplete line (which does not mean that parts have
1220 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1221 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1222 * upon next call.
1223 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001224 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1226 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001227 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001229const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001230 enum ht_state state, const char *ptr, const char *end,
1231 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001233 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001234
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001237 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 if (likely(HTTP_IS_TOKEN(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001242 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1244 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001246 if (likely(HTTP_IS_CRLF(*ptr))) {
1247 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001250 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001252 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 msg->sl.rq.v_l = 0;
1256 goto http_msg_rqline_eol;
1257 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001258 state = HTTP_MSG_ERROR;
1259 break;
1260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001262 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 goto http_msg_rquri;
1266 }
1267 if (likely(HTTP_IS_SPHT(*ptr)))
1268 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1269 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1270 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001273 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001274 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001278 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001282 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001283 /* non-ASCII chars are forbidden unless option
1284 * accept-invalid-http-request is enabled in the frontend.
1285 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001287 if (msg->err_pos < -1)
1288 goto invalid_char;
1289 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001290 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1292 }
1293
1294 if (likely(HTTP_IS_CRLF(*ptr))) {
1295 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1296 goto http_msg_req09_uri_e;
1297 }
1298
1299 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001300 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001301 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001302 state = HTTP_MSG_ERROR;
1303 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001308 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 goto http_msg_rqver;
1310 }
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1313 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1314 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001318 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001320
1321 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001322 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001323 http_msg_rqline_eol:
1324 /* We have seen the end of line. Note that we do not
1325 * necessarily have the \n yet, but at least we know that we
1326 * have EITHER \r OR \n, otherwise the request would not be
1327 * complete. We can then record the request length and return
1328 * to the caller which will be able to register it.
1329 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001330 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001331 return ptr;
1332 }
1333
1334 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001335 state = HTTP_MSG_ERROR;
1336 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001339#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1341 exit(1);
1342#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001343 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (ret_state)
1349 *ret_state = state;
1350 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001351 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001355/*
1356 * Returns the data from Authorization header. Function may be called more
1357 * than once so data is stored in txn->auth_data. When no header is found
1358 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001359 * searching again for something we are unable to find anyway. However, if
1360 * the result if valid, the cache is not reused because we would risk to
1361 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001362 */
1363
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001364/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1365 * set according to global.tune.bufsize.
1366 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001367char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001368
1369int
1370get_http_auth(struct session *s)
1371{
1372
1373 struct http_txn *txn = &s->txn;
1374 struct chunk auth_method;
1375 struct hdr_ctx ctx;
1376 char *h, *p;
1377 int len;
1378
1379#ifdef DEBUG_AUTH
1380 printf("Auth for session %p: %d\n", s, txn->auth.method);
1381#endif
1382
1383 if (txn->auth.method == HTTP_AUTH_WRONG)
1384 return 0;
1385
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001386 txn->auth.method = HTTP_AUTH_WRONG;
1387
1388 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001389
1390 if (txn->flags & TX_USE_PX_CONN) {
1391 h = "Proxy-Authorization";
1392 len = strlen(h);
1393 } else {
1394 h = "Authorization";
1395 len = strlen(h);
1396 }
1397
Willy Tarreau9b28e032012-10-12 23:49:43 +02001398 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001399 return 0;
1400
1401 h = ctx.line + ctx.val;
1402
1403 p = memchr(h, ' ', ctx.vlen);
1404 if (!p || p == h)
1405 return 0;
1406
1407 chunk_initlen(&auth_method, h, 0, p-h);
1408 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1409
1410 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1411
1412 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001413 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001414
1415 if (len < 0)
1416 return 0;
1417
1418
1419 get_http_auth_buff[len] = '\0';
1420
1421 p = strchr(get_http_auth_buff, ':');
1422
1423 if (!p)
1424 return 0;
1425
1426 txn->auth.user = get_http_auth_buff;
1427 *p = '\0';
1428 txn->auth.pass = p+1;
1429
1430 txn->auth.method = HTTP_AUTH_BASIC;
1431 return 1;
1432 }
1433
1434 return 0;
1435}
1436
Willy Tarreau58f10d72006-12-04 02:26:12 +01001437
Willy Tarreau8973c702007-01-21 23:58:29 +01001438/*
1439 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001440 * depending on the initial msg->msg_state. The caller is responsible for
1441 * ensuring that the message does not wrap. The function can be preempted
1442 * everywhere when data are missing and recalled at the exact same location
1443 * with no information loss. The message may even be realigned between two
1444 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001445 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001446 * fields. Note that msg->sol will be initialized after completing the first
1447 * state, so that none of the msg pointers has to be initialized prior to the
1448 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001450void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451{
Willy Tarreau3770f232013-12-07 00:01:53 +01001452 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001454 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001456 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001457 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001458 ptr = buf->p + msg->next;
1459 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (unlikely(ptr >= end))
1462 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 /*
1466 * First, states that are specific to the response only.
1467 * We check them first so that request and headers are
1468 * closer to each other (accessed more often).
1469 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001471 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001472 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001473 /* we have a start of message, but we have to check
1474 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001475 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001476 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001477 if (unlikely(ptr != buf->p)) {
1478 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001479 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001480 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001481 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 }
Willy Tarreau26927362012-05-18 23:22:52 +02001483 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001484 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 hdr_idx_init(idx);
1486 state = HTTP_MSG_RPVER;
1487 goto http_msg_rpver;
1488 }
1489
1490 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1491 goto http_msg_invalid;
1492
1493 if (unlikely(*ptr == '\n'))
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1496 /* stop here */
1497
Willy Tarreau8973c702007-01-21 23:58:29 +01001498 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001499 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001500 EXPECT_LF_HERE(ptr, http_msg_invalid);
1501 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1502 /* stop here */
1503
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001505 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 case HTTP_MSG_RPVER_SP:
1507 case HTTP_MSG_RPCODE:
1508 case HTTP_MSG_RPCODE_SP:
1509 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001510 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001511 state, ptr, end,
1512 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001513 if (unlikely(!ptr))
1514 return;
1515
1516 /* we have a full response and we know that we have either a CR
1517 * or an LF at <ptr>.
1518 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1520
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001521 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001522 if (likely(*ptr == '\r'))
1523 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1524 goto http_msg_rpline_end;
1525
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 /* msg->sol must point to the first of CR or LF. */
1529 EXPECT_LF_HERE(ptr, http_msg_invalid);
1530 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1531 /* stop here */
1532
1533 /*
1534 * Second, states that are specific to the request only
1535 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001537 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001539 /* we have a start of message, but we have to check
1540 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001541 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001542 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001543 if (likely(ptr != buf->p)) {
1544 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001545 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001546 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001547 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 }
Willy Tarreau26927362012-05-18 23:22:52 +02001549 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001550 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001551 state = HTTP_MSG_RQMETH;
1552 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001561 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_RQMETH_SP:
1572 case HTTP_MSG_RQURI:
1573 case HTTP_MSG_RQURI_SP:
1574 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 if (unlikely(!ptr))
1579 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 /* we have a full request and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 /* check for HTTP/0.9 request : no version information available.
1594 * msg->sol must point to the first of CR or LF.
1595 */
1596 if (unlikely(msg->sl.rq.v_l == 0))
1597 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 EXPECT_LF_HERE(ptr, http_msg_invalid);
1600 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001601 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 /*
1604 * Common states below
1605 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001607 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 if (likely(!HTTP_IS_CRLF(*ptr))) {
1610 goto http_msg_hdr_name;
1611 }
1612
1613 if (likely(*ptr == '\r'))
1614 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1615 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001618 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 /* assumes msg->sol points to the first char */
1620 if (likely(HTTP_IS_TOKEN(*ptr)))
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001623 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001626 if (likely(msg->err_pos < -1) || *ptr == '\n')
1627 goto http_msg_invalid;
1628
1629 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001631
1632 /* and we still accept this non-token character */
1633 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001636 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001637 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 if (likely(HTTP_IS_SPHT(*ptr)))
1639 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 if (likely(!HTTP_IS_CRLF(*ptr))) {
1645 goto http_msg_hdr_val;
1646 }
1647
1648 if (likely(*ptr == '\r'))
1649 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1650 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001653 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 EXPECT_LF_HERE(ptr, http_msg_invalid);
1655 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001658 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 if (likely(HTTP_IS_SPHT(*ptr))) {
1660 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001661 for (; buf->p + msg->sov < ptr; msg->sov++)
1662 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 goto http_msg_hdr_l1_sp;
1664 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001665 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001666 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 goto http_msg_complete_header;
1668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001671 /* assumes msg->sol points to the first char, and msg->sov
1672 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 */
1674 if (likely(!HTTP_IS_CRLF(*ptr)))
1675 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001676
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001677 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 /* Note: we could also copy eol into ->eoh so that we have the
1679 * real header end in case it ends with lots of LWS, but is this
1680 * really needed ?
1681 */
1682 if (likely(*ptr == '\r'))
1683 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1684 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001687 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 EXPECT_LF_HERE(ptr, http_msg_invalid);
1689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1694 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 for (; buf->p + msg->eol < ptr; msg->eol++)
1696 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 goto http_msg_hdr_val;
1698 }
1699 http_msg_complete_header:
1700 /*
1701 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 * Assumes msg->sol points to the first char, msg->sov points
1703 * to the first character of the value and msg->eol to the
1704 * first CR or LF so we know how the line ends. We insert last
1705 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 idx, idx->tail) < 0))
1709 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001710
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001711 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 if (likely(!HTTP_IS_CRLF(*ptr))) {
1713 goto http_msg_hdr_name;
1714 }
1715
1716 if (likely(*ptr == '\r'))
1717 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1718 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 /* Assumes msg->sol points to the first of either CR or LF */
1723 EXPECT_LF_HERE(ptr, http_msg_invalid);
1724 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001725 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001726 msg->eoh = msg->sol;
1727 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001730
1731 case HTTP_MSG_ERROR:
1732 /* this may only happen if we call http_msg_analyser() twice with an error */
1733 break;
1734
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001736#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1738 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001739#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001740 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 }
1742 http_msg_ood:
1743 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001744 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001745 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 http_msg_invalid:
1749 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001750 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 return;
1753}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001754
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001755/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1756 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1757 * nothing is done and 1 is returned.
1758 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001759static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760{
1761 int delta;
1762 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001763 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001764
1765 if (msg->sl.rq.v_l != 0)
1766 return 1;
1767
Willy Tarreau9b28e032012-10-12 23:49:43 +02001768 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769 delta = 0;
1770
1771 if (msg->sl.rq.u_l == 0) {
1772 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001773 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001774 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001775 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776 }
1777 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001778 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001779 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001780 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001781 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001782 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001784 NULL, NULL);
1785 if (unlikely(!cur_end))
1786 return 0;
1787
1788 /* we have a full HTTP/1.0 request now and we know that
1789 * we have either a CR or an LF at <ptr>.
1790 */
1791 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1792 return 1;
1793}
1794
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001796 * and "keep-alive" values. If we already know that some headers may safely
1797 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001798 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1799 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001800 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001801 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1802 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1803 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001804 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001805 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001806void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001807{
Willy Tarreau5b154472009-12-21 20:11:07 +01001808 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 const char *hdr_val = "Connection";
1810 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001811
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001812 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001813 return;
1814
Willy Tarreau88d349d2010-01-25 12:15:43 +01001815 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1816 hdr_val = "Proxy-Connection";
1817 hdr_len = 16;
1818 }
1819
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001822 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001823 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1824 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001825 if (to_del & 2)
1826 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001827 else
1828 txn->flags |= TX_CON_KAL_SET;
1829 }
1830 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1831 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 1)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_CLO_SET;
1836 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001837 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1838 txn->flags |= TX_HDR_CONN_UPG;
1839 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001840 }
1841
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001842 txn->flags |= TX_HDR_CONN_PRS;
1843 return;
1844}
Willy Tarreau5b154472009-12-21 20:11:07 +01001845
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001846/* Apply desired changes on the Connection: header. Values may be removed and/or
1847 * added depending on the <wanted> flags, which are exclusively composed of
1848 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1849 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1850 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001851void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001852{
1853 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001854 const char *hdr_val = "Connection";
1855 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001856
1857 ctx.idx = 0;
1858
Willy Tarreau88d349d2010-01-25 12:15:43 +01001859
1860 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1861 hdr_val = "Proxy-Connection";
1862 hdr_len = 16;
1863 }
1864
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001865 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001866 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001867 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1868 if (wanted & TX_CON_KAL_SET)
1869 txn->flags |= TX_CON_KAL_SET;
1870 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001871 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001872 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001873 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1874 if (wanted & TX_CON_CLO_SET)
1875 txn->flags |= TX_CON_CLO_SET;
1876 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001878 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880
1881 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1882 return;
1883
1884 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1885 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 hdr_val = "Connection: close";
1887 hdr_len = 17;
1888 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1889 hdr_val = "Proxy-Connection: close";
1890 hdr_len = 23;
1891 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001892 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001893 }
1894
1895 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1896 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001897 hdr_val = "Connection: keep-alive";
1898 hdr_len = 22;
1899 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1900 hdr_val = "Proxy-Connection: keep-alive";
1901 hdr_len = 28;
1902 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001904 }
1905 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001906}
1907
Willy Tarreaua458b672012-03-05 11:17:50 +01001908/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001910 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001911 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001914static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001915{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001916 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001918 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 const char *end = buf->data + buf->size;
1920 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 unsigned int chunk = 0;
1922
1923 /* The chunk size is in the following form, though we are only
1924 * interested in the size and CRLF :
1925 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1926 */
1927 while (1) {
1928 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001929 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001932 if (c < 0) /* not a hex digit anymore */
1933 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001934 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001935 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001936 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001937 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001938 chunk = (chunk << 4) + c;
1939 }
1940
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001942 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001943 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944
1945 while (http_is_spht[(unsigned char)*ptr]) {
1946 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001948 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001949 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001950 }
1951
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 /* Up to there, we know that at least one byte is present at *ptr. Check
1953 * for the end of chunk size.
1954 */
1955 while (1) {
1956 if (likely(HTTP_IS_CRLF(*ptr))) {
1957 /* we now have a CR or an LF at ptr */
1958 if (likely(*ptr == '\r')) {
1959 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001960 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001961 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 return 0;
1963 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001964
Willy Tarreaud98cf932009-12-27 22:54:55 +01001965 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001966 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001968 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 /* done */
1970 break;
1971 }
1972 else if (*ptr == ';') {
1973 /* chunk extension, ends at next CRLF */
1974 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001976 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001977 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978
1979 while (!HTTP_IS_CRLF(*ptr)) {
1980 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001981 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001982 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001983 return 0;
1984 }
1985 /* we have a CRLF now, loop above */
1986 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001987 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001989 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001990 }
1991
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001993 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001994 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001995 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001996 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001998 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001999 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002000 msg->chunk_len = chunk;
2001 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002004 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002005 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002006 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002007}
2008
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002009/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002010 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 * the trailers is found, it is automatically scheduled to be forwarded,
2012 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2013 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002014 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002015 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002016 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002017 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2018 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02002019 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01002020 * matches the length of trailers already parsed and not forwarded. It is also
2021 * important to note that this function is designed to be able to parse wrapped
2022 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002023 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002024static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002026 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002027
Willy Tarreaua458b672012-03-05 11:17:50 +01002028 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002030 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002031 const char *ptr = b_ptr(buf, msg->next);
2032 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002033 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034
2035 /* scan current line and stop at LF or CRLF */
2036 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002037 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 return 0;
2039
2040 if (*ptr == '\n') {
2041 if (!p1)
2042 p1 = ptr;
2043 p2 = ptr;
2044 break;
2045 }
2046
2047 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002048 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002049 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002050 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002051 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002052 p1 = ptr;
2053 }
2054
2055 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002056 if (ptr >= buf->data + buf->size)
2057 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 }
2059
2060 /* after LF; point to beginning of next line */
2061 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 if (p2 >= buf->data + buf->size)
2063 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002065 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002066 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002067 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002068
2069 /* schedule this line for forwarding */
2070 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 if (msg->sov >= buf->size)
2072 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 /* LF/CRLF at beginning of line => end of trailers at p2.
2076 * Everything was scheduled for forwarding, there's nothing
2077 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002078 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002079 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080 msg->msg_state = HTTP_MSG_DONE;
2081 return 1;
2082 }
2083 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002084 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 }
2086}
2087
Willy Tarreau54d23df2012-10-25 19:04:45 +02002088/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002090 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002091 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2093 * not enough data are available, the function does not change anything and
2094 * returns zero. If a parse error is encountered, the function returns < 0 and
2095 * does not change anything. Note: this function is designed to parse wrapped
2096 * CRLF at the end of the buffer.
2097 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002098static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002100 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002101 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102 int bytes;
2103
2104 /* NB: we'll check data availabilty at the end. It's not a
2105 * problem because whatever we match first will be checked
2106 * against the correct length.
2107 */
2108 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002109 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 if (*ptr == '\r') {
2111 bytes++;
2112 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002113 if (ptr >= buf->data + buf->size)
2114 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 }
2116
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 return 0;
2119
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124
2125 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002128 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2129 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002130 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2132 return 1;
2133}
Willy Tarreau5b154472009-12-21 20:11:07 +01002134
William Lallemand82fe75c2012-10-23 10:25:10 +02002135
2136/*
2137 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002138 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002139int select_compression_request_header(struct session *s, struct buffer *req)
2140{
2141 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002142 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002143 struct hdr_ctx ctx;
2144 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002145 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002146
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002147 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2148 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002149 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2150 */
2151 ctx.idx = 0;
2152 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2153 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002154 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2155 (ctx.vlen < 31 ||
2156 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2157 ctx.line[ctx.val + 30] < '6' ||
2158 (ctx.line[ctx.val + 30] == '6' &&
2159 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2160 s->comp_algo = NULL;
2161 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002162 }
2163
William Lallemand82fe75c2012-10-23 10:25:10 +02002164 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002165 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 +02002166 ctx.idx = 0;
2167 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002168 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002169 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2170 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002171
2172 /* remove all occurrences of the header when "compression offload" is set */
2173
2174 if ((s->be->comp && s->be->comp->offload) ||
2175 (s->fe->comp && s->fe->comp->offload)) {
2176 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2177 ctx.idx = 0;
2178 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2179 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2180 }
2181 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002182 return 1;
2183 }
2184 }
2185 }
2186 }
2187
2188 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002189 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2190 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 if (comp_algo->add_data == identity_add_data) {
2192 s->comp_algo = comp_algo;
2193 return 1;
2194 }
2195 }
2196 }
2197
2198 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002199 return 0;
2200}
2201
2202/*
2203 * Selects a comression algorithm depending of the server response.
2204 */
2205int select_compression_response_header(struct session *s, struct buffer *res)
2206{
2207 struct http_txn *txn = &s->txn;
2208 struct http_msg *msg = &txn->rsp;
2209 struct hdr_ctx ctx;
2210 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002211
2212 /* no common compression algorithm was found in request header */
2213 if (s->comp_algo == NULL)
2214 goto fail;
2215
2216 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002217 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002218 goto fail;
2219
William Lallemandd3002612012-11-26 14:34:47 +01002220 /* 200 only */
2221 if (txn->status != 200)
2222 goto fail;
2223
William Lallemand82fe75c2012-10-23 10:25:10 +02002224 /* Content-Length is null */
2225 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2226 goto fail;
2227
Willy Tarreau667c2a32013-04-09 08:13:58 +02002228 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2229 if (msg->flags & HTTP_MSGF_TE_CHNK)
2230 goto fail;
2231
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002233 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2235 goto fail;
2236
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002237 /* no compression when Cache-Control: no-transform is present in the message */
2238 ctx.idx = 0;
2239 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2240 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2241 goto fail;
2242 }
2243
William Lallemand82fe75c2012-10-23 10:25:10 +02002244 comp_type = NULL;
2245
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002246 /* we don't want to compress multipart content-types, nor content-types that are
2247 * not listed in the "compression type" directive if any. If no content-type was
2248 * found but configuration requires one, we don't compress either. Backend has
2249 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002250 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002251 ctx.idx = 0;
2252 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2253 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2254 goto fail;
2255
2256 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2257 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002258 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002259 if (ctx.vlen >= comp_type->name_len &&
2260 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002261 /* this Content-Type should be compressed */
2262 break;
2263 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002264 /* this Content-Type should not be compressed */
2265 if (comp_type == NULL)
2266 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002269 else { /* no content-type header */
2270 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2271 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002272 }
2273
William Lallemandd85f9172012-11-09 17:05:39 +01002274 /* limit compression rate */
2275 if (global.comp_rate_lim > 0)
2276 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2277 goto fail;
2278
William Lallemand072a2bf2012-11-20 17:01:01 +01002279 /* limit cpu usage */
2280 if (idle_pct < compress_min_idle)
2281 goto fail;
2282
William Lallemand4c49fae2012-11-07 15:00:23 +01002283 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002284 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002285 goto fail;
2286
William Lallemandec3e3892012-11-12 17:02:18 +01002287 s->flags |= SN_COMP_READY;
2288
William Lallemand82fe75c2012-10-23 10:25:10 +02002289 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002290 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002291 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2292 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2293
2294 /* add Transfer-Encoding header */
2295 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2296 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2297
2298 /*
2299 * Add Content-Encoding header when it's not identity encoding.
2300 * RFC 2616 : Identity encoding: This content-coding is used only in the
2301 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2302 * header.
2303 */
2304 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002305 trash.len = 18;
2306 memcpy(trash.str, "Content-Encoding: ", trash.len);
2307 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2308 trash.len += s->comp_algo->name_len;
2309 trash.str[trash.len] = '\0';
2310 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002312 return 1;
2313
2314fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002315 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 return 0;
2317}
2318
2319
Willy Tarreaud787e662009-07-07 10:14:51 +02002320/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2321 * processing can continue on next analysers, or zero if it either needs more
2322 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2323 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2324 * when it has nothing left to do, and may remove any analyser when it wants to
2325 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002326 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002327int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002328{
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 /*
2330 * We will parse the partial (or complete) lines.
2331 * We will check the request syntax, and also join multi-line
2332 * headers. An index of all the lines will be elaborated while
2333 * parsing.
2334 *
2335 * For the parsing, we use a 28 states FSM.
2336 *
2337 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002338 * req->buf->p = beginning of request
2339 * req->buf->p + msg->eoh = end of processed headers / start of current one
2340 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002341 * msg->eol = end of current header or line (LF or CRLF)
2342 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002343 *
2344 * At end of parsing, we may perform a capture of the error (if any), and
2345 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2346 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2347 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002351 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002354 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002355
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002356 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 +01002357 now_ms, __FUNCTION__,
2358 s,
2359 req,
2360 req->rex, req->wex,
2361 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002362 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002363 req->analysers);
2364
Willy Tarreau52a0c602009-08-16 22:45:38 +02002365 /* we're speaking HTTP here, so let's speak HTTP to the client */
2366 s->srv_error = http_return_srv_error;
2367
Willy Tarreau83e3af02009-12-28 17:39:57 +01002368 /* There's a protected area at the end of the buffer for rewriting
2369 * purposes. We don't want to start to parse the request if the
2370 * protected area is affected, because we may have to move processed
2371 * data later, which is much more complicated.
2372 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002373 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002374 if (txn->flags & TX_NOT_FIRST) {
2375 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002376 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002377 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002378 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002379 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002380 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002381 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002382 return 0;
2383 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002384 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2385 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2386 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002387 }
2388
Willy Tarreau065e8332010-01-08 00:30:20 +01002389 /* Note that we have the same problem with the response ; we
2390 * may want to send a redirect, error or anything which requires
2391 * some spare space. So we'll ensure that we have at least
2392 * maxrewrite bytes available in the response buffer before
2393 * processing that one. This will only affect pipelined
2394 * keep-alive requests.
2395 */
2396 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002397 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002398 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2399 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2400 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002401 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002402 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002403 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002404 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002405 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002406 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002407 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002408 return 0;
2409 }
2410 }
2411
Willy Tarreau9b28e032012-10-12 23:49:43 +02002412 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002413 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002414 }
2415
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 /* 1: we might have to print this header in debug mode */
2417 if (unlikely((global.mode & MODE_DEBUG) &&
2418 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002419 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002420 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002421
Willy Tarreau9b28e032012-10-12 23:49:43 +02002422 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002423 /* this is a bit complex : in case of error on the request line,
2424 * we know that rq.l is still zero, so we display only the part
2425 * up to the end of the line (truncated by debug_hdr).
2426 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002427 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 sol += hdr_idx_first_pos(&txn->hdr_idx);
2431 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002432
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 while (cur_idx) {
2434 eol = sol + txn->hdr_idx.v[cur_idx].len;
2435 debug_hdr("clihdr", s, sol, eol);
2436 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2437 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002438 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 }
2440
Willy Tarreau58f10d72006-12-04 02:26:12 +01002441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 /*
2443 * Now we quickly check if we have found a full valid request.
2444 * If not so, we check the FD and buffer states before leaving.
2445 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002446 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002447 * requests are checked first. When waiting for a second request
2448 * on a keep-alive session, if we encounter and error, close, t/o,
2449 * we note the error in the session flags but don't set any state.
2450 * Since the error will be noted there, it will not be counted by
2451 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002452 * Last, we may increase some tracked counters' http request errors on
2453 * the cases that are deliberately the client's fault. For instance,
2454 * a timeout or connection reset is not counted as an error. However
2455 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002457
Willy Tarreau655dce92009-11-08 13:10:58 +01002458 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002459 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002461 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002462 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002463 session_inc_http_req_ctr(s);
2464 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002465 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002467 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002468
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 /* 1: Since we are in header mode, if there's no space
2470 * left for headers, we won't be able to free more
2471 * later, so the session will never terminate. We
2472 * must terminate it now.
2473 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002474 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002475 /* FIXME: check if URI is set and return Status
2476 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002477 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002478 session_inc_http_req_ctr(s);
2479 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002480 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002481 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002482 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 goto return_bad_req;
2484 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485
Willy Tarreau59234e92008-11-30 23:51:27 +01002486 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002487 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002488 if (!(s->flags & SN_ERR_MASK))
2489 s->flags |= SN_ERR_CLICL;
2490
Willy Tarreaufcffa692010-01-10 14:21:19 +01002491 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002492 goto failed_keep_alive;
2493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002495 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002496 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002497 session_inc_http_err_ctr(s);
2498 }
2499
Willy Tarreaudc979f22012-12-04 10:39:01 +01002500 txn->status = 400;
2501 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 msg->msg_state = HTTP_MSG_ERROR;
2503 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504
Willy Tarreauda7ff642010-06-23 11:44:09 +02002505 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002506 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002507 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002508 if (s->listener->counters)
2509 s->listener->counters->failed_req++;
2510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 if (!(s->flags & SN_FINST_MASK))
2512 s->flags |= SN_FINST_R;
2513 return 0;
2514 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002517 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002518 if (!(s->flags & SN_ERR_MASK))
2519 s->flags |= SN_ERR_CLITO;
2520
Willy Tarreaufcffa692010-01-10 14:21:19 +01002521 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002522 goto failed_keep_alive;
2523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002525 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002526 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 session_inc_http_err_ctr(s);
2528 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002530 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 msg->msg_state = HTTP_MSG_ERROR;
2532 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533
Willy Tarreauda7ff642010-06-23 11:44:09 +02002534 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002535 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002536 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002537 if (s->listener->counters)
2538 s->listener->counters->failed_req++;
2539
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 if (!(s->flags & SN_FINST_MASK))
2541 s->flags |= SN_FINST_R;
2542 return 0;
2543 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002546 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002547 if (!(s->flags & SN_ERR_MASK))
2548 s->flags |= SN_ERR_CLICL;
2549
Willy Tarreaufcffa692010-01-10 14:21:19 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002551 goto failed_keep_alive;
2552
Willy Tarreau4076a152009-04-02 15:18:36 +02002553 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002554 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002556 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 msg->msg_state = HTTP_MSG_ERROR;
2558 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002559
Willy Tarreauda7ff642010-06-23 11:44:09 +02002560 session_inc_http_err_ctr(s);
2561 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002562 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002563 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002564 if (s->listener->counters)
2565 s->listener->counters->failed_req++;
2566
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 if (!(s->flags & SN_FINST_MASK))
2568 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002569 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570 }
2571
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002572 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002573 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2574 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002575#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002576 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002577 /* We need more data, we have to re-enable quick-ack in case we
2578 * previously disabled it, otherwise we might cause the client
2579 * to delay next data.
2580 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002581 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002582 }
2583#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002584
Willy Tarreaufcffa692010-01-10 14:21:19 +01002585 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2586 /* If the client starts to talk, let's fall back to
2587 * request timeout processing.
2588 */
2589 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002590 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002591 }
2592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002594 if (!tick_isset(req->analyse_exp)) {
2595 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2596 (txn->flags & TX_WAIT_NEXT_RQ) &&
2597 tick_isset(s->be->timeout.httpka))
2598 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2599 else
2600 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002602
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 /* we're not ready yet */
2604 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002605
2606 failed_keep_alive:
2607 /* Here we process low-level errors for keep-alive requests. In
2608 * short, if the request is not the first one and it experiences
2609 * a timeout, read error or shutdown, we just silently close so
2610 * that the client can try again.
2611 */
2612 txn->status = 0;
2613 msg->msg_state = HTTP_MSG_RQBEFORE;
2614 req->analysers = 0;
2615 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002616 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002618 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002619 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002621
Willy Tarreaud787e662009-07-07 10:14:51 +02002622 /* OK now we have a complete HTTP request with indexed headers. Let's
2623 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002624 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002625 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002626 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002627 * byte after the last LF. msg->sov points to the first byte of data.
2628 * msg->eol cannot be trusted because it may have been left uninitialized
2629 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002630 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002631
Willy Tarreauda7ff642010-06-23 11:44:09 +02002632 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002633 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2634
Willy Tarreaub16a5742010-01-10 14:46:16 +01002635 if (txn->flags & TX_WAIT_NEXT_RQ) {
2636 /* kill the pending keep-alive timeout */
2637 txn->flags &= ~TX_WAIT_NEXT_RQ;
2638 req->analyse_exp = TICK_ETERNITY;
2639 }
2640
2641
Willy Tarreaud787e662009-07-07 10:14:51 +02002642 /* Maybe we found in invalid header name while we were configured not
2643 * to block on that, so we have to capture it now.
2644 */
2645 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002646 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 /*
2649 * 1: identify the method
2650 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002651 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002652
2653 /* we can make use of server redirect on GET and HEAD */
2654 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2655 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /*
2658 * 2: check if the URI matches the monitor_uri.
2659 * We have to do this for every request which gets in, because
2660 * the monitor-uri is defined by the frontend.
2661 */
2662 if (unlikely((s->fe->monitor_uri_len != 0) &&
2663 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002664 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 s->fe->monitor_uri,
2666 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002667 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002669 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002673 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002676 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002677 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 ret = acl_pass(ret);
2680 if (cond->pol == ACL_COND_UNLESS)
2681 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 if (ret) {
2684 /* we fail this request, let's return 503 service unavail */
2685 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002686 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002687 if (!(s->flags & SN_ERR_MASK))
2688 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002690 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002691 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* nothing to fail, let's reply normaly */
2694 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002695 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002696 if (!(s->flags & SN_ERR_MASK))
2697 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 goto return_prx_cond;
2699 }
2700
2701 /*
2702 * 3: Maybe we have to copy the original REQURI for the logs ?
2703 * Note: we cannot log anymore if the request has been
2704 * classified as invalid.
2705 */
2706 if (unlikely(s->logs.logwait & LW_REQ)) {
2707 /* we have a complete HTTP request that we must log */
2708 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2709 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002710
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 if (urilen >= REQURI_LEN)
2712 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002713 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002714 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002715
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002716 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 s->do_log(s);
2718 } else {
2719 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002720 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 }
Willy Tarreau06619262006-12-17 08:37:22 +01002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002724 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002725 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002726
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 /* ... and check if the request is HTTP/1.1 or above */
2728 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2730 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2731 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002732 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002733
2734 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002735 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 +01002736
Willy Tarreau88d349d2010-01-25 12:15:43 +01002737 /* if the frontend has "option http-use-proxy-header", we'll check if
2738 * we have what looks like a proxied connection instead of a connection,
2739 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2740 * Note that this is *not* RFC-compliant, however browsers and proxies
2741 * happen to do that despite being non-standard :-(
2742 * We consider that a request not beginning with either '/' or '*' is
2743 * a proxied connection, which covers both "scheme://location" and
2744 * CONNECT ip:port.
2745 */
2746 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002747 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002748 txn->flags |= TX_USE_PX_CONN;
2749
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002750 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002751 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002752
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002754 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002755 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002757
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002758 /* 6: determine the transfer-length.
2759 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2760 * the presence of a message-body in a REQUEST and its transfer length
2761 * must be determined that way (in order of precedence) :
2762 * 1. The presence of a message-body in a request is signaled by the
2763 * inclusion of a Content-Length or Transfer-Encoding header field
2764 * in the request's header fields. When a request message contains
2765 * both a message-body of non-zero length and a method that does
2766 * not define any semantics for that request message-body, then an
2767 * origin server SHOULD either ignore the message-body or respond
2768 * with an appropriate error message (e.g., 413). A proxy or
2769 * gateway, when presented the same request, SHOULD either forward
2770 * the request inbound with the message- body or ignore the
2771 * message-body when determining a response.
2772 *
2773 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2774 * and the "chunked" transfer-coding (Section 6.2) is used, the
2775 * transfer-length is defined by the use of this transfer-coding.
2776 * If a Transfer-Encoding header field is present and the "chunked"
2777 * transfer-coding is not present, the transfer-length is defined
2778 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002779 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002780 * 3. If a Content-Length header field is present, its decimal value in
2781 * OCTETs represents both the entity-length and the transfer-length.
2782 * If a message is received with both a Transfer-Encoding header
2783 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002784 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002785 * 4. By the server closing the connection. (Closing the connection
2786 * cannot be used to indicate the end of a request body, since that
2787 * would leave no possibility for the server to send back a response.)
2788 *
2789 * Whenever a transfer-coding is applied to a message-body, the set of
2790 * transfer-codings MUST include "chunked", unless the message indicates
2791 * it is terminated by closing the connection. When the "chunked"
2792 * transfer-coding is used, it MUST be the last transfer-coding applied
2793 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002794 */
2795
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002796 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002797 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002798 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002799 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002800 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002801 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002802 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2803 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002804 /* bad transfer-encoding (chunked followed by something else) */
2805 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002806 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002807 break;
2808 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002809 }
2810
Willy Tarreau32b47f42009-10-18 20:55:02 +02002811 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002812 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002813 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002814 signed long long cl;
2815
Willy Tarreauad14f752011-09-02 20:33:27 +02002816 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002817 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002818 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002819 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002820
Willy Tarreauad14f752011-09-02 20:33:27 +02002821 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002822 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002823 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002824 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002825
Willy Tarreauad14f752011-09-02 20:33:27 +02002826 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002827 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002828 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002829 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002830
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002831 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002832 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002833 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002834 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002835
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002836 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002837 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002838 }
2839
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002840 /* bodyless requests have a known length */
2841 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002842 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002843
Willy Tarreaud787e662009-07-07 10:14:51 +02002844 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002845 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002846 req->analyse_exp = TICK_ETERNITY;
2847 return 1;
2848
2849 return_bad_req:
2850 /* We centralize bad requests processing here */
2851 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2852 /* we detected a parsing error. We want to archive this request
2853 * in the dedicated proxy area for later troubleshooting.
2854 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002855 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002856 }
2857
2858 txn->req.msg_state = HTTP_MSG_ERROR;
2859 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002860 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002861
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002862 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002863 if (s->listener->counters)
2864 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002865
2866 return_prx_cond:
2867 if (!(s->flags & SN_ERR_MASK))
2868 s->flags |= SN_ERR_PRXCOND;
2869 if (!(s->flags & SN_FINST_MASK))
2870 s->flags |= SN_FINST_R;
2871
2872 req->analysers = 0;
2873 req->analyse_exp = TICK_ETERNITY;
2874 return 0;
2875}
2876
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002877
Willy Tarreau347a35d2013-11-22 17:51:09 +01002878/* This function prepares an applet to handle the stats. It can deal with the
2879 * "100-continue" expectation, check that admin rules are met for POST requests,
2880 * and program a response message if something was unexpected. It cannot fail
2881 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002882 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 * s->target which is supposed to already point to the stats applet. The caller
2884 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002885 */
2886int http_handle_stats(struct session *s, struct channel *req)
2887{
2888 struct stats_admin_rule *stats_admin_rule;
2889 struct stream_interface *si = s->rep->prod;
2890 struct http_txn *txn = &s->txn;
2891 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002892 struct uri_auth *uri_auth = s->be->uri_auth;
2893 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002894 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002895
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002896 appctx = si_appctx(si);
2897 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2898 appctx->st1 = appctx->st2 = 0;
2899 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2900 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002901
2902 uri = msg->chn->buf->p + msg->sl.rq.u;
2903 lookup = uri + uri_auth->uri_len;
2904
2905 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2906 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002907 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002908 break;
2909 }
2910 }
2911
2912 if (uri_auth->refresh) {
2913 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2914 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002915 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002916 break;
2917 }
2918 }
2919 }
2920
2921 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2922 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002923 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002924 break;
2925 }
2926 }
2927
2928 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2929 if (memcmp(h, ";st=", 4) == 0) {
2930 int i;
2931 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002932 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002933 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2934 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002936 break;
2937 }
2938 }
2939 break;
2940 }
2941 }
2942
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002943 appctx->ctx.stats.scope_str = 0;
2944 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002945 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2946 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2947 int itx = 0;
2948 const char *h2;
2949 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2950 const char *err;
2951
2952 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2953 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002954 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002955 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2956 itx++;
2957 h++;
2958 }
2959
2960 if (itx > STAT_SCOPE_TXT_MAXLEN)
2961 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002962 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002963
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002964 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002965 memcpy(scope_txt, h2, itx);
2966 scope_txt[itx] = '\0';
2967 err = invalid_char(scope_txt);
2968 if (err) {
2969 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002970 appctx->ctx.stats.scope_str = 0;
2971 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002972 }
2973 break;
2974 }
2975 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002976
2977 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002978 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002979 int ret = 1;
2980
2981 if (stats_admin_rule->cond) {
2982 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2983 ret = acl_pass(ret);
2984 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2985 ret = !ret;
2986 }
2987
2988 if (ret) {
2989 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002990 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002991 break;
2992 }
2993 }
2994
2995 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002996 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002997 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002998 if (msg->msg_state < HTTP_MSG_100_SENT) {
2999 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3000 * send an HTTP/1.1 100 Continue intermediate response.
3001 */
3002 if (msg->flags & HTTP_MSGF_VER_11) {
3003 struct hdr_ctx ctx;
3004 ctx.idx = 0;
3005 /* Expect is allowed in 1.1, look for it */
3006 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3007 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3008 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3009 }
3010 }
3011 msg->msg_state = HTTP_MSG_100_SENT;
3012 s->logs.tv_request = now; /* update the request timer to reflect full request */
3013 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003014 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003015 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003016 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003017 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3018 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003019 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003020 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003021 else {
3022 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003023 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003024 }
3025
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003026 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003027 return 1;
3028}
3029
Lukas Tribus67db8df2013-06-23 17:37:13 +02003030/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3031 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3032 */
3033static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3034{
3035#ifdef IP_TOS
3036 if (from.ss_family == AF_INET)
3037 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3038#endif
3039#ifdef IPV6_TCLASS
3040 if (from.ss_family == AF_INET6) {
3041 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3042 /* v4-mapped addresses need IP_TOS */
3043 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3044 else
3045 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3046 }
3047#endif
3048}
3049
Willy Tarreau20b0de52012-12-24 15:45:22 +01003050/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003051 * transaction <txn>. Returns the first rule that prevents further processing
3052 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3053 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3054 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003055 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003056static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003057http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003058{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003059 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003060 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003061 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003062
Willy Tarreauff011f22011-01-06 17:51:27 +01003063 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003064 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003065 continue;
3066
Willy Tarreau96257ec2012-12-27 10:46:37 +01003067 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003068 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003069 int ret;
3070
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003071 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 ret = acl_pass(ret);
3073
Willy Tarreauff011f22011-01-06 17:51:27 +01003074 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003075 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003076
3077 if (!ret) /* condition not matched */
3078 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003079 }
3080
Willy Tarreau20b0de52012-12-24 15:45:22 +01003081
Willy Tarreau96257ec2012-12-27 10:46:37 +01003082 switch (rule->action) {
3083 case HTTP_REQ_ACT_ALLOW:
3084 return NULL; /* "allow" rules are OK */
3085
3086 case HTTP_REQ_ACT_DENY:
3087 txn->flags |= TX_CLDENY;
3088 return rule;
3089
Willy Tarreauccbcc372012-12-27 12:37:57 +01003090 case HTTP_REQ_ACT_TARPIT:
3091 txn->flags |= TX_CLTARPIT;
3092 return rule;
3093
Willy Tarreau96257ec2012-12-27 10:46:37 +01003094 case HTTP_REQ_ACT_AUTH:
3095 return rule;
3096
Willy Tarreau81499eb2012-12-27 12:19:02 +01003097 case HTTP_REQ_ACT_REDIR:
3098 return rule;
3099
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003100 case HTTP_REQ_ACT_SET_NICE:
3101 s->task->nice = rule->arg.nice;
3102 break;
3103
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003104 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003105 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003106 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003107 break;
3108
Willy Tarreau51347ed2013-06-11 19:34:13 +02003109 case HTTP_REQ_ACT_SET_MARK:
3110#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003111 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003112 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003113#endif
3114 break;
3115
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003116 case HTTP_REQ_ACT_SET_LOGL:
3117 s->logs.level = rule->arg.loglevel;
3118 break;
3119
Willy Tarreau96257ec2012-12-27 10:46:37 +01003120 case HTTP_REQ_ACT_SET_HDR:
3121 ctx.idx = 0;
3122 /* remove all occurrences of the header */
3123 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3124 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3125 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003126 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003127 /* now fall through to header addition */
3128
3129 case HTTP_REQ_ACT_ADD_HDR:
3130 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3131 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3132 trash.len = rule->arg.hdr_add.name_len;
3133 trash.str[trash.len++] = ':';
3134 trash.str[trash.len++] = ' ';
3135 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3136 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3137 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003138 }
3139 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003140
3141 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003142 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003143}
3144
Willy Tarreau71241ab2012-12-27 11:30:54 +01003145
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003146/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3147 * transaction <txn>. Returns the first rule that prevents further processing
3148 * of the response (deny, ...) or NULL if it executed all rules or stopped
3149 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3150 * rule.
3151 */
3152static struct http_res_rule *
3153http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3154{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003155 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003156 struct http_res_rule *rule;
3157 struct hdr_ctx ctx;
3158
3159 list_for_each_entry(rule, rules, list) {
3160 if (rule->action >= HTTP_RES_ACT_MAX)
3161 continue;
3162
3163 /* check optional condition */
3164 if (rule->cond) {
3165 int ret;
3166
3167 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3168 ret = acl_pass(ret);
3169
3170 if (rule->cond->pol == ACL_COND_UNLESS)
3171 ret = !ret;
3172
3173 if (!ret) /* condition not matched */
3174 continue;
3175 }
3176
3177
3178 switch (rule->action) {
3179 case HTTP_RES_ACT_ALLOW:
3180 return NULL; /* "allow" rules are OK */
3181
3182 case HTTP_RES_ACT_DENY:
3183 txn->flags |= TX_SVDENY;
3184 return rule;
3185
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003186 case HTTP_RES_ACT_SET_NICE:
3187 s->task->nice = rule->arg.nice;
3188 break;
3189
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003190 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003191 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003192 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003193 break;
3194
Willy Tarreau51347ed2013-06-11 19:34:13 +02003195 case HTTP_RES_ACT_SET_MARK:
3196#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003197 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003198 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003199#endif
3200 break;
3201
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003202 case HTTP_RES_ACT_SET_LOGL:
3203 s->logs.level = rule->arg.loglevel;
3204 break;
3205
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003206 case HTTP_RES_ACT_SET_HDR:
3207 ctx.idx = 0;
3208 /* remove all occurrences of the header */
3209 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3210 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3211 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3212 }
3213 /* now fall through to header addition */
3214
3215 case HTTP_RES_ACT_ADD_HDR:
3216 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3217 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3218 trash.len = rule->arg.hdr_add.name_len;
3219 trash.str[trash.len++] = ':';
3220 trash.str[trash.len++] = ' ';
3221 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3222 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3223 break;
3224 }
3225 }
3226
3227 /* we reached the end of the rules, nothing to report */
3228 return NULL;
3229}
3230
3231
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232/* Perform an HTTP redirect based on the information in <rule>. The function
3233 * returns non-zero on success, or zero in case of a, irrecoverable error such
3234 * as too large a request to build a valid response.
3235 */
3236static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3237{
3238 struct http_msg *msg = &txn->req;
3239 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003240 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003241
3242 /* build redirect message */
3243 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003244 case 308:
3245 msg_fmt = HTTP_308;
3246 break;
3247 case 307:
3248 msg_fmt = HTTP_307;
3249 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003250 case 303:
3251 msg_fmt = HTTP_303;
3252 break;
3253 case 301:
3254 msg_fmt = HTTP_301;
3255 break;
3256 case 302:
3257 default:
3258 msg_fmt = HTTP_302;
3259 break;
3260 }
3261
3262 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3263 return 0;
3264
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003265 location = trash.str + trash.len;
3266
Willy Tarreau71241ab2012-12-27 11:30:54 +01003267 switch(rule->type) {
3268 case REDIRECT_TYPE_SCHEME: {
3269 const char *path;
3270 const char *host;
3271 struct hdr_ctx ctx;
3272 int pathlen;
3273 int hostlen;
3274
3275 host = "";
3276 hostlen = 0;
3277 ctx.idx = 0;
3278 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3279 host = ctx.line + ctx.val;
3280 hostlen = ctx.vlen;
3281 }
3282
3283 path = http_get_path(txn);
3284 /* build message using path */
3285 if (path) {
3286 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3287 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3288 int qs = 0;
3289 while (qs < pathlen) {
3290 if (path[qs] == '?') {
3291 pathlen = qs;
3292 break;
3293 }
3294 qs++;
3295 }
3296 }
3297 } else {
3298 path = "/";
3299 pathlen = 1;
3300 }
3301
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003302 if (rule->rdr_str) { /* this is an old "redirect" rule */
3303 /* check if we can add scheme + "://" + host + path */
3304 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3305 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003307 /* add scheme */
3308 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3309 trash.len += rule->rdr_len;
3310 }
3311 else {
3312 /* add scheme with executing log format */
3313 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003314
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003315 /* check if we can add scheme + "://" + host + path */
3316 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3317 return 0;
3318 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003319 /* add "://" */
3320 memcpy(trash.str + trash.len, "://", 3);
3321 trash.len += 3;
3322
3323 /* add host */
3324 memcpy(trash.str + trash.len, host, hostlen);
3325 trash.len += hostlen;
3326
3327 /* add path */
3328 memcpy(trash.str + trash.len, path, pathlen);
3329 trash.len += pathlen;
3330
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003331 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003332 if (trash.len && trash.str[trash.len - 1] != '/' &&
3333 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3334 if (trash.len > trash.size - 5)
3335 return 0;
3336 trash.str[trash.len] = '/';
3337 trash.len++;
3338 }
3339
3340 break;
3341 }
3342 case REDIRECT_TYPE_PREFIX: {
3343 const char *path;
3344 int pathlen;
3345
3346 path = http_get_path(txn);
3347 /* build message using path */
3348 if (path) {
3349 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3350 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3351 int qs = 0;
3352 while (qs < pathlen) {
3353 if (path[qs] == '?') {
3354 pathlen = qs;
3355 break;
3356 }
3357 qs++;
3358 }
3359 }
3360 } else {
3361 path = "/";
3362 pathlen = 1;
3363 }
3364
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003365 if (rule->rdr_str) { /* this is an old "redirect" rule */
3366 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3367 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003368
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003369 /* add prefix. Note that if prefix == "/", we don't want to
3370 * add anything, otherwise it makes it hard for the user to
3371 * configure a self-redirection.
3372 */
3373 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3374 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3375 trash.len += rule->rdr_len;
3376 }
3377 }
3378 else {
3379 /* add prefix with executing log format */
3380 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3381
3382 /* Check length */
3383 if (trash.len + pathlen > trash.size - 4)
3384 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003385 }
3386
3387 /* add path */
3388 memcpy(trash.str + trash.len, path, pathlen);
3389 trash.len += pathlen;
3390
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003391 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003392 if (trash.len && trash.str[trash.len - 1] != '/' &&
3393 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3394 if (trash.len > trash.size - 5)
3395 return 0;
3396 trash.str[trash.len] = '/';
3397 trash.len++;
3398 }
3399
3400 break;
3401 }
3402 case REDIRECT_TYPE_LOCATION:
3403 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003404 if (rule->rdr_str) { /* this is an old "redirect" rule */
3405 if (trash.len + rule->rdr_len > trash.size - 4)
3406 return 0;
3407
3408 /* add location */
3409 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3410 trash.len += rule->rdr_len;
3411 }
3412 else {
3413 /* add location with executing log format */
3414 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003415
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003416 /* Check left length */
3417 if (trash.len > trash.size - 4)
3418 return 0;
3419 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003420 break;
3421 }
3422
3423 if (rule->cookie_len) {
3424 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3425 trash.len += 14;
3426 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3427 trash.len += rule->cookie_len;
3428 memcpy(trash.str + trash.len, "\r\n", 2);
3429 trash.len += 2;
3430 }
3431
3432 /* add end of headers and the keep-alive/close status.
3433 * We may choose to set keep-alive if the Location begins
3434 * with a slash, because the client will come back to the
3435 * same server.
3436 */
3437 txn->status = rule->code;
3438 /* let's log the request time */
3439 s->logs.tv_request = now;
3440
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003441 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003442 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3443 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3444 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3445 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3446 /* keep-alive possible */
3447 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3448 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3449 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3450 trash.len += 30;
3451 } else {
3452 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3453 trash.len += 24;
3454 }
3455 }
3456 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3457 trash.len += 4;
3458 bo_inject(txn->rsp.chn, trash.str, trash.len);
3459 /* "eat" the request */
3460 bi_fast_delete(txn->req.chn->buf, msg->sov);
3461 msg->sov = 0;
3462 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3463 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3464 txn->req.msg_state = HTTP_MSG_CLOSED;
3465 txn->rsp.msg_state = HTTP_MSG_DONE;
3466 } else {
3467 /* keep-alive not possible */
3468 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3469 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3470 trash.len += 29;
3471 } else {
3472 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3473 trash.len += 23;
3474 }
3475 stream_int_retnclose(txn->req.chn->prod, &trash);
3476 txn->req.chn->analysers = 0;
3477 }
3478
3479 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003480 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003481 if (!(s->flags & SN_FINST_MASK))
3482 s->flags |= SN_FINST_R;
3483
3484 return 1;
3485}
3486
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003487/* This stream analyser runs all HTTP request processing which is common to
3488 * frontends and backends, which means blocking ACLs, filters, connection-close,
3489 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003490 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003491 * either needs more data or wants to immediately abort the request (eg: deny,
3492 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003493 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003494int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003495{
Willy Tarreaud787e662009-07-07 10:14:51 +02003496 struct http_txn *txn = &s->txn;
3497 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003499 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003500 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003501 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003502
Willy Tarreau655dce92009-11-08 13:10:58 +01003503 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003504 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003505 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003506 return 0;
3507 }
3508
Willy Tarreau3a816292009-07-07 10:55:49 +02003509 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003510 req->analyse_exp = TICK_ETERNITY;
3511
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003512 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 +02003513 now_ms, __FUNCTION__,
3514 s,
3515 req,
3516 req->rex, req->wex,
3517 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003518 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003519 req->analysers);
3520
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003521 /* first check whether we have some ACLs set to block this request */
3522 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003523 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003524
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003525 ret = acl_pass(ret);
3526 if (cond->pol == ACL_COND_UNLESS)
3527 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003528
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003529 if (ret) {
3530 txn->status = 403;
3531 /* let's log the request time */
3532 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003533 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003534 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003535 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003536 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003537 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003538
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003539 /* just in case we have some per-backend tracking */
3540 session_inc_be_http_req_ctr(s);
3541
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003542 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003543 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003544
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003545 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003546 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003547 if (stats_check_uri(s->rep->prod, txn, px)) {
3548 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003549 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3550 txn->status = 500;
3551 s->logs.tv_request = now;
3552 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003553
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003554 if (!(s->flags & SN_ERR_MASK))
3555 s->flags |= SN_ERR_RESOURCE;
3556 goto return_prx_cond;
3557 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003558 /* parse the whole stats request and extract the relevant information */
3559 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003560 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 +01003561 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003562 }
3563
Willy Tarreau3b44e722013-11-16 10:28:23 +01003564 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3565 * blocked by an http-request rule.
3566 */
3567 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003568 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003569 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003570 }
Willy Tarreau06619262006-12-17 08:37:22 +01003571
Willy Tarreau3b44e722013-11-16 10:28:23 +01003572 /* return a 403 if either rule has blocked */
3573 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003575 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003576 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003577 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003578 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003579 s->fe->fe_counters.denied_req++;
3580 if (s->fe != s->be)
3581 s->be->be_counters.denied_req++;
3582 if (s->listener->counters)
3583 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003584 goto return_prx_cond;
3585 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003586
3587 /* When a connection is tarpitted, we use the tarpit timeout,
3588 * which may be the same as the connect timeout if unspecified.
3589 * If unset, then set it to zero because we really want it to
3590 * eventually expire. We build the tarpit as an analyser.
3591 */
3592 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003593 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003594 /* wipe the request out so that we can drop the connection early
3595 * if the client closes first.
3596 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003597 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003598 req->analysers = 0; /* remove switching rules etc... */
3599 req->analysers |= AN_REQ_HTTP_TARPIT;
3600 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3601 if (!req->analyse_exp)
3602 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003603 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003604 s->fe->fe_counters.denied_req++;
3605 if (s->fe != s->be)
3606 s->be->be_counters.denied_req++;
3607 if (s->listener->counters)
3608 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003609 return 1;
3610 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003611 }
Willy Tarreau06619262006-12-17 08:37:22 +01003612
Willy Tarreau70dffda2014-01-30 03:07:23 +01003613 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003614 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003615 * Option httpclose by itself sets tunnel mode where headers are mangled.
3616 * However, if another mode is set, it will affect it (eg: server-close/
3617 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3618 * if FE and BE have the same settings (common). The method consists in
3619 * checking if options changed between the two calls (implying that either
3620 * one is non-null, or one of them is non-null and we are there for the first
3621 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003622 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003623
Willy Tarreau416ce612014-01-31 15:45:34 +01003624 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003625 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003626 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003627
Willy Tarreau70dffda2014-01-30 03:07:23 +01003628 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3629 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3630 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3631 tmp = TX_CON_WANT_TUN;
3632
3633 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3634 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3635 tmp = TX_CON_WANT_TUN;
3636 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003637
3638 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003639 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3640 /* option httpclose + server_close => forceclose */
3641 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3642 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3643 tmp = TX_CON_WANT_CLO;
3644 else
3645 tmp = TX_CON_WANT_SCL;
3646 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003647
3648 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3649 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003650 tmp = TX_CON_WANT_CLO;
3651
Willy Tarreau5b154472009-12-21 20:11:07 +01003652 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3653 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003654
Willy Tarreau416ce612014-01-31 15:45:34 +01003655 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3656 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003657 /* parse the Connection header and possibly clean it */
3658 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003659 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003660 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3661 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003662 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003663 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003664 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003665 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003666 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003667
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003668 /* check if client or config asks for explicit close in KAL/SCL */
3669 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3670 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3671 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003672 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003673 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003674 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003675 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3676 }
Willy Tarreau78599912009-10-17 20:12:21 +02003677
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003678 /* we can be blocked here because the request needs to be authenticated,
3679 * either to pass or to access stats.
3680 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003681 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003682 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003683
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003684 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003685 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003686
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003687 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003688 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003689 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003690 /* on 401 we still count one error, because normal browsing
3691 * won't significantly increase the counter but brute force
3692 * attempts will.
3693 */
3694 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003695 goto return_prx_cond;
3696 }
3697
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003698 /* add request headers from the rule sets in the same order */
3699 list_for_each_entry(wl, &px->req_add, list) {
3700 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003701 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003702 ret = acl_pass(ret);
3703 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3704 ret = !ret;
3705 if (!ret)
3706 continue;
3707 }
3708
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003709 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003710 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003711 }
3712
3713 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3714 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3715 goto return_bad_req;
3716 req->analyse_exp = TICK_ETERNITY;
3717 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003718 }
3719
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003720 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003721 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003722 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3723 s->fe->fe_counters.intercepted_req++;
3724
3725 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3726 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3727 if (!(s->flags & SN_FINST_MASK))
3728 s->flags |= SN_FINST_R;
3729
3730 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003731 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003732 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003733 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003735 /* check whether we have some ACLs set to redirect this request */
3736 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003737 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003738 int ret;
3739
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003740 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003741 ret = acl_pass(ret);
3742 if (rule->cond->pol == ACL_COND_UNLESS)
3743 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003744 if (!ret)
3745 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003746 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003747 if (!http_apply_redirect_rule(rule, s, txn))
3748 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003749
Willy Tarreau71241ab2012-12-27 11:30:54 +01003750 req->analyse_exp = TICK_ETERNITY;
3751 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003752 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003753
Willy Tarreau2be39392010-01-03 17:24:51 +01003754 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3755 * If this happens, then the data will not come immediately, so we must
3756 * send all what we have without waiting. Note that due to the small gain
3757 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003758 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003759 * itself once used.
3760 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003761 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003762
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003763 /* that's OK for us now, let's move on to next analysers */
3764 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003765
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003766 return_bad_req:
3767 /* We centralize bad requests processing here */
3768 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3769 /* we detected a parsing error. We want to archive this request
3770 * in the dedicated proxy area for later troubleshooting.
3771 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003772 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003773 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003774
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003775 txn->req.msg_state = HTTP_MSG_ERROR;
3776 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003777 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003778
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003779 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003780 if (s->listener->counters)
3781 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003783 return_prx_cond:
3784 if (!(s->flags & SN_ERR_MASK))
3785 s->flags |= SN_ERR_PRXCOND;
3786 if (!(s->flags & SN_FINST_MASK))
3787 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003788
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003789 req->analysers = 0;
3790 req->analyse_exp = TICK_ETERNITY;
3791 return 0;
3792}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003793
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003794/* This function performs all the processing enabled for the current request.
3795 * It returns 1 if the processing can continue on next analysers, or zero if it
3796 * needs more data, encounters an error, or wants to immediately abort the
3797 * request. It relies on buffers flags, and updates s->req->analysers.
3798 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003799int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003800{
3801 struct http_txn *txn = &s->txn;
3802 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003803 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003804
Willy Tarreau655dce92009-11-08 13:10:58 +01003805 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003806 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003807 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003808 return 0;
3809 }
3810
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003811 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 +02003812 now_ms, __FUNCTION__,
3813 s,
3814 req,
3815 req->rex, req->wex,
3816 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003817 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003818 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003819
William Lallemand82fe75c2012-10-23 10:25:10 +02003820 if (s->fe->comp || s->be->comp)
3821 select_compression_request_header(s, req->buf);
3822
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 /*
3824 * Right now, we know that we have processed the entire headers
3825 * and that unwanted requests have been filtered out. We can do
3826 * whatever we want with the remaining request. Also, now we
3827 * may have separate values for ->fe, ->be.
3828 */
Willy Tarreau06619262006-12-17 08:37:22 +01003829
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003831 * If HTTP PROXY is set we simply get remote server address parsing
3832 * incoming request. Note that this requires that a connection is
3833 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003834 */
3835 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003836 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003837 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003838
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003839 /* Note that for now we don't reuse existing proxy connections */
3840 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003841 txn->req.msg_state = HTTP_MSG_ERROR;
3842 txn->status = 500;
3843 req->analysers = 0;
3844 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3845
3846 if (!(s->flags & SN_ERR_MASK))
3847 s->flags |= SN_ERR_RESOURCE;
3848 if (!(s->flags & SN_FINST_MASK))
3849 s->flags |= SN_FINST_R;
3850
3851 return 0;
3852 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003853
3854 path = http_get_path(txn);
3855 url2sa(req->buf->p + msg->sl.rq.u,
3856 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3857 &conn->addr.to);
3858 /* if the path was found, we have to remove everything between
3859 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3860 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3861 * u_l characters by a single "/".
3862 */
3863 if (path) {
3864 char *cur_ptr = req->buf->p;
3865 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3866 int delta;
3867
3868 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3869 http_msg_move_end(&txn->req, delta);
3870 cur_end += delta;
3871 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3872 goto return_bad_req;
3873 }
3874 else {
3875 char *cur_ptr = req->buf->p;
3876 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3877 int delta;
3878
3879 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3880 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3881 http_msg_move_end(&txn->req, delta);
3882 cur_end += delta;
3883 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3884 goto return_bad_req;
3885 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003887
Willy Tarreau59234e92008-11-30 23:51:27 +01003888 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003889 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003890 * Note that doing so might move headers in the request, but
3891 * the fields will stay coherent and the URI will not move.
3892 * This should only be performed in the backend.
3893 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003894 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003895 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3896 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003897
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003899 * 8: the appsession cookie was looked up very early in 1.2,
3900 * so let's do the same now.
3901 */
3902
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003903 /* It needs to look into the URI unless persistence must be ignored */
3904 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003905 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 +01003906 }
3907
William Lallemanda73203e2012-03-12 12:48:57 +01003908 /* add unique-id if "header-unique-id" is specified */
3909
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003910 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3911 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3912 goto return_bad_req;
3913 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003914 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003915 }
William Lallemanda73203e2012-03-12 12:48:57 +01003916
3917 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003918 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3919 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003920 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003921 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003922 goto return_bad_req;
3923 }
3924
Cyril Bontéb21570a2009-11-29 20:04:48 +01003925 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003926 * 9: add X-Forwarded-For if either the frontend or the backend
3927 * asks for it.
3928 */
3929 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003930 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003931 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003932 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3933 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003934 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003935 /* The header is set to be added only if none is present
3936 * and we found it, so don't do anything.
3937 */
3938 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003939 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 /* Add an X-Forwarded-For header unless the source IP is
3941 * in the 'except' network range.
3942 */
3943 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003944 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003945 != s->fe->except_net.s_addr) &&
3946 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003947 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003948 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003949 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003950 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003951 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003952
3953 /* Note: we rely on the backend to get the header name to be used for
3954 * x-forwarded-for, because the header is really meant for the backends.
3955 * However, if the backend did not specify any option, we have to rely
3956 * on the frontend's header name.
3957 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003958 if (s->be->fwdfor_hdr_len) {
3959 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003960 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003961 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003962 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003963 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003964 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003965 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003966
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003967 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003968 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003969 }
3970 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003971 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 /* FIXME: for the sake of completeness, we should also support
3973 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003974 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 int len;
3976 char pn[INET6_ADDRSTRLEN];
3977 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003978 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003980
Willy Tarreau59234e92008-11-30 23:51:27 +01003981 /* Note: we rely on the backend to get the header name to be used for
3982 * x-forwarded-for, because the header is really meant for the backends.
3983 * However, if the backend did not specify any option, we have to rely
3984 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003985 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 if (s->be->fwdfor_hdr_len) {
3987 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 } else {
3990 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003992 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003993 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003994
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003995 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 goto return_bad_req;
3997 }
3998 }
3999
4000 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004001 * 10: add X-Original-To if either the frontend or the backend
4002 * asks for it.
4003 */
4004 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4005
4006 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004007 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004008 /* Add an X-Original-To header unless the destination IP is
4009 * in the 'except' network range.
4010 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004011 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004012
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004013 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004014 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004015 (((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 +02004016 != s->fe->except_to.s_addr) &&
4017 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004018 (((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 +02004019 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004020 int len;
4021 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004022 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004023
4024 /* Note: we rely on the backend to get the header name to be used for
4025 * x-original-to, because the header is really meant for the backends.
4026 * However, if the backend did not specify any option, we have to rely
4027 * on the frontend's header name.
4028 */
4029 if (s->be->orgto_hdr_len) {
4030 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004032 } else {
4033 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004034 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004035 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004036 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004037
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004038 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004039 goto return_bad_req;
4040 }
4041 }
4042 }
4043
Willy Tarreau50fc7772012-11-11 22:19:57 +01004044 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4045 * If an "Upgrade" token is found, the header is left untouched in order not to have
4046 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4047 * "Upgrade" is present in the Connection header.
4048 */
4049 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4050 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004051 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4052 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004053 unsigned int want_flags = 0;
4054
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004055 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004056 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004057 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4058 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004059 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004060 want_flags |= TX_CON_CLO_SET;
4061 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004062 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004063 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4064 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004065 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004066 want_flags |= TX_CON_KAL_SET;
4067 }
4068
4069 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004070 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004071 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004073
Willy Tarreau522d6c02009-12-06 18:49:18 +01004074 /* If we have no server assigned yet and we're balancing on url_param
4075 * with a POST request, we may be interested in checking the body for
4076 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004077 */
4078 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4079 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004080 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004081 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004082 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004083 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004085
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004086 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004087 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004088#ifdef TCP_QUICKACK
4089 /* We expect some data from the client. Unless we know for sure
4090 * we already have a full request, we have to re-enable quick-ack
4091 * in case we previously disabled it, otherwise we might cause
4092 * the client to delay further data.
4093 */
4094 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004095 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004096 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004097 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004098 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004099#endif
4100 }
Willy Tarreau03945942009-12-22 16:50:27 +01004101
Willy Tarreau59234e92008-11-30 23:51:27 +01004102 /*************************************************************
4103 * OK, that's finished for the headers. We have done what we *
4104 * could. Let's switch to the DATA state. *
4105 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106 req->analyse_exp = TICK_ETERNITY;
4107 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004108
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004109 /* if the server closes the connection, we want to immediately react
4110 * and close the socket to save packets and syscalls.
4111 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004112 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4113 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004114
Willy Tarreau59234e92008-11-30 23:51:27 +01004115 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004116 /* OK let's go on with the BODY now */
4117 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004118
Willy Tarreau59234e92008-11-30 23:51:27 +01004119 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004120 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004121 /* we detected a parsing error. We want to archive this request
4122 * in the dedicated proxy area for later troubleshooting.
4123 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004124 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004125 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004126
Willy Tarreau59234e92008-11-30 23:51:27 +01004127 txn->req.msg_state = HTTP_MSG_ERROR;
4128 txn->status = 400;
4129 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004130 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004131
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004132 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004133 if (s->listener->counters)
4134 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004135
Willy Tarreau59234e92008-11-30 23:51:27 +01004136 if (!(s->flags & SN_ERR_MASK))
4137 s->flags |= SN_ERR_PRXCOND;
4138 if (!(s->flags & SN_FINST_MASK))
4139 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004140 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004141}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004142
Willy Tarreau60b85b02008-11-30 23:28:40 +01004143/* This function is an analyser which processes the HTTP tarpit. It always
4144 * returns zero, at the beginning because it prevents any other processing
4145 * from occurring, and at the end because it terminates the request.
4146 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004147int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004148{
4149 struct http_txn *txn = &s->txn;
4150
4151 /* This connection is being tarpitted. The CLIENT side has
4152 * already set the connect expiration date to the right
4153 * timeout. We just have to check that the client is still
4154 * there and that the timeout has not expired.
4155 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004156 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004157 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004158 !tick_is_expired(req->analyse_exp, now_ms))
4159 return 0;
4160
4161 /* We will set the queue timer to the time spent, just for
4162 * logging purposes. We fake a 500 server error, so that the
4163 * attacker will not suspect his connection has been tarpitted.
4164 * It will not cause trouble to the logs because we can exclude
4165 * the tarpitted connections by filtering on the 'PT' status flags.
4166 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004167 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4168
4169 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004170 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004171 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004172
4173 req->analysers = 0;
4174 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004175
Willy Tarreau60b85b02008-11-30 23:28:40 +01004176 if (!(s->flags & SN_ERR_MASK))
4177 s->flags |= SN_ERR_PRXCOND;
4178 if (!(s->flags & SN_FINST_MASK))
4179 s->flags |= SN_FINST_T;
4180 return 0;
4181}
4182
Willy Tarreaud34af782008-11-30 23:36:37 +01004183/* This function is an analyser which processes the HTTP request body. It looks
4184 * for parameters to be used for the load balancing algorithm (url_param). It
4185 * must only be called after the standard HTTP request processing has occurred,
4186 * because it expects the request to be parsed. It returns zero if it needs to
4187 * read more data, or 1 once it has completed its analysis.
4188 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004189int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004190{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004191 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004192 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004193 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004194
4195 /* We have to parse the HTTP request body to find any required data.
4196 * "balance url_param check_post" should have been the only way to get
4197 * into this. We were brought here after HTTP header analysis, so all
4198 * related structures are ready.
4199 */
4200
Willy Tarreau522d6c02009-12-06 18:49:18 +01004201 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4202 goto missing_data;
4203
4204 if (msg->msg_state < HTTP_MSG_100_SENT) {
4205 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4206 * send an HTTP/1.1 100 Continue intermediate response.
4207 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004208 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004209 struct hdr_ctx ctx;
4210 ctx.idx = 0;
4211 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004212 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004213 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004214 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004215 }
4216 }
4217 msg->msg_state = HTTP_MSG_100_SENT;
4218 }
4219
4220 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004221 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004222 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004223 * is still null. We must save the body in msg->next because it
4224 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004225 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004226 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004227
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004228 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004229 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4230 else
4231 msg->msg_state = HTTP_MSG_DATA;
4232 }
4233
4234 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004235 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004236 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004238 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004239 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004240
Willy Tarreau115acb92009-12-26 13:56:06 +01004241 if (!ret)
4242 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004243 else if (ret < 0) {
4244 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004245 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004246 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004247 }
4248
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004250 * We have the first data byte is in msg->sov. We're waiting for at
4251 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004252 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004253
Willy Tarreau124d9912011-03-01 20:30:48 +01004254 if (msg->body_len < limit)
4255 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004256
Willy Tarreau9b28e032012-10-12 23:49:43 +02004257 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004258 goto http_end;
4259
4260 missing_data:
4261 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004262 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004263 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004264 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004265 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004266
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004267 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004268 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004269 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004270
4271 if (!(s->flags & SN_ERR_MASK))
4272 s->flags |= SN_ERR_CLITO;
4273 if (!(s->flags & SN_FINST_MASK))
4274 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004275 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004276 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004277
4278 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004279 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004280 /* Not enough data. We'll re-use the http-request
4281 * timeout here. Ideally, we should set the timeout
4282 * relative to the accept() date. We just set the
4283 * request timeout once at the beginning of the
4284 * request.
4285 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004286 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004287 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004288 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004289 return 0;
4290 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004291
4292 http_end:
4293 /* The situation will not evolve, so let's give up on the analysis. */
4294 s->logs.tv_request = now; /* update the request timer to reflect full request */
4295 req->analysers &= ~an_bit;
4296 req->analyse_exp = TICK_ETERNITY;
4297 return 1;
4298
4299 return_bad_req: /* let's centralize all bad requests */
4300 txn->req.msg_state = HTTP_MSG_ERROR;
4301 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004302 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004303
Willy Tarreau79ebac62010-06-07 13:47:49 +02004304 if (!(s->flags & SN_ERR_MASK))
4305 s->flags |= SN_ERR_PRXCOND;
4306 if (!(s->flags & SN_FINST_MASK))
4307 s->flags |= SN_FINST_R;
4308
Willy Tarreau522d6c02009-12-06 18:49:18 +01004309 return_err_msg:
4310 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004311 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004312 if (s->listener->counters)
4313 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004314 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004315}
4316
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004317/* send a server's name with an outgoing request over an established connection.
4318 * Note: this function is designed to be called once the request has been scheduled
4319 * for being forwarded. This is the reason why it rewinds the buffer before
4320 * proceeding.
4321 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004322int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004323
4324 struct hdr_ctx ctx;
4325
Mark Lamourinec2247f02012-01-04 13:02:01 -05004326 char *hdr_name = be->server_id_hdr_name;
4327 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004328 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004329 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004330 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004331
William Lallemandd9e90662012-01-30 17:27:17 +01004332 ctx.idx = 0;
4333
Willy Tarreau9b28e032012-10-12 23:49:43 +02004334 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004335 if (old_o) {
4336 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004337 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004338 }
4339
Willy Tarreau9b28e032012-10-12 23:49:43 +02004340 old_i = chn->buf->i;
4341 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 -05004342 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004343 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004344 }
4345
4346 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004347 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004348 memcpy(hdr_val, hdr_name, hdr_name_len);
4349 hdr_val += hdr_name_len;
4350 *hdr_val++ = ':';
4351 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004352 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4353 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004354
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004355 if (old_o) {
4356 /* If this was a forwarded request, we must readjust the amount of
4357 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004358 * variations. Note that if the request was already scheduled for
4359 * forwarding, it had its req->sol pointing to the body, which
4360 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004361 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004362 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004363 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004364 }
4365
Mark Lamourinec2247f02012-01-04 13:02:01 -05004366 return 0;
4367}
4368
Willy Tarreau610ecce2010-01-04 21:15:02 +01004369/* Terminate current transaction and prepare a new one. This is very tricky
4370 * right now but it works.
4371 */
4372void http_end_txn_clean_session(struct session *s)
4373{
Willy Tarreau068621e2013-12-23 15:11:25 +01004374 int prev_status = s->txn.status;
4375
Willy Tarreau610ecce2010-01-04 21:15:02 +01004376 /* FIXME: We need a more portable way of releasing a backend's and a
4377 * server's connections. We need a safer way to reinitialize buffer
4378 * flags. We also need a more accurate method for computing per-request
4379 * data.
4380 */
4381 http_silent_debug(__LINE__, s);
4382
Willy Tarreau4213a112013-12-15 10:25:42 +01004383 /* unless we're doing keep-alive, we want to quickly close the connection
4384 * to the server.
4385 */
4386 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4387 !si_conn_ready(s->req->cons)) {
4388 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4389 si_shutr(s->req->cons);
4390 si_shutw(s->req->cons);
4391 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392
4393 http_silent_debug(__LINE__, s);
4394
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004395 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004397 if (unlikely(s->srv_conn))
4398 sess_change_server(s, NULL);
4399 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004400
4401 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4402 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004403 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404
4405 if (s->txn.status) {
4406 int n;
4407
4408 n = s->txn.status / 100;
4409 if (n < 1 || n > 5)
4410 n = 0;
4411
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004412 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004413 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004414 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004415 s->fe->fe_counters.p.http.comp_rsp++;
4416 }
Willy Tarreau24657792010-02-26 10:30:28 +01004417 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004418 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004419 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004420 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004421 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004422 s->be->be_counters.p.http.comp_rsp++;
4423 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004424 }
4425
4426 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004427 s->logs.bytes_in -= s->req->buf->i;
4428 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429
4430 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004431 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432 !(s->flags & SN_MONITOR) &&
4433 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4434 s->do_log(s);
4435 }
4436
4437 s->logs.accept_date = date; /* user-visible date for logging */
4438 s->logs.tv_accept = now; /* corrected date for internal use */
4439 tv_zero(&s->logs.tv_request);
4440 s->logs.t_queue = -1;
4441 s->logs.t_connect = -1;
4442 s->logs.t_data = -1;
4443 s->logs.t_close = 0;
4444 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4445 s->logs.srv_queue_size = 0; /* we will get this number soon */
4446
Willy Tarreau9b28e032012-10-12 23:49:43 +02004447 s->logs.bytes_in = s->req->total = s->req->buf->i;
4448 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004449
4450 if (s->pend_pos)
4451 pendconn_free(s->pend_pos);
4452
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004453 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454 if (s->flags & SN_CURR_SESS) {
4455 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004456 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004458 if (may_dequeue_tasks(objt_server(s->target), s->be))
4459 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004460 }
4461
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004462 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463
Willy Tarreau4213a112013-12-15 10:25:42 +01004464 /* only release our endpoint if we don't intend to reuse the
4465 * connection.
4466 */
4467 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4468 !si_conn_ready(s->req->cons)) {
4469 si_release_endpoint(s->req->cons);
4470 }
4471
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004473 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004474 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004476 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004477 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4478 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 +02004479 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau36346242014-02-24 18:26:30 +01004480 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004481
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 s->txn.meth = 0;
4483 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004484 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004485
4486 if (prev_status == 401 || prev_status == 407) {
4487 /* In HTTP keep-alive mode, if we receive a 401, we still have
4488 * a chance of being able to send the visitor again to the same
4489 * server over the same connection. This is required by some
4490 * broken protocols such as NTLM, and anyway whenever there is
4491 * an opportunity for sending the challenge to the proper place,
4492 * it's better to do it (at least it helps with debugging).
4493 */
4494 s->txn.flags |= TX_PREFER_LAST;
4495 }
4496
Willy Tarreauee55dc02010-06-01 10:56:34 +02004497 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 s->req->cons->flags |= SI_FL_INDEP_STR;
4499
Willy Tarreau96e31212011-05-30 18:10:30 +02004500 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004501 s->req->flags |= CF_NEVER_WAIT;
4502 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004503 }
4504
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505 /* if the request buffer is not empty, it means we're
4506 * about to process another request, so send pending
4507 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004508 * Just don't do this if the buffer is close to be full,
4509 * because the request will wait for it to flush a little
4510 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004512 if (s->req->buf->i) {
4513 if (s->rep->buf->o &&
4514 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4515 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004516 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004517 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004518
4519 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004520 channel_auto_read(s->req);
4521 channel_auto_close(s->req);
4522 channel_auto_read(s->rep);
4523 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004524
Willy Tarreau27375622013-12-17 00:00:28 +01004525 /* we're in keep-alive with an idle connection, monitor it */
4526 si_idle_conn(s->req->cons);
4527
Willy Tarreau342b11c2010-11-24 16:22:09 +01004528 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004529 s->rep->analysers = 0;
4530
4531 http_silent_debug(__LINE__, s);
4532}
4533
4534
4535/* This function updates the request state machine according to the response
4536 * state machine and buffer flags. It returns 1 if it changes anything (flag
4537 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4538 * it is only used to find when a request/response couple is complete. Both
4539 * this function and its equivalent should loop until both return zero. It
4540 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4541 */
4542int http_sync_req_state(struct session *s)
4543{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004544 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 unsigned int old_state = txn->req.msg_state;
4548
4549 http_silent_debug(__LINE__, s);
4550 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4551 return 0;
4552
4553 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004554 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004555 * We can shut the read side unless we want to abort_on_close,
4556 * or we have a POST request. The issue with POST requests is
4557 * that some browsers still send a CRLF after the request, and
4558 * this CRLF must be read so that it does not remain in the kernel
4559 * buffers, otherwise a close could cause an RST on some systems
4560 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004561 * Note that if we're using keep-alive on the client side, we'd
4562 * rather poll now and keep the polling enabled for the whole
4563 * session's life than enabling/disabling it between each
4564 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004565 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004566 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4567 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4568 !(s->be->options & PR_O_ABRT_CLOSE) &&
4569 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004570 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571
Willy Tarreau40f151a2012-12-20 12:10:09 +01004572 /* if the server closes the connection, we want to immediately react
4573 * and close the socket to save packets and syscalls.
4574 */
4575 chn->cons->flags |= SI_FL_NOHALF;
4576
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4578 goto wait_other_side;
4579
4580 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4581 /* The server has not finished to respond, so we
4582 * don't want to move in order not to upset it.
4583 */
4584 goto wait_other_side;
4585 }
4586
4587 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4588 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004589 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004591 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 goto wait_other_side;
4593 }
4594
4595 /* When we get here, it means that both the request and the
4596 * response have finished receiving. Depending on the connection
4597 * mode, we'll have to wait for the last bytes to leave in either
4598 * direction, and sometimes for a close to be effective.
4599 */
4600
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004601 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4602 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4604 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004605 }
4606 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4607 /* Option forceclose is set, or either side wants to close,
4608 * let's enforce it now that we're not expecting any new
4609 * data to come. The caller knows the session is complete
4610 * once both states are CLOSED.
4611 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004612 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4613 channel_shutr_now(chn);
4614 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004616 }
4617 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004618 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4619 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004620 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004621 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4622 channel_auto_read(chn);
4623 txn->req.msg_state = HTTP_MSG_TUNNEL;
4624 chn->flags |= CF_NEVER_WAIT;
4625 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 }
4627
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004628 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004630 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004631
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004632 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 txn->req.msg_state = HTTP_MSG_CLOSING;
4634 goto http_msg_closing;
4635 }
4636 else {
4637 txn->req.msg_state = HTTP_MSG_CLOSED;
4638 goto http_msg_closed;
4639 }
4640 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004641 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642 }
4643
4644 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4645 http_msg_closing:
4646 /* nothing else to forward, just waiting for the output buffer
4647 * to be empty and for the shutw_now to take effect.
4648 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004649 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650 txn->req.msg_state = HTTP_MSG_CLOSED;
4651 goto http_msg_closed;
4652 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004653 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654 txn->req.msg_state = HTTP_MSG_ERROR;
4655 goto wait_other_side;
4656 }
4657 }
4658
4659 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4660 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004661 /* see above in MSG_DONE why we only do this in these states */
4662 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4663 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4664 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004665 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004666 goto wait_other_side;
4667 }
4668
4669 wait_other_side:
4670 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004671 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672}
4673
4674
4675/* This function updates the response state machine according to the request
4676 * state machine and buffer flags. It returns 1 if it changes anything (flag
4677 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4678 * it is only used to find when a request/response couple is complete. Both
4679 * this function and its equivalent should loop until both return zero. It
4680 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4681 */
4682int http_sync_res_state(struct session *s)
4683{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004684 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004686 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 unsigned int old_state = txn->rsp.msg_state;
4688
4689 http_silent_debug(__LINE__, s);
4690 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4691 return 0;
4692
4693 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4694 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004695 * still monitor the server connection for a possible close
4696 * while the request is being uploaded, so we don't disable
4697 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004698 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004699 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004700
4701 if (txn->req.msg_state == HTTP_MSG_ERROR)
4702 goto wait_other_side;
4703
4704 if (txn->req.msg_state < HTTP_MSG_DONE) {
4705 /* The client seems to still be sending data, probably
4706 * because we got an error response during an upload.
4707 * We have the choice of either breaking the connection
4708 * or letting it pass through. Let's do the later.
4709 */
4710 goto wait_other_side;
4711 }
4712
4713 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4714 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004715 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004717 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004718 goto wait_other_side;
4719 }
4720
4721 /* When we get here, it means that both the request and the
4722 * response have finished receiving. Depending on the connection
4723 * mode, we'll have to wait for the last bytes to leave in either
4724 * direction, and sometimes for a close to be effective.
4725 */
4726
4727 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4728 /* Server-close mode : shut read and wait for the request
4729 * side to close its output buffer. The caller will detect
4730 * when we're in DONE and the other is in CLOSED and will
4731 * catch that for the final cleanup.
4732 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004733 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4734 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004735 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004736 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4737 /* Option forceclose is set, or either side wants to close,
4738 * let's enforce it now that we're not expecting any new
4739 * data to come. The caller knows the session is complete
4740 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004742 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4743 channel_shutr_now(chn);
4744 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004745 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004746 }
4747 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004748 /* The last possible modes are keep-alive and tunnel. Tunnel will
4749 * need to forward remaining data. Keep-alive will need to monitor
4750 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004752 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004753 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004754 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4755 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 }
4757
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004758 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004760 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4762 goto http_msg_closing;
4763 }
4764 else {
4765 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4766 goto http_msg_closed;
4767 }
4768 }
4769 goto wait_other_side;
4770 }
4771
4772 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4773 http_msg_closing:
4774 /* nothing else to forward, just waiting for the output buffer
4775 * to be empty and for the shutw_now to take effect.
4776 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004777 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004778 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4779 goto http_msg_closed;
4780 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004781 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004783 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004784 if (objt_server(s->target))
4785 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004786 goto wait_other_side;
4787 }
4788 }
4789
4790 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4791 http_msg_closed:
4792 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004793 bi_erase(chn);
4794 channel_auto_close(chn);
4795 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004796 goto wait_other_side;
4797 }
4798
4799 wait_other_side:
4800 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004801 /* We force the response to leave immediately if we're waiting for the
4802 * other side, since there is no pending shutdown to push it out.
4803 */
4804 if (!channel_is_empty(chn))
4805 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004806 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807}
4808
4809
4810/* Resync the request and response state machines. Return 1 if either state
4811 * changes.
4812 */
4813int http_resync_states(struct session *s)
4814{
4815 struct http_txn *txn = &s->txn;
4816 int old_req_state = txn->req.msg_state;
4817 int old_res_state = txn->rsp.msg_state;
4818
4819 http_silent_debug(__LINE__, s);
4820 http_sync_req_state(s);
4821 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004822 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 if (!http_sync_res_state(s))
4824 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004825 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826 if (!http_sync_req_state(s))
4827 break;
4828 }
4829 http_silent_debug(__LINE__, s);
4830 /* OK, both state machines agree on a compatible state.
4831 * There are a few cases we're interested in :
4832 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4833 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4834 * directions, so let's simply disable both analysers.
4835 * - HTTP_MSG_CLOSED on the response only means we must abort the
4836 * request.
4837 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4838 * with server-close mode means we've completed one request and we
4839 * must re-initialize the server connection.
4840 */
4841
4842 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4843 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4844 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4845 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4846 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_auto_close(s->req);
4848 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004850 channel_auto_close(s->rep);
4851 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004853 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4854 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004855 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004856 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004857 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004858 channel_auto_close(s->rep);
4859 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004860 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004861 channel_abort(s->req);
4862 channel_auto_close(s->req);
4863 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004864 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004865 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004866 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4867 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004868 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004869 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4870 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4871 /* server-close/keep-alive: terminate this transaction,
4872 * possibly killing the server connection and reinitialize
4873 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004874 */
4875 http_end_txn_clean_session(s);
4876 }
4877
4878 http_silent_debug(__LINE__, s);
4879 return txn->req.msg_state != old_req_state ||
4880 txn->rsp.msg_state != old_res_state;
4881}
4882
Willy Tarreaud98cf932009-12-27 22:54:55 +01004883/* This function is an analyser which forwards request body (including chunk
4884 * sizes if any). It is called as soon as we must forward, even if we forward
4885 * zero byte. The only situation where it must not be called is when we're in
4886 * tunnel mode and we want to forward till the close. It's used both to forward
4887 * remaining data and to resync after end of body. It expects the msg_state to
4888 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4889 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004890 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004891 * bytes of pending data + the headers if not already done (between sol and sov).
4892 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004893 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004894int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004895{
4896 struct http_txn *txn = &s->txn;
4897 struct http_msg *msg = &s->txn.req;
4898
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004899 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4900 return 0;
4901
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004902 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004903 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004904 /* Output closed while we were sending data. We must abort and
4905 * wake the other side up.
4906 */
4907 msg->msg_state = HTTP_MSG_ERROR;
4908 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004909 return 1;
4910 }
4911
Willy Tarreau80a92c02014-03-12 10:41:13 +01004912 /* Some post-connect processing might want us to refrain from starting to
4913 * forward data. Currently, the only reason for this is "balance url_param"
4914 * whichs need to parse/process the request after we've enabled forwarding.
4915 */
4916 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
4917 if (!(s->rep->flags & CF_READ_ATTACHED)) {
4918 channel_auto_connect(req);
4919 goto missing_data;
4920 }
4921 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4922 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923
4924 /* Note that we don't have to send 100-continue back because we don't
4925 * need the data to complete our job, and it's up to the server to
4926 * decide whether to return 100, 417 or anything else in return of
4927 * an "Expect: 100-continue" header.
4928 */
4929
4930 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004931 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004932 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004933 * is still null. We must save the body in msg->next because it
4934 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004935 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004936 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004937
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004938 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004939 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004940 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004941 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004942 }
4943
Willy Tarreau80a92c02014-03-12 10:41:13 +01004944 /* in most states, we should abort in case of early close */
4945 channel_auto_close(req);
4946
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004948 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004949
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004951 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004952 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004953 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004954 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004955 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004956 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004957 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004958 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004959
Willy Tarreaucaabe412010-01-03 23:08:28 +01004960 if (msg->msg_state == HTTP_MSG_DATA) {
4961 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004962 if (req->to_forward) {
4963 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004964 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004965 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004966
4967 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004968 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004969 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004971 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004972 }
4973 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004974 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004975 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004976 * TRAILERS state.
4977 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004978 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004979
Willy Tarreau54d23df2012-10-25 19:04:45 +02004980 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004981 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004982 else if (ret < 0) {
4983 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004984 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004985 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004986 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004987 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004988 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004989 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004990 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004991 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004992 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993
4994 if (ret == 0)
4995 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004996 else if (ret < 0) {
4997 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004998 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004999 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005000 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005001 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005002 /* we're in MSG_CHUNK_SIZE now */
5003 }
5004 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005005 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005006
5007 if (ret == 0)
5008 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005009 else if (ret < 0) {
5010 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005011 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005012 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005013 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005014 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005015 /* we're in HTTP_MSG_DONE now */
5016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005018 int old_state = msg->msg_state;
5019
Willy Tarreau610ecce2010-01-04 21:15:02 +01005020 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005021 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005022 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5023 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005024 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025 if (http_resync_states(s)) {
5026 /* some state changes occurred, maybe the analyser
5027 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005028 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005029 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005030 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005031 /* request errors are most likely due to
5032 * the server aborting the transfer.
5033 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005034 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005035 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005036 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005037 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005039 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005041 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005042
5043 /* If "option abortonclose" is set on the backend, we
5044 * want to monitor the client's connection and forward
5045 * any shutdown notification to the server, which will
5046 * decide whether to close or to go on processing the
5047 * request.
5048 */
5049 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005050 channel_auto_read(req);
5051 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005052 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005053 else if (s->txn.meth == HTTP_METH_POST) {
5054 /* POST requests may require to read extra CRLF
5055 * sent by broken browsers and which could cause
5056 * an RST to be sent upon close on some systems
5057 * (eg: Linux).
5058 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005059 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005060 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005061
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063 }
5064 }
5065
Willy Tarreaud98cf932009-12-27 22:54:55 +01005066 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005067 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005068 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005069 if (!(s->flags & SN_ERR_MASK))
5070 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005071 if (!(s->flags & SN_FINST_MASK)) {
5072 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5073 s->flags |= SN_FINST_H;
5074 else
5075 s->flags |= SN_FINST_D;
5076 }
5077
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005078 s->fe->fe_counters.cli_aborts++;
5079 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005080 if (objt_server(s->target))
5081 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005082
5083 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005084 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005085
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005086 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005087 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005088 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005089
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005090 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005091 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005092 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005093 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005094 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005095
Willy Tarreau5c620922011-05-11 19:56:11 +02005096 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005097 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005098 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005099 * modes are already handled by the stream sock layer. We must not do
5100 * this in content-length mode because it could present the MSG_MORE
5101 * flag with the last block of forwarded data, which would cause an
5102 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005103 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005104 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005105 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005106
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005108 return 0;
5109
Willy Tarreaud98cf932009-12-27 22:54:55 +01005110 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005111 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005112 if (s->listener->counters)
5113 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005114 return_bad_req_stats_ok:
5115 txn->req.msg_state = HTTP_MSG_ERROR;
5116 if (txn->status) {
5117 /* Note: we don't send any error if some data were already sent */
5118 stream_int_retnclose(req->prod, NULL);
5119 } else {
5120 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005121 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005122 }
5123 req->analysers = 0;
5124 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005125
5126 if (!(s->flags & SN_ERR_MASK))
5127 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005128 if (!(s->flags & SN_FINST_MASK)) {
5129 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5130 s->flags |= SN_FINST_H;
5131 else
5132 s->flags |= SN_FINST_D;
5133 }
5134 return 0;
5135
5136 aborted_xfer:
5137 txn->req.msg_state = HTTP_MSG_ERROR;
5138 if (txn->status) {
5139 /* Note: we don't send any error if some data were already sent */
5140 stream_int_retnclose(req->prod, NULL);
5141 } else {
5142 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005143 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005144 }
5145 req->analysers = 0;
5146 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5147
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005148 s->fe->fe_counters.srv_aborts++;
5149 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005150 if (objt_server(s->target))
5151 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005152
5153 if (!(s->flags & SN_ERR_MASK))
5154 s->flags |= SN_ERR_SRVCL;
5155 if (!(s->flags & SN_FINST_MASK)) {
5156 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5157 s->flags |= SN_FINST_H;
5158 else
5159 s->flags |= SN_FINST_D;
5160 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005161 return 0;
5162}
5163
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005164/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5165 * processing can continue on next analysers, or zero if it either needs more
5166 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5167 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5168 * when it has nothing left to do, and may remove any analyser when it wants to
5169 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005170 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005171int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005172{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005173 struct http_txn *txn = &s->txn;
5174 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005175 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005176 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005177 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005178 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005179
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005180 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 +02005181 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005183 rep,
5184 rep->rex, rep->wex,
5185 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005186 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005187 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 /*
5190 * Now parse the partial (or complete) lines.
5191 * We will check the response syntax, and also join multi-line
5192 * headers. An index of all the lines will be elaborated while
5193 * parsing.
5194 *
5195 * For the parsing, we use a 28 states FSM.
5196 *
5197 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005198 * rep->buf->p = beginning of response
5199 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5200 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005201 * msg->eol = end of current header or line (LF or CRLF)
5202 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 */
5204
Willy Tarreau83e3af02009-12-28 17:39:57 +01005205 /* There's a protected area at the end of the buffer for rewriting
5206 * purposes. We don't want to start to parse the request if the
5207 * protected area is affected, because we may have to move processed
5208 * data later, which is much more complicated.
5209 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005210 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005211 if (unlikely(!channel_reserved(rep))) {
5212 /* some data has still not left the buffer, wake us once that's done */
5213 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5214 goto abort_response;
5215 channel_dont_close(rep);
5216 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005217 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005218 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005219 }
5220
Willy Tarreau379357a2013-06-08 12:55:46 +02005221 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5222 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5223 buffer_slow_realign(rep->buf);
5224
Willy Tarreau9b28e032012-10-12 23:49:43 +02005225 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005226 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005227 }
5228
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005229 /* 1: we might have to print this header in debug mode */
5230 if (unlikely((global.mode & MODE_DEBUG) &&
5231 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005232 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005234
Willy Tarreau9b28e032012-10-12 23:49:43 +02005235 sol = rep->buf->p;
5236 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005237 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005238
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005239 sol += hdr_idx_first_pos(&txn->hdr_idx);
5240 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005241
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242 while (cur_idx) {
5243 eol = sol + txn->hdr_idx.v[cur_idx].len;
5244 debug_hdr("srvhdr", s, sol, eol);
5245 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5246 cur_idx = txn->hdr_idx.v[cur_idx].next;
5247 }
5248 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250 /*
5251 * Now we quickly check if we have found a full valid response.
5252 * If not so, we check the FD and buffer states before leaving.
5253 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005254 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 * responses are checked first.
5256 *
5257 * Depending on whether the client is still there or not, we
5258 * may send an error response back or not. Note that normally
5259 * we should only check for HTTP status there, and check I/O
5260 * errors somewhere else.
5261 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005262
Willy Tarreau655dce92009-11-08 13:10:58 +01005263 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 /* Invalid response */
5265 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5266 /* we detected a parsing error. We want to archive this response
5267 * in the dedicated proxy area for later troubleshooting.
5268 */
5269 hdr_response_bad:
5270 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005271 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005273 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005274 if (objt_server(s->target)) {
5275 objt_server(s->target)->counters.failed_resp++;
5276 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005277 }
Willy Tarreau64648412010-03-05 10:41:54 +01005278 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005279 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 rep->analysers = 0;
5281 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005282 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005283 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005284 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005285
5286 if (!(s->flags & SN_ERR_MASK))
5287 s->flags |= SN_ERR_PRXCOND;
5288 if (!(s->flags & SN_FINST_MASK))
5289 s->flags |= SN_FINST_H;
5290
5291 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005293
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005295 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005296 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005297 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005298 goto hdr_response_bad;
5299 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005300
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005302 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005304 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005305 else if (txn->flags & TX_NOT_FIRST)
5306 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005307
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005308 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005309 if (objt_server(s->target)) {
5310 objt_server(s->target)->counters.failed_resp++;
5311 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005312 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005313
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005314 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 rep->analysers = 0;
5316 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005317 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005318 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005319 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005320
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005321 if (!(s->flags & SN_ERR_MASK))
5322 s->flags |= SN_ERR_SRVCL;
5323 if (!(s->flags & SN_FINST_MASK))
5324 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005325 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005327
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005329 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005331 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005332 else if (txn->flags & TX_NOT_FIRST)
5333 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005334
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005335 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005336 if (objt_server(s->target)) {
5337 objt_server(s->target)->counters.failed_resp++;
5338 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005339 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005340
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005341 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 rep->analysers = 0;
5343 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005344 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005345 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005346 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005347
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005348 if (!(s->flags & SN_ERR_MASK))
5349 s->flags |= SN_ERR_SRVTO;
5350 if (!(s->flags & SN_FINST_MASK))
5351 s->flags |= SN_FINST_H;
5352 return 0;
5353 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005354
Willy Tarreauf003d372012-11-26 13:35:37 +01005355 /* client abort with an abortonclose */
5356 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5357 s->fe->fe_counters.cli_aborts++;
5358 s->be->be_counters.cli_aborts++;
5359 if (objt_server(s->target))
5360 objt_server(s->target)->counters.cli_aborts++;
5361
5362 rep->analysers = 0;
5363 channel_auto_close(rep);
5364
5365 txn->status = 400;
5366 bi_erase(rep);
5367 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5368
5369 if (!(s->flags & SN_ERR_MASK))
5370 s->flags |= SN_ERR_CLICL;
5371 if (!(s->flags & SN_FINST_MASK))
5372 s->flags |= SN_FINST_H;
5373
5374 /* process_session() will take care of the error */
5375 return 0;
5376 }
5377
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005378 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005379 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005380 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005381 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005382 else if (txn->flags & TX_NOT_FIRST)
5383 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005384
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005385 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005386 if (objt_server(s->target)) {
5387 objt_server(s->target)->counters.failed_resp++;
5388 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005389 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005390
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005391 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005392 rep->analysers = 0;
5393 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005394 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005395 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005396 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005397
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 if (!(s->flags & SN_ERR_MASK))
5399 s->flags |= SN_ERR_SRVCL;
5400 if (!(s->flags & SN_FINST_MASK))
5401 s->flags |= SN_FINST_H;
5402 return 0;
5403 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005404
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005405 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005406 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005408 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005409 else if (txn->flags & TX_NOT_FIRST)
5410 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005411
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005412 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005414 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415
5416 if (!(s->flags & SN_ERR_MASK))
5417 s->flags |= SN_ERR_CLICL;
5418 if (!(s->flags & SN_FINST_MASK))
5419 s->flags |= SN_FINST_H;
5420
5421 /* process_session() will take care of the error */
5422 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005423 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005424
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005425 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005426 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427 return 0;
5428 }
5429
5430 /* More interesting part now : we know that we have a complete
5431 * response which at least looks like HTTP. We have an indicator
5432 * of each header's length, so we can parse them quickly.
5433 */
5434
5435 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005436 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005438 /*
5439 * 1: get the status code
5440 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005441 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442 if (n < 1 || n > 5)
5443 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005444 /* when the client triggers a 4xx from the server, it's most often due
5445 * to a missing object or permission. These events should be tracked
5446 * because if they happen often, it may indicate a brute force or a
5447 * vulnerability scan.
5448 */
5449 if (n == 4)
5450 session_inc_http_err_ctr(s);
5451
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005452 if (objt_server(s->target))
5453 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005454
Willy Tarreau5b154472009-12-21 20:11:07 +01005455 /* check if the response is HTTP/1.1 or above */
5456 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005457 ((rep->buf->p[5] > '1') ||
5458 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005459 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005460
5461 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005462 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 +01005463
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005464 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005465 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005466
Willy Tarreau9b28e032012-10-12 23:49:43 +02005467 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005468
Willy Tarreau39650402010-03-15 19:44:39 +01005469 /* Adjust server's health based on status code. Note: status codes 501
5470 * and 505 are triggered on demand by client request, so we must not
5471 * count them as server failures.
5472 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005473 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005474 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005475 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005476 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005477 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005478 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005479
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005480 /*
5481 * 2: check for cacheability.
5482 */
5483
5484 switch (txn->status) {
5485 case 200:
5486 case 203:
5487 case 206:
5488 case 300:
5489 case 301:
5490 case 410:
5491 /* RFC2616 @13.4:
5492 * "A response received with a status code of
5493 * 200, 203, 206, 300, 301 or 410 MAY be stored
5494 * by a cache (...) unless a cache-control
5495 * directive prohibits caching."
5496 *
5497 * RFC2616 @9.5: POST method :
5498 * "Responses to this method are not cacheable,
5499 * unless the response includes appropriate
5500 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005501 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005502 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005503 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005504 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5505 break;
5506 default:
5507 break;
5508 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005509
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005510 /*
5511 * 3: we may need to capture headers
5512 */
5513 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005514 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005515 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005516 txn->rsp.cap, s->fe->rsp_cap);
5517
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005518 /* 4: determine the transfer-length.
5519 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5520 * the presence of a message-body in a RESPONSE and its transfer length
5521 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005522 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005523 * All responses to the HEAD request method MUST NOT include a
5524 * message-body, even though the presence of entity-header fields
5525 * might lead one to believe they do. All 1xx (informational), 204
5526 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5527 * message-body. All other responses do include a message-body,
5528 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005529 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005530 * 1. Any response which "MUST NOT" include a message-body (such as the
5531 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5532 * always terminated by the first empty line after the header fields,
5533 * regardless of the entity-header fields present in the message.
5534 *
5535 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5536 * the "chunked" transfer-coding (Section 6.2) is used, the
5537 * transfer-length is defined by the use of this transfer-coding.
5538 * If a Transfer-Encoding header field is present and the "chunked"
5539 * transfer-coding is not present, the transfer-length is defined by
5540 * the sender closing the connection.
5541 *
5542 * 3. If a Content-Length header field is present, its decimal value in
5543 * OCTETs represents both the entity-length and the transfer-length.
5544 * If a message is received with both a Transfer-Encoding header
5545 * field and a Content-Length header field, the latter MUST be ignored.
5546 *
5547 * 4. If the message uses the media type "multipart/byteranges", and
5548 * the transfer-length is not otherwise specified, then this self-
5549 * delimiting media type defines the transfer-length. This media
5550 * type MUST NOT be used unless the sender knows that the recipient
5551 * can parse it; the presence in a request of a Range header with
5552 * multiple byte-range specifiers from a 1.1 client implies that the
5553 * client can parse multipart/byteranges responses.
5554 *
5555 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556 */
5557
5558 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005559 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005560 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005561 * FIXME: should we parse anyway and return an error on chunked encoding ?
5562 */
5563 if (txn->meth == HTTP_METH_HEAD ||
5564 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005565 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005566 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005567 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005568 goto skip_content_length;
5569 }
5570
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005571 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005573 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005574 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005575 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005576 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5577 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 /* bad transfer-encoding (chunked followed by something else) */
5579 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005580 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005581 break;
5582 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583 }
5584
5585 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5586 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005587 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005588 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005589 signed long long cl;
5590
Willy Tarreauad14f752011-09-02 20:33:27 +02005591 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005592 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005593 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005594 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005595
Willy Tarreauad14f752011-09-02 20:33:27 +02005596 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005597 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005598 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005599 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005600
Willy Tarreauad14f752011-09-02 20:33:27 +02005601 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005602 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005603 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005604 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005605
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005606 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005607 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005608 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005609 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005610
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005611 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005612 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005613 }
5614
William Lallemand82fe75c2012-10-23 10:25:10 +02005615 if (s->fe->comp || s->be->comp)
5616 select_compression_response_header(s, rep->buf);
5617
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005618 /* FIXME: we should also implement the multipart/byterange method.
5619 * For now on, we resort to close mode in this case (unknown length).
5620 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005621skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005622
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005623 /* end of job, return OK */
5624 rep->analysers &= ~an_bit;
5625 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005626 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005627 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005628
5629 abort_keep_alive:
5630 /* A keep-alive request to the server failed on a network error.
5631 * The client is required to retry. We need to close without returning
5632 * any other information so that the client retries.
5633 */
5634 txn->status = 0;
5635 rep->analysers = 0;
5636 s->req->analysers = 0;
5637 channel_auto_close(rep);
5638 s->logs.logwait = 0;
5639 s->logs.level = 0;
5640 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5641 bi_erase(rep);
5642 stream_int_retnclose(rep->cons, NULL);
5643 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005644}
5645
5646/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005647 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5648 * and updates t->rep->analysers. It might make sense to explode it into several
5649 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005650 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005651int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005652{
5653 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005654 struct http_msg *msg = &txn->rsp;
5655 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005656 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005657 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005658
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005659 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 +02005660 now_ms, __FUNCTION__,
5661 t,
5662 rep,
5663 rep->rex, rep->wex,
5664 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005665 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 rep->analysers);
5667
Willy Tarreau655dce92009-11-08 13:10:58 +01005668 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005669 return 0;
5670
5671 rep->analysers &= ~an_bit;
5672 rep->analyse_exp = TICK_ETERNITY;
5673
Willy Tarreau5b154472009-12-21 20:11:07 +01005674 /* Now we have to check if we need to modify the Connection header.
5675 * This is more difficult on the response than it is on the request,
5676 * because we can have two different HTTP versions and we don't know
5677 * how the client will interprete a response. For instance, let's say
5678 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5679 * HTTP/1.1 response without any header. Maybe it will bound itself to
5680 * HTTP/1.0 because it only knows about it, and will consider the lack
5681 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5682 * the lack of header as a keep-alive. Thus we will use two flags
5683 * indicating how a request MAY be understood by the client. In case
5684 * of multiple possibilities, we'll fix the header to be explicit. If
5685 * ambiguous cases such as both close and keepalive are seen, then we
5686 * will fall back to explicit close. Note that we won't take risks with
5687 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005688 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005689 */
5690
Willy Tarreaudc008c52010-02-01 16:20:08 +01005691 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5692 txn->status == 101)) {
5693 /* Either we've established an explicit tunnel, or we're
5694 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005695 * to understand the next protocols. We have to switch to tunnel
5696 * mode, so that we transfer the request and responses then let
5697 * this protocol pass unmodified. When we later implement specific
5698 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005699 * header which contains information about that protocol for
5700 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005701 */
5702 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5703 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005704 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5705 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005706 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5707 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005708 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005709
Willy Tarreau70dffda2014-01-30 03:07:23 +01005710 /* this situation happens when combining pretend-keepalive with httpclose. */
5711 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5712 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5713 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5714 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5715
Willy Tarreau60466522010-01-18 19:08:45 +01005716 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005717 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005718 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5719 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005720
Willy Tarreau60466522010-01-18 19:08:45 +01005721 /* now adjust header transformations depending on current state */
5722 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5723 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5724 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005725 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005726 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005727 }
Willy Tarreau60466522010-01-18 19:08:45 +01005728 else { /* SCL / KAL */
5729 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005730 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005731 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005732 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005733
Willy Tarreau60466522010-01-18 19:08:45 +01005734 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005735 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005736
Willy Tarreau60466522010-01-18 19:08:45 +01005737 /* Some keep-alive responses are converted to Server-close if
5738 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005739 */
Willy Tarreau60466522010-01-18 19:08:45 +01005740 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5741 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005742 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005743 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005744 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005745 }
5746
Willy Tarreau7959a552013-09-23 16:44:27 +02005747 /* we want to have the response time before we start processing it */
5748 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5749
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005750 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 /*
5752 * 3: we will have to evaluate the filters.
5753 * As opposed to version 1.2, now they will be evaluated in the
5754 * filters order and not in the header order. This means that
5755 * each filter has to be validated among all headers.
5756 *
5757 * Filters are tried with ->be first, then with ->fe if it is
5758 * different from ->be.
5759 */
5760
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005761 cur_proxy = t->be;
5762 while (1) {
5763 struct proxy *rule_set = cur_proxy;
5764
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005765 /* evaluate http-response rules */
5766 if (!http_res_last_rule)
5767 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5768
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769 /* try headers filters */
5770 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005771 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005772 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005773 if (objt_server(t->target)) {
5774 objt_server(t->target)->counters.failed_resp++;
5775 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005776 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005777 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005778 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005779 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005781 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005782 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005783 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005784 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785 if (!(t->flags & SN_ERR_MASK))
5786 t->flags |= SN_ERR_PRXCOND;
5787 if (!(t->flags & SN_FINST_MASK))
5788 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005789 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005790 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005791 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005792
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793 /* has the response been denied ? */
5794 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005795 if (objt_server(t->target))
5796 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005797
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005798 t->be->be_counters.denied_resp++;
5799 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005800 if (t->listener->counters)
5801 t->listener->counters->denied_resp++;
5802
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005803 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005805
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005807 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005808 if (txn->status < 200)
5809 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005810 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005811 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005812 ret = acl_pass(ret);
5813 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5814 ret = !ret;
5815 if (!ret)
5816 continue;
5817 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005818 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005819 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005820 }
5821
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005822 /* check whether we're already working on the frontend */
5823 if (cur_proxy == t->fe)
5824 break;
5825 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005826 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005827
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005828 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005829 * We may be facing a 100-continue response, in which case this
5830 * is not the right response, and we're waiting for the next one.
5831 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005832 * next one.
5833 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005834 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005835 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005836 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005837 msg->msg_state = HTTP_MSG_RPBEFORE;
5838 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005839 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005840 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5841 return 1;
5842 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005843 else if (unlikely(txn->status < 200))
5844 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005845
5846 /* we don't have any 1xx status code now */
5847
5848 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005849 * 4: check for server cookie.
5850 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005851 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5852 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005853 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005854
Willy Tarreaubaaee002006-06-26 02:48:02 +02005855
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005857 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 */
Willy Tarreau67402132012-05-31 20:40:20 +02005859 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005860 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005861
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005862 /*
5863 * 6: add server cookie in the response if needed
5864 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005865 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005866 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005867 (!(t->flags & SN_DIRECT) ||
5868 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5869 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5870 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5871 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005872 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005873 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005874 /* the server is known, it's not the one the client requested, or the
5875 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 * insert a set-cookie here, except if we want to insert only on POST
5877 * requests and this one isn't. Note that servers which don't have cookies
5878 * (eg: some backup servers) will return a full cookie removal request.
5879 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005880 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005881 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005882 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5883 t->be->cookie_name);
5884 }
5885 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005886 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005887
5888 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5889 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005890 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5891 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5892 trash.len += 5;
5893
Willy Tarreauef4f3912010-10-07 21:00:29 +02005894 if (t->be->cookie_maxlife) {
5895 /* emit first_date, which is either the original one or
5896 * the current date.
5897 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005898 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005899 s30tob64(txn->cookie_first_date ?
5900 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005901 (date.tv_sec+3) >> 2, trash.str + trash.len);
5902 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005903 }
5904 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005905 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005906 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005907
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005908 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005910
Willy Tarreau4992dd22012-05-31 21:02:17 +02005911 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005913
5914 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005915 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005916
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005918 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005919
Willy Tarreauf1348312010-10-07 15:54:11 +02005920 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005921 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005922 /* the server did not change, only the date was updated */
5923 txn->flags |= TX_SCK_UPDATED;
5924 else
5925 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005926
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005927 /* Here, we will tell an eventual cache on the client side that we don't
5928 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5929 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5930 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5931 */
Willy Tarreau67402132012-05-31 20:40:20 +02005932 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005933
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005934 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5935
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005936 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005937 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005938 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 }
5940 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005941
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005942 /*
5943 * 7: check if result will be cacheable with a cookie.
5944 * We'll block the response if security checks have caught
5945 * nasty things such as a cacheable cookie.
5946 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005947 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5948 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005949 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005950
5951 /* we're in presence of a cacheable response containing
5952 * a set-cookie header. We'll block it as requested by
5953 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005954 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005955 if (objt_server(t->target))
5956 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005957
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005958 t->be->be_counters.denied_resp++;
5959 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005960 if (t->listener->counters)
5961 t->listener->counters->denied_resp++;
5962
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005963 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005964 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005965 send_log(t->be, LOG_ALERT,
5966 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005967 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005968 goto return_srv_prx_502;
5969 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005970
5971 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005972 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005973 * If an "Upgrade" token is found, the header is left untouched in order
5974 * not to have to deal with some client bugs : some of them fail an upgrade
5975 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005977 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5978 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005979 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5980 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005981 unsigned int want_flags = 0;
5982
5983 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5984 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5985 /* we want a keep-alive response here. Keep-alive header
5986 * required if either side is not 1.1.
5987 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005988 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005989 want_flags |= TX_CON_KAL_SET;
5990 }
5991 else {
5992 /* we want a close response here. Close header required if
5993 * the server is 1.1, regardless of the client.
5994 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005995 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005996 want_flags |= TX_CON_CLO_SET;
5997 }
5998
5999 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006000 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006001 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006003 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006004 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006005 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006006 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006007
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008 /*************************************************************
6009 * OK, that's finished for the headers. We have done what we *
6010 * could. Let's switch to the DATA state. *
6011 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006012
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006013 /* if the user wants to log as soon as possible, without counting
6014 * bytes from the server, then this is the right moment. We have
6015 * to temporarily assign bytes_out to log what we currently have.
6016 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006017 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006018 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6019 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006020 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021 t->logs.bytes_out = 0;
6022 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006024 /* Note: we must not try to cheat by jumping directly to DATA,
6025 * otherwise we would not let the client side wake up.
6026 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006028 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006029 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006030 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006031}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032
Willy Tarreaud98cf932009-12-27 22:54:55 +01006033/* This function is an analyser which forwards response body (including chunk
6034 * sizes if any). It is called as soon as we must forward, even if we forward
6035 * zero byte. The only situation where it must not be called is when we're in
6036 * tunnel mode and we want to forward till the close. It's used both to forward
6037 * remaining data and to resync after end of body. It expects the msg_state to
6038 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6039 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006040 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006041 * bytes of pending data + the headers if not already done (between sol and sov).
6042 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006043 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006044int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045{
6046 struct http_txn *txn = &s->txn;
6047 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006048 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006049 static struct buffer *tmpbuf = NULL;
6050 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006051 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006052 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006053
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006054 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6055 return 0;
6056
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006057 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006058 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006059 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006060 /* Output closed while we were sending data. We must abort and
6061 * wake the other side up.
6062 */
6063 msg->msg_state = HTTP_MSG_ERROR;
6064 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006065 return 1;
6066 }
6067
Willy Tarreau4fe41902010-06-07 22:27:41 +02006068 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006069 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006070
William Lallemand82fe75c2012-10-23 10:25:10 +02006071 /* this is the first time we need the compression buffer */
6072 if (s->comp_algo != NULL && tmpbuf == NULL) {
6073 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6074 goto aborted_xfer; /* no memory */
6075 }
6076
Willy Tarreaud98cf932009-12-27 22:54:55 +01006077 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006078 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006079 * rep->buf.p still points to the beginning of the message and msg->sol
6080 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006081 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006082 channel_forward(res, msg->sov);
6083 msg->next = 0;
6084 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006085
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006086 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006087 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006088 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006089 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090 }
6091
William Lallemand82fe75c2012-10-23 10:25:10 +02006092 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006093 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006094 if (ret < 0) {
6095 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006096 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006097 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006098 compressing = 1;
6099 }
6100
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006102 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006103 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006104 if (s->comp_algo == NULL) {
6105 bytes = msg->sov - msg->sol;
6106 if (msg->chunk_len || bytes) {
6107 msg->sol = msg->sov;
6108 msg->next -= bytes; /* will be forwarded */
6109 msg->chunk_len += bytes;
6110 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6111 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006112 }
6113
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006114 switch (msg->msg_state - HTTP_MSG_DATA) {
6115 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006116 if (compressing) {
6117 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6118 if (ret < 0)
6119 goto aborted_xfer;
6120 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006121
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006122 if (res->to_forward || msg->chunk_len) {
6123 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006124 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006125 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006126
6127 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006128 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006129 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006130 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006131 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006132 if (compressing && consumed_data) {
6133 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6134 compressing = 0;
6135 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006136 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006137 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006138 /* fall through for HTTP_MSG_CHUNK_CRLF */
6139
6140 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6141 /* we want the CRLF after the data */
6142
6143 ret = http_skip_chunk_crlf(msg);
6144 if (ret == 0)
6145 goto missing_data;
6146 else if (ret < 0) {
6147 if (msg->err_pos >= 0)
6148 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6149 goto return_bad_res;
6150 }
6151 /* skipping data in buffer for compression */
6152 if (compressing) {
6153 b_adv(res->buf, msg->next);
6154 msg->next = 0;
6155 msg->sov = 0;
6156 msg->sol = 0;
6157 }
6158 /* we're in MSG_CHUNK_SIZE now, fall through */
6159
6160 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006161 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006162 * set ->sov and ->next to point to the body and switch to DATA or
6163 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006164 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006165
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006166 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006167 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006169 else if (ret < 0) {
6170 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006171 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006172 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006173 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006174 if (compressing) {
6175 if (likely(msg->chunk_len > 0)) {
6176 /* skipping data if we are in compression mode */
6177 b_adv(res->buf, msg->next);
6178 msg->next = 0;
6179 msg->sov = 0;
6180 msg->sol = 0;
6181 } else {
6182 if (consumed_data) {
6183 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6184 compressing = 0;
6185 }
6186 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006187 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006188 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006189 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006190
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006191 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6192 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006193 if (ret == 0)
6194 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006195 else if (ret < 0) {
6196 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006197 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006199 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006200 if (s->comp_algo != NULL) {
6201 /* forwarding trailers */
6202 channel_forward(res, msg->next);
6203 msg->next = 0;
6204 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006205 /* we're in HTTP_MSG_DONE now, but we might still have
6206 * some data pending, so let's loop over once.
6207 */
6208 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006209
6210 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006211 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006212
6213 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006214 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006215 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6216 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006217 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006218 if (http_resync_states(s)) {
6219 http_silent_debug(__LINE__, s);
6220 /* some state changes occurred, maybe the analyser
6221 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006222 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006223 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006224 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006225 /* response errors are most likely due to
6226 * the client aborting the transfer.
6227 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006228 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006229 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006230 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006231 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006232 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006233 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006234 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006235 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006236 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006237 }
6238 }
6239
Willy Tarreaud98cf932009-12-27 22:54:55 +01006240 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006241 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006242 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6243 compressing = 0;
6244 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006245
6246 if (res->flags & CF_SHUTW)
6247 goto aborted_xfer;
6248
6249 /* stop waiting for data if the input is closed before the end. If the
6250 * client side was already closed, it means that the client has aborted,
6251 * so we don't want to count this as a server abort. Otherwise it's a
6252 * server abort.
6253 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006254 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006255 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6256 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006257 if (!(s->flags & SN_ERR_MASK))
6258 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006259 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006260 if (objt_server(s->target))
6261 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006262 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006263 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006264
Willy Tarreau40dba092010-03-04 18:14:51 +01006265 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006266 if (!s->req->analysers)
6267 goto return_bad_res;
6268
Willy Tarreauea953162012-05-18 23:41:28 +02006269 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006270 if (s->comp_algo == NULL) {
6271 bytes = msg->sov - msg->sol;
6272 if (msg->chunk_len || bytes) {
6273 msg->sol = msg->sov;
6274 msg->next -= bytes; /* will be forwarded */
6275 msg->chunk_len += bytes;
6276 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6277 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006278 }
6279
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006280 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006281 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006282 * Similarly, with keep-alive on the client side, we don't want to forward a
6283 * close.
6284 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006285 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006286 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6287 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006288 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006289
Willy Tarreau5c620922011-05-11 19:56:11 +02006290 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006291 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006292 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006293 * modes are already handled by the stream sock layer. We must not do
6294 * this in content-length mode because it could present the MSG_MORE
6295 * flag with the last block of forwarded data, which would cause an
6296 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006297 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006298 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006299 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006300
Willy Tarreaud98cf932009-12-27 22:54:55 +01006301 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006302 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006303 return 0;
6304
Willy Tarreau40dba092010-03-04 18:14:51 +01006305 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006306 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006307 if (objt_server(s->target))
6308 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006309
6310 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006311 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006312 /* don't send any error message as we're in the body */
6313 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006314 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006315 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006316 if (objt_server(s->target))
6317 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006318
6319 if (!(s->flags & SN_ERR_MASK))
6320 s->flags |= SN_ERR_PRXCOND;
6321 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006322 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006323 return 0;
6324
6325 aborted_xfer:
6326 txn->rsp.msg_state = HTTP_MSG_ERROR;
6327 /* don't send any error message as we're in the body */
6328 stream_int_retnclose(res->cons, NULL);
6329 res->analysers = 0;
6330 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6331
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006332 s->fe->fe_counters.cli_aborts++;
6333 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006334 if (objt_server(s->target))
6335 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006336
6337 if (!(s->flags & SN_ERR_MASK))
6338 s->flags |= SN_ERR_CLICL;
6339 if (!(s->flags & SN_FINST_MASK))
6340 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006341 return 0;
6342}
6343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006344/* Iterate the same filter through all request headers.
6345 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346 * Since it can manage the switch to another backend, it updates the per-proxy
6347 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006349int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 char term;
6352 char *cur_ptr, *cur_end, *cur_next;
6353 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006354 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006355 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006356 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006358 last_hdr = 0;
6359
Willy Tarreau9b28e032012-10-12 23:49:43 +02006360 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006361 old_idx = 0;
6362
6363 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006364 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006366 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367 (exp->action == ACT_ALLOW ||
6368 exp->action == ACT_DENY ||
6369 exp->action == ACT_TARPIT))
6370 return 0;
6371
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006372 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006373 if (!cur_idx)
6374 break;
6375
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006376 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 cur_ptr = cur_next;
6378 cur_end = cur_ptr + cur_hdr->len;
6379 cur_next = cur_end + cur_hdr->cr + 1;
6380
6381 /* Now we have one header between cur_ptr and cur_end,
6382 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006383 */
6384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 /* The annoying part is that pattern matching needs
6386 * that we modify the contents to null-terminate all
6387 * strings before testing them.
6388 */
6389
6390 term = *cur_end;
6391 *cur_end = '\0';
6392
6393 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6394 switch (exp->action) {
6395 case ACT_SETBE:
6396 /* It is not possible to jump a second time.
6397 * FIXME: should we return an HTTP/500 here so that
6398 * the admin knows there's a problem ?
6399 */
6400 if (t->be != t->fe)
6401 break;
6402
6403 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006404 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006405 last_hdr = 1;
6406 break;
6407
6408 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 last_hdr = 1;
6411 break;
6412
6413 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006414 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006415 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006416 break;
6417
6418 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006419 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006420 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006421 break;
6422
6423 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006424 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6425 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 /* FIXME: if the user adds a newline in the replacement, the
6427 * index will not be recalculated for now, and the new line
6428 * will not be counted as a new header.
6429 */
6430
6431 cur_end += delta;
6432 cur_next += delta;
6433 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006434 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 break;
6436
6437 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006438 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439 cur_next += delta;
6440
Willy Tarreaufa355d42009-11-29 18:12:29 +01006441 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006442 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6443 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006444 cur_hdr->len = 0;
6445 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006446 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 break;
6448
6449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 if (cur_end)
6452 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 /* keep the link from this header to next one in case of later
6455 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457 old_idx = cur_idx;
6458 }
6459 return 0;
6460}
6461
6462
6463/* Apply the filter to the request line.
6464 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6465 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 * Since it can manage the switch to another backend, it updates the per-proxy
6467 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006469int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470{
6471 char term;
6472 char *cur_ptr, *cur_end;
6473 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006474 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006475 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476
Willy Tarreau3d300592007-03-18 18:34:41 +01006477 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 (exp->action == ACT_ALLOW ||
6481 exp->action == ACT_DENY ||
6482 exp->action == ACT_TARPIT))
6483 return 0;
6484 else if (exp->action == ACT_REMOVE)
6485 return 0;
6486
6487 done = 0;
6488
Willy Tarreau9b28e032012-10-12 23:49:43 +02006489 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006490 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491
6492 /* Now we have the request line between cur_ptr and cur_end */
6493
6494 /* The annoying part is that pattern matching needs
6495 * that we modify the contents to null-terminate all
6496 * strings before testing them.
6497 */
6498
6499 term = *cur_end;
6500 *cur_end = '\0';
6501
6502 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6503 switch (exp->action) {
6504 case ACT_SETBE:
6505 /* It is not possible to jump a second time.
6506 * FIXME: should we return an HTTP/500 here so that
6507 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006508 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 if (t->be != t->fe)
6510 break;
6511
6512 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006513 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 done = 1;
6515 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 done = 1;
6520 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006523 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 done = 1;
6525 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006528 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 done = 1;
6530 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006532 case ACT_REPLACE:
6533 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006534 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6535 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 /* FIXME: if the user adds a newline in the replacement, the
6537 * index will not be recalculated for now, and the new line
6538 * will not be counted as a new header.
6539 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006540
Willy Tarreaufa355d42009-11-29 18:12:29 +01006541 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006543 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 HTTP_MSG_RQMETH,
6545 cur_ptr, cur_end + 1,
6546 NULL, NULL);
6547 if (unlikely(!cur_end))
6548 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 /* we have a full request and we know that we have either a CR
6551 * or an LF at <ptr>.
6552 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006553 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6554 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 /* there is no point trying this regex on headers */
6556 return 1;
6557 }
6558 }
6559 *cur_end = term; /* restore the string terminator */
6560 return done;
6561}
Willy Tarreau97de6242006-12-27 17:18:38 +01006562
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563
Willy Tarreau58f10d72006-12-04 02:26:12 +01006564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006566 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568 * unparsable request. Since it can manage the switch to another backend, it
6569 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006570 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006571int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006573 struct http_txn *txn = &s->txn;
6574 struct hdr_exp *exp;
6575
6576 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 /*
6580 * The interleaving of transformations and verdicts
6581 * makes it difficult to decide to continue or stop
6582 * the evaluation.
6583 */
6584
Willy Tarreau6c123b12010-01-28 20:22:06 +01006585 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6586 break;
6587
Willy Tarreau3d300592007-03-18 18:34:41 +01006588 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006590 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006591 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006592
6593 /* if this filter had a condition, evaluate it now and skip to
6594 * next filter if the condition does not match.
6595 */
6596 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006597 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006598 ret = acl_pass(ret);
6599 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6600 ret = !ret;
6601
6602 if (!ret)
6603 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006604 }
6605
6606 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006607 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 if (unlikely(ret < 0))
6609 return -1;
6610
6611 if (likely(ret == 0)) {
6612 /* The filter did not match the request, it can be
6613 * iterated through all headers.
6614 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006615 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006618 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006619}
6620
6621
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006624 * Try to retrieve the server associated to the appsession.
6625 * If the server is found, it's assigned to the session.
6626 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006627void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006628 struct http_txn *txn = &t->txn;
6629 appsess *asession = NULL;
6630 char *sessid_temp = NULL;
6631
Cyril Bontéb21570a2009-11-29 20:04:48 +01006632 if (len > t->be->appsession_len) {
6633 len = t->be->appsession_len;
6634 }
6635
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006636 if (t->be->options2 & PR_O2_AS_REQL) {
6637 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006638 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006639 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006640 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006641 }
6642
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006643 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006644 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6645 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6646 return;
6647 }
6648
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006649 memcpy(txn->sessid, buf, len);
6650 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006651 }
6652
6653 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6654 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6655 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6656 return;
6657 }
6658
Cyril Bontéb21570a2009-11-29 20:04:48 +01006659 memcpy(sessid_temp, buf, len);
6660 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006661
6662 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6663 /* free previously allocated memory */
6664 pool_free2(apools.sessid, sessid_temp);
6665
6666 if (asession != NULL) {
6667 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6668 if (!(t->be->options2 & PR_O2_AS_REQL))
6669 asession->request_count++;
6670
6671 if (asession->serverid != NULL) {
6672 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006673
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006674 while (srv) {
6675 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006676 if ((srv->state & SRV_RUNNING) ||
6677 (t->be->options & PR_O_PERSIST) ||
6678 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006679 /* we found the server and it's usable */
6680 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006681 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006682 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006683 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006684
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006685 break;
6686 } else {
6687 txn->flags &= ~TX_CK_MASK;
6688 txn->flags |= TX_CK_DOWN;
6689 }
6690 }
6691 srv = srv->next;
6692 }
6693 }
6694 }
6695}
6696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697/* Find the end of a cookie value contained between <s> and <e>. It works the
6698 * same way as with headers above except that the semi-colon also ends a token.
6699 * See RFC2965 for more information. Note that it requires a valid header to
6700 * return a valid result.
6701 */
6702char *find_cookie_value_end(char *s, const char *e)
6703{
6704 int quoted, qdpair;
6705
6706 quoted = qdpair = 0;
6707 for (; s < e; s++) {
6708 if (qdpair) qdpair = 0;
6709 else if (quoted) {
6710 if (*s == '\\') qdpair = 1;
6711 else if (*s == '"') quoted = 0;
6712 }
6713 else if (*s == '"') quoted = 1;
6714 else if (*s == ',' || *s == ';') return s;
6715 }
6716 return s;
6717}
6718
6719/* Delete a value in a header between delimiters <from> and <next> in buffer
6720 * <buf>. The number of characters displaced is returned, and the pointer to
6721 * the first delimiter is updated if required. The function tries as much as
6722 * possible to respect the following principles :
6723 * - replace <from> delimiter by the <next> one unless <from> points to a
6724 * colon, in which case <next> is simply removed
6725 * - set exactly one space character after the new first delimiter, unless
6726 * there are not enough characters in the block being moved to do so.
6727 * - remove unneeded spaces before the previous delimiter and after the new
6728 * one.
6729 *
6730 * It is the caller's responsibility to ensure that :
6731 * - <from> points to a valid delimiter or the colon ;
6732 * - <next> points to a valid delimiter or the final CR/LF ;
6733 * - there are non-space chars before <from> ;
6734 * - there is a CR/LF at or after <next>.
6735 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006736int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737{
6738 char *prev = *from;
6739
6740 if (*prev == ':') {
6741 /* We're removing the first value, preserve the colon and add a
6742 * space if possible.
6743 */
6744 if (!http_is_crlf[(unsigned char)*next])
6745 next++;
6746 prev++;
6747 if (prev < next)
6748 *prev++ = ' ';
6749
6750 while (http_is_spht[(unsigned char)*next])
6751 next++;
6752 } else {
6753 /* Remove useless spaces before the old delimiter. */
6754 while (http_is_spht[(unsigned char)*(prev-1)])
6755 prev--;
6756 *from = prev;
6757
6758 /* copy the delimiter and if possible a space if we're
6759 * not at the end of the line.
6760 */
6761 if (!http_is_crlf[(unsigned char)*next]) {
6762 *prev++ = *next++;
6763 if (prev + 1 < next)
6764 *prev++ = ' ';
6765 while (http_is_spht[(unsigned char)*next])
6766 next++;
6767 }
6768 }
6769 return buffer_replace2(buf, prev, next, NULL, 0);
6770}
6771
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006772/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006773 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 * desirable to call it only when needed. This code is quite complex because
6775 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6776 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006777 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006778void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006780 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006781 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006782 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006783 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6784 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006785
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006786 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006787 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006788 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006790 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006791 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006792 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006793
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006794 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 hdr_beg = hdr_next;
6796 hdr_end = hdr_beg + cur_hdr->len;
6797 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006799 /* We have one full header between hdr_beg and hdr_end, and the
6800 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006801 * "Cookie:" headers.
6802 */
6803
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006805 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806 old_idx = cur_idx;
6807 continue;
6808 }
6809
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006810 del_from = NULL; /* nothing to be deleted */
6811 preserve_hdr = 0; /* assume we may kill the whole header */
6812
Willy Tarreau58f10d72006-12-04 02:26:12 +01006813 /* Now look for cookies. Conforming to RFC2109, we have to support
6814 * attributes whose name begin with a '$', and associate them with
6815 * the right cookie, if we want to delete this cookie.
6816 * So there are 3 cases for each cookie read :
6817 * 1) it's a special attribute, beginning with a '$' : ignore it.
6818 * 2) it's a server id cookie that we *MAY* want to delete : save
6819 * some pointers on it (last semi-colon, beginning of cookie...)
6820 * 3) it's an application cookie : we *MAY* have to delete a previous
6821 * "special" cookie.
6822 * At the end of loop, if a "special" cookie remains, we may have to
6823 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006824 * *MUST* delete it.
6825 *
6826 * Note: RFC2965 is unclear about the processing of spaces around
6827 * the equal sign in the ATTR=VALUE form. A careful inspection of
6828 * the RFC explicitly allows spaces before it, and not within the
6829 * tokens (attrs or values). An inspection of RFC2109 allows that
6830 * too but section 10.1.3 lets one think that spaces may be allowed
6831 * after the equal sign too, resulting in some (rare) buggy
6832 * implementations trying to do that. So let's do what servers do.
6833 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6834 * allowed quoted strings in values, with any possible character
6835 * after a backslash, including control chars and delimitors, which
6836 * causes parsing to become ambiguous. Browsers also allow spaces
6837 * within values even without quotes.
6838 *
6839 * We have to keep multiple pointers in order to support cookie
6840 * removal at the beginning, middle or end of header without
6841 * corrupting the header. All of these headers are valid :
6842 *
6843 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6844 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6845 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6846 * | | | | | | | | |
6847 * | | | | | | | | hdr_end <--+
6848 * | | | | | | | +--> next
6849 * | | | | | | +----> val_end
6850 * | | | | | +-----------> val_beg
6851 * | | | | +--------------> equal
6852 * | | | +----------------> att_end
6853 * | | +---------------------> att_beg
6854 * | +--------------------------> prev
6855 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006856 */
6857
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006858 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6859 /* Iterate through all cookies on this line */
6860
6861 /* find att_beg */
6862 att_beg = prev + 1;
6863 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6864 att_beg++;
6865
6866 /* find att_end : this is the first character after the last non
6867 * space before the equal. It may be equal to hdr_end.
6868 */
6869 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 while (equal < hdr_end) {
6872 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006873 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 if (http_is_spht[(unsigned char)*equal++])
6875 continue;
6876 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877 }
6878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6880 * is between <att_beg> and <equal>, both may be identical.
6881 */
6882
6883 /* look for end of cookie if there is an equal sign */
6884 if (equal < hdr_end && *equal == '=') {
6885 /* look for the beginning of the value */
6886 val_beg = equal + 1;
6887 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6888 val_beg++;
6889
6890 /* find the end of the value, respecting quotes */
6891 next = find_cookie_value_end(val_beg, hdr_end);
6892
6893 /* make val_end point to the first white space or delimitor after the value */
6894 val_end = next;
6895 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6896 val_end--;
6897 } else {
6898 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006899 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 /* We have nothing to do with attributes beginning with '$'. However,
6902 * they will automatically be removed if a header before them is removed,
6903 * since they're supposed to be linked together.
6904 */
6905 if (*att_beg == '$')
6906 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006907
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 /* Ignore cookies with no equal sign */
6909 if (equal == next) {
6910 /* This is not our cookie, so we must preserve it. But if we already
6911 * scheduled another cookie for removal, we cannot remove the
6912 * complete header, but we can remove the previous block itself.
6913 */
6914 preserve_hdr = 1;
6915 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006916 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006917 val_end += delta;
6918 next += delta;
6919 hdr_end += delta;
6920 hdr_next += delta;
6921 cur_hdr->len += delta;
6922 http_msg_move_end(&txn->req, delta);
6923 prev = del_from;
6924 del_from = NULL;
6925 }
6926 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006927 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006928
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006929 /* if there are spaces around the equal sign, we need to
6930 * strip them otherwise we'll get trouble for cookie captures,
6931 * or even for rewrites. Since this happens extremely rarely,
6932 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006933 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6935 int stripped_before = 0;
6936 int stripped_after = 0;
6937
6938 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006939 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 equal += stripped_before;
6941 val_beg += stripped_before;
6942 }
6943
6944 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006945 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 val_beg += stripped_after;
6947 stripped_before += stripped_after;
6948 }
6949
6950 val_end += stripped_before;
6951 next += stripped_before;
6952 hdr_end += stripped_before;
6953 hdr_next += stripped_before;
6954 cur_hdr->len += stripped_before;
6955 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006956 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006958
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006959 /* First, let's see if we want to capture this cookie. We check
6960 * that we don't already have a client side cookie, because we
6961 * can only capture one. Also as an optimisation, we ignore
6962 * cookies shorter than the declared name.
6963 */
6964 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6965 (val_end - att_beg >= t->fe->capture_namelen) &&
6966 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6967 int log_len = val_end - att_beg;
6968
6969 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6970 Alert("HTTP logging : out of memory.\n");
6971 } else {
6972 if (log_len > t->fe->capture_len)
6973 log_len = t->fe->capture_len;
6974 memcpy(txn->cli_cookie, att_beg, log_len);
6975 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006977 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978
Willy Tarreaubca99692010-10-06 19:25:55 +02006979 /* Persistence cookies in passive, rewrite or insert mode have the
6980 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006982 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006984 * For cookies in prefix mode, the form is :
6985 *
6986 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006987 */
6988 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6989 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6990 struct server *srv = t->be->srv;
6991 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6994 * have the server ID between val_beg and delim, and the original cookie between
6995 * delim+1 and val_end. Otherwise, delim==val_end :
6996 *
6997 * Cookie: NAME=SRV; # in all but prefix modes
6998 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6999 * | || || | |+-> next
7000 * | || || | +--> val_end
7001 * | || || +---------> delim
7002 * | || |+------------> val_beg
7003 * | || +-------------> att_end = equal
7004 * | |+-----------------> att_beg
7005 * | +------------------> prev
7006 * +-------------------------> hdr_beg
7007 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007008
Willy Tarreau67402132012-05-31 20:40:20 +02007009 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007010 for (delim = val_beg; delim < val_end; delim++)
7011 if (*delim == COOKIE_DELIM)
7012 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007013 } else {
7014 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007015 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007016 /* Now check if the cookie contains a date field, which would
7017 * appear after a vertical bar ('|') just after the server name
7018 * and before the delimiter.
7019 */
7020 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7021 if (vbar1) {
7022 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007023 * right is the last seen date. It is a base64 encoded
7024 * 30-bit value representing the UNIX date since the
7025 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007026 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007027 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007028 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007029 if (val_end - vbar1 >= 5) {
7030 val = b64tos30(vbar1);
7031 if (val > 0)
7032 txn->cookie_last_date = val << 2;
7033 }
7034 /* look for a second vertical bar */
7035 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7036 if (vbar1 && (val_end - vbar1 > 5)) {
7037 val = b64tos30(vbar1 + 1);
7038 if (val > 0)
7039 txn->cookie_first_date = val << 2;
7040 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007041 }
7042 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043
Willy Tarreauf64d1412010-10-07 20:06:11 +02007044 /* if the cookie has an expiration date and the proxy wants to check
7045 * it, then we do that now. We first check if the cookie is too old,
7046 * then only if it has expired. We detect strict overflow because the
7047 * time resolution here is not great (4 seconds). Cookies with dates
7048 * in the future are ignored if their offset is beyond one day. This
7049 * allows an admin to fix timezone issues without expiring everyone
7050 * and at the same time avoids keeping unwanted side effects for too
7051 * long.
7052 */
7053 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007054 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7055 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007056 txn->flags &= ~TX_CK_MASK;
7057 txn->flags |= TX_CK_OLD;
7058 delim = val_beg; // let's pretend we have not found the cookie
7059 txn->cookie_first_date = 0;
7060 txn->cookie_last_date = 0;
7061 }
7062 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007063 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7064 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007065 txn->flags &= ~TX_CK_MASK;
7066 txn->flags |= TX_CK_EXPIRED;
7067 delim = val_beg; // let's pretend we have not found the cookie
7068 txn->cookie_first_date = 0;
7069 txn->cookie_last_date = 0;
7070 }
7071
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 /* Here, we'll look for the first running server which supports the cookie.
7073 * This allows to share a same cookie between several servers, for example
7074 * to dedicate backup servers to specific servers only.
7075 * However, to prevent clients from sticking to cookie-less backup server
7076 * when they have incidentely learned an empty cookie, we simply ignore
7077 * empty cookies and mark them as invalid.
7078 * The same behaviour is applied when persistence must be ignored.
7079 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007080 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 while (srv) {
7084 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7085 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7086 if ((srv->state & SRV_RUNNING) ||
7087 (t->be->options & PR_O_PERSIST) ||
7088 (t->flags & SN_FORCE_PRST)) {
7089 /* we found the server and we can use it */
7090 txn->flags &= ~TX_CK_MASK;
7091 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7092 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007093 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 break;
7095 } else {
7096 /* we found a server, but it's down,
7097 * mark it as such and go on in case
7098 * another one is available.
7099 */
7100 txn->flags &= ~TX_CK_MASK;
7101 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007104 srv = srv->next;
7105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106
Willy Tarreauf64d1412010-10-07 20:06:11 +02007107 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007108 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007109 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007110 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7111 txn->flags |= TX_CK_UNUSED;
7112 else
7113 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 /* depending on the cookie mode, we may have to either :
7117 * - delete the complete cookie if we're in insert+indirect mode, so that
7118 * the server never sees it ;
7119 * - remove the server id from the cookie value, and tag the cookie as an
7120 * application cookie so that it does not get accidentely removed later,
7121 * if we're in cookie prefix mode
7122 */
Willy Tarreau67402132012-05-31 20:40:20 +02007123 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125
Willy Tarreau9b28e032012-10-12 23:49:43 +02007126 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007127 val_end += delta;
7128 next += delta;
7129 hdr_end += delta;
7130 hdr_next += delta;
7131 cur_hdr->len += delta;
7132 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007133
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007134 del_from = NULL;
7135 preserve_hdr = 1; /* we want to keep this cookie */
7136 }
7137 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007138 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007139 del_from = prev;
7140 }
7141 } else {
7142 /* This is not our cookie, so we must preserve it. But if we already
7143 * scheduled another cookie for removal, we cannot remove the
7144 * complete header, but we can remove the previous block itself.
7145 */
7146 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007148 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007149 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007150 if (att_beg >= del_from)
7151 att_beg += delta;
7152 if (att_end >= del_from)
7153 att_end += delta;
7154 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 val_end += delta;
7156 next += delta;
7157 hdr_end += delta;
7158 hdr_next += delta;
7159 cur_hdr->len += delta;
7160 http_msg_move_end(&txn->req, delta);
7161 prev = del_from;
7162 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007164 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007165
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007166 /* Look for the appsession cookie unless persistence must be ignored */
7167 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7168 int cmp_len, value_len;
7169 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007170
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007171 if (t->be->options2 & PR_O2_AS_PFX) {
7172 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7173 value_begin = att_beg + t->be->appsession_name_len;
7174 value_len = val_end - att_beg - t->be->appsession_name_len;
7175 } else {
7176 cmp_len = att_end - att_beg;
7177 value_begin = val_beg;
7178 value_len = val_end - val_beg;
7179 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007180
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 /* let's see if the cookie is our appcookie */
7182 if (cmp_len == t->be->appsession_name_len &&
7183 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7184 manage_client_side_appsession(t, value_begin, value_len);
7185 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007186 }
7187
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007188 /* continue with next cookie on this header line */
7189 att_beg = next;
7190 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007191
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007192 /* There are no more cookies on this line.
7193 * We may still have one (or several) marked for deletion at the
7194 * end of the line. We must do this now in two ways :
7195 * - if some cookies must be preserved, we only delete from the
7196 * mark to the end of line ;
7197 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007200 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007201 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007202 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007204 cur_hdr->len += delta;
7205 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007206 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007207
7208 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007209 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7210 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007211 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007212 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007215 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216 }
7217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007218 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007221}
7222
7223
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224/* Iterate the same filter through all response headers contained in <rtr>.
7225 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7226 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007227int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228{
7229 char term;
7230 char *cur_ptr, *cur_end, *cur_next;
7231 int cur_idx, old_idx, last_hdr;
7232 struct http_txn *txn = &t->txn;
7233 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007234 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235
7236 last_hdr = 0;
7237
Willy Tarreau9b28e032012-10-12 23:49:43 +02007238 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 old_idx = 0;
7240
7241 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007242 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007244 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 (exp->action == ACT_ALLOW ||
7246 exp->action == ACT_DENY))
7247 return 0;
7248
7249 cur_idx = txn->hdr_idx.v[old_idx].next;
7250 if (!cur_idx)
7251 break;
7252
7253 cur_hdr = &txn->hdr_idx.v[cur_idx];
7254 cur_ptr = cur_next;
7255 cur_end = cur_ptr + cur_hdr->len;
7256 cur_next = cur_end + cur_hdr->cr + 1;
7257
7258 /* Now we have one header between cur_ptr and cur_end,
7259 * and the next header starts at cur_next.
7260 */
7261
7262 /* The annoying part is that pattern matching needs
7263 * that we modify the contents to null-terminate all
7264 * strings before testing them.
7265 */
7266
7267 term = *cur_end;
7268 *cur_end = '\0';
7269
7270 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7271 switch (exp->action) {
7272 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 last_hdr = 1;
7275 break;
7276
7277 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 last_hdr = 1;
7280 break;
7281
7282 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007283 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7284 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 /* FIXME: if the user adds a newline in the replacement, the
7286 * index will not be recalculated for now, and the new line
7287 * will not be counted as a new header.
7288 */
7289
7290 cur_end += delta;
7291 cur_next += delta;
7292 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007293 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 break;
7295
7296 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007297 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 cur_next += delta;
7299
Willy Tarreaufa355d42009-11-29 18:12:29 +01007300 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7302 txn->hdr_idx.used--;
7303 cur_hdr->len = 0;
7304 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007305 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 break;
7307
7308 }
7309 }
7310 if (cur_end)
7311 *cur_end = term; /* restore the string terminator */
7312
7313 /* keep the link from this header to next one in case of later
7314 * removal of next header.
7315 */
7316 old_idx = cur_idx;
7317 }
7318 return 0;
7319}
7320
7321
7322/* Apply the filter to the status line in the response buffer <rtr>.
7323 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7324 * or -1 if a replacement resulted in an invalid status line.
7325 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007326int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327{
7328 char term;
7329 char *cur_ptr, *cur_end;
7330 int done;
7331 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007332 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333
7334
Willy Tarreau3d300592007-03-18 18:34:41 +01007335 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007337 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338 (exp->action == ACT_ALLOW ||
7339 exp->action == ACT_DENY))
7340 return 0;
7341 else if (exp->action == ACT_REMOVE)
7342 return 0;
7343
7344 done = 0;
7345
Willy Tarreau9b28e032012-10-12 23:49:43 +02007346 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007347 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348
7349 /* Now we have the status line between cur_ptr and cur_end */
7350
7351 /* The annoying part is that pattern matching needs
7352 * that we modify the contents to null-terminate all
7353 * strings before testing them.
7354 */
7355
7356 term = *cur_end;
7357 *cur_end = '\0';
7358
7359 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7360 switch (exp->action) {
7361 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007362 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363 done = 1;
7364 break;
7365
7366 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 done = 1;
7369 break;
7370
7371 case ACT_REPLACE:
7372 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007373 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7374 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 /* FIXME: if the user adds a newline in the replacement, the
7376 * index will not be recalculated for now, and the new line
7377 * will not be counted as a new header.
7378 */
7379
Willy Tarreaufa355d42009-11-29 18:12:29 +01007380 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007381 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007382 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007383 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 cur_ptr, cur_end + 1,
7385 NULL, NULL);
7386 if (unlikely(!cur_end))
7387 return -1;
7388
7389 /* we have a full respnse and we know that we have either a CR
7390 * or an LF at <ptr>.
7391 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007392 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007393 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394 /* there is no point trying this regex on headers */
7395 return 1;
7396 }
7397 }
7398 *cur_end = term; /* restore the string terminator */
7399 return done;
7400}
7401
7402
7403
7404/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007405 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7407 * unparsable response.
7408 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007409int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007410{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007411 struct http_txn *txn = &s->txn;
7412 struct hdr_exp *exp;
7413
7414 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415 int ret;
7416
7417 /*
7418 * The interleaving of transformations and verdicts
7419 * makes it difficult to decide to continue or stop
7420 * the evaluation.
7421 */
7422
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007423 if (txn->flags & TX_SVDENY)
7424 break;
7425
Willy Tarreau3d300592007-03-18 18:34:41 +01007426 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7428 exp->action == ACT_PASS)) {
7429 exp = exp->next;
7430 continue;
7431 }
7432
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007433 /* if this filter had a condition, evaluate it now and skip to
7434 * next filter if the condition does not match.
7435 */
7436 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007437 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007438 ret = acl_pass(ret);
7439 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7440 ret = !ret;
7441 if (!ret)
7442 continue;
7443 }
7444
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007446 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 if (unlikely(ret < 0))
7448 return -1;
7449
7450 if (likely(ret == 0)) {
7451 /* The filter did not match the response, it can be
7452 * iterated through all headers.
7453 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007454 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 }
7457 return 0;
7458}
7459
7460
Willy Tarreaua15645d2007-03-18 16:22:39 +01007461/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007462 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 * desirable to call it only when needed. This function is also used when we
7464 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007466void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467{
7468 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007469 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007471 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 char *hdr_beg, *hdr_end, *hdr_next;
7473 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007474
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475 /* Iterate through the headers.
7476 * we start with the start line.
7477 */
7478 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007479 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480
7481 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7482 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007483 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484
7485 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007486 hdr_beg = hdr_next;
7487 hdr_end = hdr_beg + cur_hdr->len;
7488 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489
Willy Tarreau24581ba2010-08-31 22:39:35 +02007490 /* We have one full header between hdr_beg and hdr_end, and the
7491 * next header starts at hdr_next. We're only interested in
7492 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 */
7494
Willy Tarreau24581ba2010-08-31 22:39:35 +02007495 is_cookie2 = 0;
7496 prev = hdr_beg + 10;
7497 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007498 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7500 if (!val) {
7501 old_idx = cur_idx;
7502 continue;
7503 }
7504 is_cookie2 = 1;
7505 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 }
7507
Willy Tarreau24581ba2010-08-31 22:39:35 +02007508 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7509 * <prev> points to the colon.
7510 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007511 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7514 * check-cache is enabled) and we are not interested in checking
7515 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007516 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007517 if (t->be->cookie_name == NULL &&
7518 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007519 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520 return;
7521
Willy Tarreau24581ba2010-08-31 22:39:35 +02007522 /* OK so now we know we have to process this response cookie.
7523 * The format of the Set-Cookie header is slightly different
7524 * from the format of the Cookie header in that it does not
7525 * support the comma as a cookie delimiter (thus the header
7526 * cannot be folded) because the Expires attribute described in
7527 * the original Netscape's spec may contain an unquoted date
7528 * with a comma inside. We have to live with this because
7529 * many browsers don't support Max-Age and some browsers don't
7530 * support quoted strings. However the Set-Cookie2 header is
7531 * clean.
7532 *
7533 * We have to keep multiple pointers in order to support cookie
7534 * removal at the beginning, middle or end of header without
7535 * corrupting the header (in case of set-cookie2). A special
7536 * pointer, <scav> points to the beginning of the set-cookie-av
7537 * fields after the first semi-colon. The <next> pointer points
7538 * either to the end of line (set-cookie) or next unquoted comma
7539 * (set-cookie2). All of these headers are valid :
7540 *
7541 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7542 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7543 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7544 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7545 * | | | | | | | | | |
7546 * | | | | | | | | +-> next hdr_end <--+
7547 * | | | | | | | +------------> scav
7548 * | | | | | | +--------------> val_end
7549 * | | | | | +--------------------> val_beg
7550 * | | | | +----------------------> equal
7551 * | | | +------------------------> att_end
7552 * | | +----------------------------> att_beg
7553 * | +------------------------------> prev
7554 * +-----------------------------------------> hdr_beg
7555 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 for (; prev < hdr_end; prev = next) {
7558 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 /* find att_beg */
7561 att_beg = prev + 1;
7562 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7563 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564
Willy Tarreau24581ba2010-08-31 22:39:35 +02007565 /* find att_end : this is the first character after the last non
7566 * space before the equal. It may be equal to hdr_end.
7567 */
7568 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007569
Willy Tarreau24581ba2010-08-31 22:39:35 +02007570 while (equal < hdr_end) {
7571 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7572 break;
7573 if (http_is_spht[(unsigned char)*equal++])
7574 continue;
7575 att_end = equal;
7576 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7579 * is between <att_beg> and <equal>, both may be identical.
7580 */
7581
7582 /* look for end of cookie if there is an equal sign */
7583 if (equal < hdr_end && *equal == '=') {
7584 /* look for the beginning of the value */
7585 val_beg = equal + 1;
7586 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7587 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 /* find the end of the value, respecting quotes */
7590 next = find_cookie_value_end(val_beg, hdr_end);
7591
7592 /* make val_end point to the first white space or delimitor after the value */
7593 val_end = next;
7594 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7595 val_end--;
7596 } else {
7597 /* <equal> points to next comma, semi-colon or EOL */
7598 val_beg = val_end = next = equal;
7599 }
7600
7601 if (next < hdr_end) {
7602 /* Set-Cookie2 supports multiple cookies, and <next> points to
7603 * a colon or semi-colon before the end. So skip all attr-value
7604 * pairs and look for the next comma. For Set-Cookie, since
7605 * commas are permitted in values, skip to the end.
7606 */
7607 if (is_cookie2)
7608 next = find_hdr_value_end(next, hdr_end);
7609 else
7610 next = hdr_end;
7611 }
7612
7613 /* Now everything is as on the diagram above */
7614
7615 /* Ignore cookies with no equal sign */
7616 if (equal == val_end)
7617 continue;
7618
7619 /* If there are spaces around the equal sign, we need to
7620 * strip them otherwise we'll get trouble for cookie captures,
7621 * or even for rewrites. Since this happens extremely rarely,
7622 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007624 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7625 int stripped_before = 0;
7626 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007629 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007630 equal += stripped_before;
7631 val_beg += stripped_before;
7632 }
7633
7634 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007635 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007636 val_beg += stripped_after;
7637 stripped_before += stripped_after;
7638 }
7639
7640 val_end += stripped_before;
7641 next += stripped_before;
7642 hdr_end += stripped_before;
7643 hdr_next += stripped_before;
7644 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007645 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007646 }
7647
7648 /* First, let's see if we want to capture this cookie. We check
7649 * that we don't already have a server side cookie, because we
7650 * can only capture one. Also as an optimisation, we ignore
7651 * cookies shorter than the declared name.
7652 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007653 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007654 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007655 (val_end - att_beg >= t->fe->capture_namelen) &&
7656 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7657 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007658 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 Alert("HTTP logging : out of memory.\n");
7660 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007661 else {
7662 if (log_len > t->fe->capture_len)
7663 log_len = t->fe->capture_len;
7664 memcpy(txn->srv_cookie, att_beg, log_len);
7665 txn->srv_cookie[log_len] = 0;
7666 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667 }
7668
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007669 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007671 if (!(t->flags & SN_IGNORE_PRST) &&
7672 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7673 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007674 /* assume passive cookie by default */
7675 txn->flags &= ~TX_SCK_MASK;
7676 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677
7678 /* If the cookie is in insert mode on a known server, we'll delete
7679 * this occurrence because we'll insert another one later.
7680 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007681 * a direct access.
7682 */
Willy Tarreau67402132012-05-31 20:40:20 +02007683 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007684 /* The "preserve" flag was set, we don't want to touch the
7685 * server's cookie.
7686 */
7687 }
Willy Tarreau67402132012-05-31 20:40:20 +02007688 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7689 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007690 /* this cookie must be deleted */
7691 if (*prev == ':' && next == hdr_end) {
7692 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007693 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007694 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7695 txn->hdr_idx.used--;
7696 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007697 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007698 hdr_next += delta;
7699 http_msg_move_end(&txn->rsp, delta);
7700 /* note: while both invalid now, <next> and <hdr_end>
7701 * are still equal, so the for() will stop as expected.
7702 */
7703 } else {
7704 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007705 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007706 next = prev;
7707 hdr_end += delta;
7708 hdr_next += delta;
7709 cur_hdr->len += delta;
7710 http_msg_move_end(&txn->rsp, delta);
7711 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007712 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007713 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007714 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007715 }
Willy Tarreau67402132012-05-31 20:40:20 +02007716 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007717 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718 * with this server since we know it.
7719 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007720 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007721 next += delta;
7722 hdr_end += delta;
7723 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007724 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007725 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007726
Willy Tarreauf1348312010-10-07 15:54:11 +02007727 txn->flags &= ~TX_SCK_MASK;
7728 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007729 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007730 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007732 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007734 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007735 next += delta;
7736 hdr_end += delta;
7737 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007739 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740
Willy Tarreau827aee92011-03-10 16:55:02 +01007741 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007742 txn->flags &= ~TX_SCK_MASK;
7743 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007744 }
7745 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007746 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7747 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007748 int cmp_len, value_len;
7749 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750
Cyril Bontéb21570a2009-11-29 20:04:48 +01007751 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007752 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7753 value_begin = att_beg + t->be->appsession_name_len;
7754 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007755 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007756 cmp_len = att_end - att_beg;
7757 value_begin = val_beg;
7758 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007759 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007760
Cyril Bonté17530c32010-04-06 21:11:10 +02007761 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007762 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7763 /* free a possibly previously allocated memory */
7764 pool_free2(apools.sessid, txn->sessid);
7765
Cyril Bontéb21570a2009-11-29 20:04:48 +01007766 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007767 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007768 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7769 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7770 return;
7771 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007772 memcpy(txn->sessid, value_begin, value_len);
7773 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007774 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007775 }
7776 /* that's done for this cookie, check the next one on the same
7777 * line when next != hdr_end (only if is_cookie2).
7778 */
7779 }
7780 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007782 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007783
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007784 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007785 appsess *asession = NULL;
7786 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007787 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007788 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007789 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007790 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7791 Alert("Not enough Memory process_srv():asession:calloc().\n");
7792 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7793 return;
7794 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007795 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7796
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007797 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7798 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7799 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007800 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007801 return;
7802 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007803 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007804 asession->sessid[t->be->appsession_len] = 0;
7805
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007806 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007807 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007808 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007809 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007810 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007811 return;
7812 }
7813 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007814 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007815
7816 asession->request_count = 0;
7817 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7818 }
7819
7820 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7821 asession->request_count++;
7822 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823}
7824
7825
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826/*
7827 * Check if response is cacheable or not. Updates t->flags.
7828 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007829void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830{
7831 struct http_txn *txn = &t->txn;
7832 char *p1, *p2;
7833
7834 char *cur_ptr, *cur_end, *cur_next;
7835 int cur_idx;
7836
Willy Tarreau5df51872007-11-25 16:20:08 +01007837 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838 return;
7839
7840 /* Iterate through the headers.
7841 * we start with the start line.
7842 */
7843 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845
7846 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7847 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007848 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849
7850 cur_hdr = &txn->hdr_idx.v[cur_idx];
7851 cur_ptr = cur_next;
7852 cur_end = cur_ptr + cur_hdr->len;
7853 cur_next = cur_end + cur_hdr->cr + 1;
7854
7855 /* We have one full header between cur_ptr and cur_end, and the
7856 * next header starts at cur_next. We're only interested in
7857 * "Cookie:" headers.
7858 */
7859
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007860 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7861 if (val) {
7862 if ((cur_end - (cur_ptr + val) >= 8) &&
7863 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7864 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7865 return;
7866 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 }
7868
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007869 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7870 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 continue;
7872
7873 /* OK, right now we know we have a cache-control header at cur_ptr */
7874
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007875 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876
7877 if (p1 >= cur_end) /* no more info */
7878 continue;
7879
7880 /* p1 is at the beginning of the value */
7881 p2 = p1;
7882
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007883 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 p2++;
7885
7886 /* we have a complete value between p1 and p2 */
7887 if (p2 < cur_end && *p2 == '=') {
7888 /* we have something of the form no-cache="set-cookie" */
7889 if ((cur_end - p1 >= 21) &&
7890 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7891 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007892 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 continue;
7894 }
7895
7896 /* OK, so we know that either p2 points to the end of string or to a comma */
7897 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007898 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7900 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7901 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007902 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 return;
7904 }
7905
7906 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007907 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908 continue;
7909 }
7910 }
7911}
7912
7913
Willy Tarreau58f10d72006-12-04 02:26:12 +01007914/*
7915 * Try to retrieve a known appsession in the URI, then the associated server.
7916 * If the server is found, it's assigned to the session.
7917 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007918void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007920 char *end_params, *first_param, *cur_param, *next_param;
7921 char separator;
7922 int value_len;
7923
7924 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007926 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007927 (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 +01007928 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007929 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930
Cyril Bontéb21570a2009-11-29 20:04:48 +01007931 first_param = NULL;
7932 switch (mode) {
7933 case PR_O2_AS_M_PP:
7934 first_param = memchr(begin, ';', len);
7935 break;
7936 case PR_O2_AS_M_QS:
7937 first_param = memchr(begin, '?', len);
7938 break;
7939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940
Cyril Bontéb21570a2009-11-29 20:04:48 +01007941 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007943 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944
Cyril Bontéb21570a2009-11-29 20:04:48 +01007945 switch (mode) {
7946 case PR_O2_AS_M_PP:
7947 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7948 end_params = (char *) begin + len;
7949 }
7950 separator = ';';
7951 break;
7952 case PR_O2_AS_M_QS:
7953 end_params = (char *) begin + len;
7954 separator = '&';
7955 break;
7956 default:
7957 /* unknown mode, shouldn't happen */
7958 return;
7959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960
Cyril Bontéb21570a2009-11-29 20:04:48 +01007961 cur_param = next_param = end_params;
7962 while (cur_param > first_param) {
7963 cur_param--;
7964 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7965 /* let's see if this is the appsession parameter */
7966 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7967 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7968 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7969 /* Cool... it's the right one */
7970 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7971 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7972 if (value_len > 0) {
7973 manage_client_side_appsession(t, cur_param, value_len);
7974 }
7975 break;
7976 }
7977 next_param = cur_param;
7978 }
7979 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007980#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007981 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007982 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007983#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984}
7985
Willy Tarreaub2513902006-12-17 14:52:38 +01007986/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007987 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007988 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007989 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007990 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007991 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007992 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007993 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007994 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007995int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007996{
7997 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007998 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008000
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008001 if (!uri_auth)
8002 return 0;
8003
Cyril Bonté70be45d2010-10-12 00:14:35 +02008004 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008005 return 0;
8006
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008007 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008008 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008009 return 0;
8010
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008011 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008012 return 0;
8013
Willy Tarreaub2513902006-12-17 14:52:38 +01008014 return 1;
8015}
8016
Willy Tarreau4076a152009-04-02 15:18:36 +02008017/*
8018 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008019 * By default it tries to report the error position as msg->err_pos. However if
8020 * this one is not set, it will then report msg->next, which is the last known
8021 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008022 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008023 */
8024void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008025 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008026 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008027{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008028 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008029 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008030
Willy Tarreau9b28e032012-10-12 23:49:43 +02008031 es->len = MIN(chn->buf->i, sizeof(es->buf));
8032 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008033 len1 = MIN(len1, es->len);
8034 len2 = es->len - len1; /* remaining data if buffer wraps */
8035
Willy Tarreau9b28e032012-10-12 23:49:43 +02008036 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008037 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008038 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008039
Willy Tarreau4076a152009-04-02 15:18:36 +02008040 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008041 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008042 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008043 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008044
Willy Tarreau4076a152009-04-02 15:18:36 +02008045 es->when = date; // user-visible date
8046 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008047 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008048 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008049 if (objt_conn(s->req->prod->end))
8050 es->src = __objt_conn(s->req->prod->end)->addr.from;
8051 else
8052 memset(&es->src, 0, sizeof(es->src));
8053
Willy Tarreau078272e2010-12-12 12:46:33 +01008054 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008055 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008056 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008057 es->s_flags = s->flags;
8058 es->t_flags = s->txn.flags;
8059 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008060 es->b_out = chn->buf->o;
8061 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008062 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008063 es->m_clen = msg->chunk_len;
8064 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008065}
Willy Tarreaub2513902006-12-17 14:52:38 +01008066
Willy Tarreau294c4732011-12-16 21:35:50 +01008067/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8068 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8069 * performed over the whole headers. Otherwise it must contain a valid header
8070 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8071 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8072 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8073 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008074 * -1. The value fetch stops at commas, so this function is suited for use with
8075 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008076 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008077 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008078unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008079 struct hdr_idx *idx, int occ,
8080 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008081{
Willy Tarreau294c4732011-12-16 21:35:50 +01008082 struct hdr_ctx local_ctx;
8083 char *ptr_hist[MAX_HDR_HISTORY];
8084 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008085 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008086 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008087
Willy Tarreau294c4732011-12-16 21:35:50 +01008088 if (!ctx) {
8089 local_ctx.idx = 0;
8090 ctx = &local_ctx;
8091 }
8092
Willy Tarreaubce70882009-09-07 11:51:47 +02008093 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008094 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008095 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008096 occ--;
8097 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008098 *vptr = ctx->line + ctx->val;
8099 *vlen = ctx->vlen;
8100 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008101 }
8102 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008103 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008104 }
8105
8106 /* negative occurrence, we scan all the list then walk back */
8107 if (-occ > MAX_HDR_HISTORY)
8108 return 0;
8109
Willy Tarreau294c4732011-12-16 21:35:50 +01008110 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008111 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008112 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8113 len_hist[hist_ptr] = ctx->vlen;
8114 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008115 hist_ptr = 0;
8116 found++;
8117 }
8118 if (-occ > found)
8119 return 0;
8120 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008121 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8122 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8123 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008124 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008125 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008126 if (hist_ptr >= MAX_HDR_HISTORY)
8127 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008128 *vptr = ptr_hist[hist_ptr];
8129 *vlen = len_hist[hist_ptr];
8130 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008131}
8132
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008133/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8134 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8135 * performed over the whole headers. Otherwise it must contain a valid header
8136 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8137 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8138 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8139 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8140 * -1. This function differs from http_get_hdr() in that it only returns full
8141 * line header values and does not stop at commas.
8142 * The return value is 0 if nothing was found, or non-zero otherwise.
8143 */
8144unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8145 struct hdr_idx *idx, int occ,
8146 struct hdr_ctx *ctx, char **vptr, int *vlen)
8147{
8148 struct hdr_ctx local_ctx;
8149 char *ptr_hist[MAX_HDR_HISTORY];
8150 int len_hist[MAX_HDR_HISTORY];
8151 unsigned int hist_ptr;
8152 int found;
8153
8154 if (!ctx) {
8155 local_ctx.idx = 0;
8156 ctx = &local_ctx;
8157 }
8158
8159 if (occ >= 0) {
8160 /* search from the beginning */
8161 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8162 occ--;
8163 if (occ <= 0) {
8164 *vptr = ctx->line + ctx->val;
8165 *vlen = ctx->vlen;
8166 return 1;
8167 }
8168 }
8169 return 0;
8170 }
8171
8172 /* negative occurrence, we scan all the list then walk back */
8173 if (-occ > MAX_HDR_HISTORY)
8174 return 0;
8175
8176 found = hist_ptr = 0;
8177 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8178 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8179 len_hist[hist_ptr] = ctx->vlen;
8180 if (++hist_ptr >= MAX_HDR_HISTORY)
8181 hist_ptr = 0;
8182 found++;
8183 }
8184 if (-occ > found)
8185 return 0;
8186 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8187 * find occurrence -occ, so we have to check [hist_ptr+occ].
8188 */
8189 hist_ptr += occ;
8190 if (hist_ptr >= MAX_HDR_HISTORY)
8191 hist_ptr -= MAX_HDR_HISTORY;
8192 *vptr = ptr_hist[hist_ptr];
8193 *vlen = len_hist[hist_ptr];
8194 return 1;
8195}
8196
Willy Tarreaubaaee002006-06-26 02:48:02 +02008197/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008198 * Print a debug line with a header. Always stop at the first CR or LF char,
8199 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8200 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008201 */
8202void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8203{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008204 int max;
8205 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008206 dir,
8207 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8208 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 +02008209
8210 for (max = 0; start + max < end; max++)
8211 if (start[max] == '\r' || start[max] == '\n')
8212 break;
8213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008214 UBOUND(max, trash.size - trash.len - 3);
8215 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8216 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008217 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008218}
8219
Willy Tarreau0937bc42009-12-22 15:03:09 +01008220/*
8221 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8222 * the required fields are properly allocated and that we only need to (re)init
8223 * them. This should be used before processing any new request.
8224 */
8225void http_init_txn(struct session *s)
8226{
8227 struct http_txn *txn = &s->txn;
8228 struct proxy *fe = s->fe;
8229
8230 txn->flags = 0;
8231 txn->status = -1;
8232
Willy Tarreauf64d1412010-10-07 20:06:11 +02008233 txn->cookie_first_date = 0;
8234 txn->cookie_last_date = 0;
8235
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008236 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008237 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008238 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008239 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008240 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008241 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008242 txn->req.chunk_len = 0LL;
8243 txn->req.body_len = 0LL;
8244 txn->rsp.chunk_len = 0LL;
8245 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008246 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8247 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008248 txn->req.chn = s->req;
8249 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008250
8251 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008252
8253 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8254 if (fe->options2 & PR_O2_REQBUG_OK)
8255 txn->req.err_pos = -1; /* let buggy requests pass */
8256
Willy Tarreau46023632010-01-07 22:51:47 +01008257 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008258 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8259
Willy Tarreau46023632010-01-07 22:51:47 +01008260 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008261 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8262
8263 if (txn->hdr_idx.v)
8264 hdr_idx_init(&txn->hdr_idx);
8265}
8266
8267/* to be used at the end of a transaction */
8268void http_end_txn(struct session *s)
8269{
8270 struct http_txn *txn = &s->txn;
8271
Willy Tarreau75195602014-03-11 15:48:55 +01008272 /* release any possible compression context */
8273 if (s->flags & SN_COMP_READY)
8274 s->comp_algo->end(&s->comp_ctx);
8275 s->comp_algo = NULL;
8276 s->flags &= ~SN_COMP_READY;
8277
Willy Tarreau0937bc42009-12-22 15:03:09 +01008278 /* these ones will have been dynamically allocated */
8279 pool_free2(pool2_requri, txn->uri);
8280 pool_free2(pool2_capture, txn->cli_cookie);
8281 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008282 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008283 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008284
William Lallemanda73203e2012-03-12 12:48:57 +01008285 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008286 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008287 txn->uri = NULL;
8288 txn->srv_cookie = NULL;
8289 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008290
8291 if (txn->req.cap) {
8292 struct cap_hdr *h;
8293 for (h = s->fe->req_cap; h; h = h->next)
8294 pool_free2(h->pool, txn->req.cap[h->index]);
8295 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8296 }
8297
8298 if (txn->rsp.cap) {
8299 struct cap_hdr *h;
8300 for (h = s->fe->rsp_cap; h; h = h->next)
8301 pool_free2(h->pool, txn->rsp.cap[h->index]);
8302 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8303 }
8304
Willy Tarreau0937bc42009-12-22 15:03:09 +01008305}
8306
8307/* to be used at the end of a transaction to prepare a new one */
8308void http_reset_txn(struct session *s)
8309{
8310 http_end_txn(s);
8311 http_init_txn(s);
8312
8313 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008314 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008315 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008316 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008317 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008318 /* re-init store persistence */
8319 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008320 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008321
Willy Tarreau0937bc42009-12-22 15:03:09 +01008322 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008323
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008324 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008325
Willy Tarreau739cfba2010-01-25 23:11:14 +01008326 /* We must trim any excess data from the response buffer, because we
8327 * may have blocked an invalid response from a server that we don't
8328 * want to accidentely forward once we disable the analysers, nor do
8329 * we want those data to come along with next response. A typical
8330 * example of such data would be from a buggy server responding to
8331 * a HEAD with some data, or sending more than the advertised
8332 * content-length.
8333 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008334 if (unlikely(s->rep->buf->i))
8335 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008336
Willy Tarreau0937bc42009-12-22 15:03:09 +01008337 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008338 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008339
Willy Tarreaud04e8582010-05-31 12:31:35 +02008340 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008341 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008342
8343 s->req->rex = TICK_ETERNITY;
8344 s->req->wex = TICK_ETERNITY;
8345 s->req->analyse_exp = TICK_ETERNITY;
8346 s->rep->rex = TICK_ETERNITY;
8347 s->rep->wex = TICK_ETERNITY;
8348 s->rep->analyse_exp = TICK_ETERNITY;
8349}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008350
Willy Tarreauff011f22011-01-06 17:51:27 +01008351void free_http_req_rules(struct list *r) {
8352 struct http_req_rule *tr, *pr;
8353
8354 list_for_each_entry_safe(pr, tr, r, list) {
8355 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008356 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008357 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008358
8359 free(pr);
8360 }
8361}
8362
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008363/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008364struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8365{
8366 struct http_req_rule *rule;
8367 int cur_arg;
8368
8369 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8370 if (!rule) {
8371 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008372 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008373 }
8374
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008375 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008376 rule->action = HTTP_REQ_ACT_ALLOW;
8377 cur_arg = 1;
8378 } else if (!strcmp(args[0], "deny")) {
8379 rule->action = HTTP_REQ_ACT_DENY;
8380 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008381 } else if (!strcmp(args[0], "tarpit")) {
8382 rule->action = HTTP_REQ_ACT_TARPIT;
8383 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008384 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008385 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008386 cur_arg = 1;
8387
8388 while(*args[cur_arg]) {
8389 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008390 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008391 cur_arg+=2;
8392 continue;
8393 } else
8394 break;
8395 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008396 } else if (!strcmp(args[0], "set-nice")) {
8397 rule->action = HTTP_REQ_ACT_SET_NICE;
8398 cur_arg = 1;
8399
8400 if (!*args[cur_arg] ||
8401 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8402 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8403 file, linenum, args[0]);
8404 goto out_err;
8405 }
8406 rule->arg.nice = atoi(args[cur_arg]);
8407 if (rule->arg.nice < -1024)
8408 rule->arg.nice = -1024;
8409 else if (rule->arg.nice > 1024)
8410 rule->arg.nice = 1024;
8411 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008412 } else if (!strcmp(args[0], "set-tos")) {
8413#ifdef IP_TOS
8414 char *err;
8415 rule->action = HTTP_REQ_ACT_SET_TOS;
8416 cur_arg = 1;
8417
8418 if (!*args[cur_arg] ||
8419 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8420 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8421 file, linenum, args[0]);
8422 goto out_err;
8423 }
8424
8425 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8426 if (err && *err != '\0') {
8427 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8428 file, linenum, err, args[0]);
8429 goto out_err;
8430 }
8431 cur_arg++;
8432#else
8433 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8434 goto out_err;
8435#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008436 } else if (!strcmp(args[0], "set-mark")) {
8437#ifdef SO_MARK
8438 char *err;
8439 rule->action = HTTP_REQ_ACT_SET_MARK;
8440 cur_arg = 1;
8441
8442 if (!*args[cur_arg] ||
8443 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8444 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8445 file, linenum, args[0]);
8446 goto out_err;
8447 }
8448
8449 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8450 if (err && *err != '\0') {
8451 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8452 file, linenum, err, args[0]);
8453 goto out_err;
8454 }
8455 cur_arg++;
8456 global.last_checks |= LSTCHK_NETADM;
8457#else
8458 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8459 goto out_err;
8460#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008461 } else if (!strcmp(args[0], "set-log-level")) {
8462 rule->action = HTTP_REQ_ACT_SET_LOGL;
8463 cur_arg = 1;
8464
8465 if (!*args[cur_arg] ||
8466 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8467 bad_log_level:
8468 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8469 file, linenum, args[0]);
8470 goto out_err;
8471 }
8472 if (strcmp(args[cur_arg], "silent") == 0)
8473 rule->arg.loglevel = -1;
8474 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8475 goto bad_log_level;
8476 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008477 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8478 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8479 cur_arg = 1;
8480
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008481 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8482 (*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 +01008483 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8484 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008485 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008486 }
8487
8488 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8489 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8490 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008491
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008492 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008493 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Willy Tarreau434c57c2013-01-08 01:10:24 +01008494 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008495 free(proxy->conf.lfs_file);
8496 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8497 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008498 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008499 } else if (strcmp(args[0], "redirect") == 0) {
8500 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008501 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008502
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008503 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008504 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8505 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8506 goto out_err;
8507 }
8508
8509 /* this redirect rule might already contain a parsed condition which
8510 * we'll pass to the http-request rule.
8511 */
8512 rule->action = HTTP_REQ_ACT_REDIR;
8513 rule->arg.redir = redir;
8514 rule->cond = redir->cond;
8515 redir->cond = NULL;
8516 cur_arg = 2;
8517 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008518 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008519 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 +01008520 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008521 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008522 }
8523
8524 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8525 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008526 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008527
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008528 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8529 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8530 file, linenum, args[0], errmsg);
8531 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008532 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008533 }
8534 rule->cond = cond;
8535 }
8536 else if (*args[cur_arg]) {
8537 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8538 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8539 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008540 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008541 }
8542
8543 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008544 out_err:
8545 free(rule);
8546 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008547}
8548
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008549/* parse an "http-respose" rule */
8550struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8551{
8552 struct http_res_rule *rule;
8553 int cur_arg;
8554
8555 rule = calloc(1, sizeof(*rule));
8556 if (!rule) {
8557 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8558 goto out_err;
8559 }
8560
8561 if (!strcmp(args[0], "allow")) {
8562 rule->action = HTTP_RES_ACT_ALLOW;
8563 cur_arg = 1;
8564 } else if (!strcmp(args[0], "deny")) {
8565 rule->action = HTTP_RES_ACT_DENY;
8566 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008567 } else if (!strcmp(args[0], "set-nice")) {
8568 rule->action = HTTP_RES_ACT_SET_NICE;
8569 cur_arg = 1;
8570
8571 if (!*args[cur_arg] ||
8572 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8573 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8574 file, linenum, args[0]);
8575 goto out_err;
8576 }
8577 rule->arg.nice = atoi(args[cur_arg]);
8578 if (rule->arg.nice < -1024)
8579 rule->arg.nice = -1024;
8580 else if (rule->arg.nice > 1024)
8581 rule->arg.nice = 1024;
8582 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008583 } else if (!strcmp(args[0], "set-tos")) {
8584#ifdef IP_TOS
8585 char *err;
8586 rule->action = HTTP_RES_ACT_SET_TOS;
8587 cur_arg = 1;
8588
8589 if (!*args[cur_arg] ||
8590 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8591 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8592 file, linenum, args[0]);
8593 goto out_err;
8594 }
8595
8596 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8597 if (err && *err != '\0') {
8598 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8599 file, linenum, err, args[0]);
8600 goto out_err;
8601 }
8602 cur_arg++;
8603#else
8604 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8605 goto out_err;
8606#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008607 } else if (!strcmp(args[0], "set-mark")) {
8608#ifdef SO_MARK
8609 char *err;
8610 rule->action = HTTP_RES_ACT_SET_MARK;
8611 cur_arg = 1;
8612
8613 if (!*args[cur_arg] ||
8614 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8615 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8616 file, linenum, args[0]);
8617 goto out_err;
8618 }
8619
8620 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8621 if (err && *err != '\0') {
8622 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8623 file, linenum, err, args[0]);
8624 goto out_err;
8625 }
8626 cur_arg++;
8627 global.last_checks |= LSTCHK_NETADM;
8628#else
8629 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8630 goto out_err;
8631#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008632 } else if (!strcmp(args[0], "set-log-level")) {
8633 rule->action = HTTP_RES_ACT_SET_LOGL;
8634 cur_arg = 1;
8635
8636 if (!*args[cur_arg] ||
8637 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8638 bad_log_level:
8639 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8640 file, linenum, args[0]);
8641 goto out_err;
8642 }
8643 if (strcmp(args[cur_arg], "silent") == 0)
8644 rule->arg.loglevel = -1;
8645 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8646 goto bad_log_level;
8647 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008648 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8649 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8650 cur_arg = 1;
8651
8652 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8653 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8654 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8655 file, linenum, args[0]);
8656 goto out_err;
8657 }
8658
8659 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8660 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8661 LIST_INIT(&rule->arg.hdr_add.fmt);
8662
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008663 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008664 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008665 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008666 free(proxy->conf.lfs_file);
8667 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8668 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008669 cur_arg += 2;
8670 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008671 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 +02008672 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8673 goto out_err;
8674 }
8675
8676 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8677 struct acl_cond *cond;
8678 char *errmsg = NULL;
8679
8680 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8681 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8682 file, linenum, args[0], errmsg);
8683 free(errmsg);
8684 goto out_err;
8685 }
8686 rule->cond = cond;
8687 }
8688 else if (*args[cur_arg]) {
8689 Alert("parsing [%s:%d]: 'http-response %s' expects"
8690 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8691 file, linenum, args[0], args[cur_arg]);
8692 goto out_err;
8693 }
8694
8695 return rule;
8696 out_err:
8697 free(rule);
8698 return NULL;
8699}
8700
Willy Tarreau4baae242012-12-27 12:00:31 +01008701/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008702 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8703 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008704 */
8705struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008706 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008707{
8708 struct redirect_rule *rule;
8709 int cur_arg;
8710 int type = REDIRECT_TYPE_NONE;
8711 int code = 302;
8712 const char *destination = NULL;
8713 const char *cookie = NULL;
8714 int cookie_set = 0;
8715 unsigned int flags = REDIRECT_FLAG_NONE;
8716 struct acl_cond *cond = NULL;
8717
8718 cur_arg = 0;
8719 while (*(args[cur_arg])) {
8720 if (strcmp(args[cur_arg], "location") == 0) {
8721 if (!*args[cur_arg + 1])
8722 goto missing_arg;
8723
8724 type = REDIRECT_TYPE_LOCATION;
8725 cur_arg++;
8726 destination = args[cur_arg];
8727 }
8728 else if (strcmp(args[cur_arg], "prefix") == 0) {
8729 if (!*args[cur_arg + 1])
8730 goto missing_arg;
8731
8732 type = REDIRECT_TYPE_PREFIX;
8733 cur_arg++;
8734 destination = args[cur_arg];
8735 }
8736 else if (strcmp(args[cur_arg], "scheme") == 0) {
8737 if (!*args[cur_arg + 1])
8738 goto missing_arg;
8739
8740 type = REDIRECT_TYPE_SCHEME;
8741 cur_arg++;
8742 destination = args[cur_arg];
8743 }
8744 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8745 if (!*args[cur_arg + 1])
8746 goto missing_arg;
8747
8748 cur_arg++;
8749 cookie = args[cur_arg];
8750 cookie_set = 1;
8751 }
8752 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8753 if (!*args[cur_arg + 1])
8754 goto missing_arg;
8755
8756 cur_arg++;
8757 cookie = args[cur_arg];
8758 cookie_set = 0;
8759 }
8760 else if (strcmp(args[cur_arg], "code") == 0) {
8761 if (!*args[cur_arg + 1])
8762 goto missing_arg;
8763
8764 cur_arg++;
8765 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008766 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008767 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008768 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008769 args[cur_arg - 1], args[cur_arg]);
8770 return NULL;
8771 }
8772 }
8773 else if (!strcmp(args[cur_arg],"drop-query")) {
8774 flags |= REDIRECT_FLAG_DROP_QS;
8775 }
8776 else if (!strcmp(args[cur_arg],"append-slash")) {
8777 flags |= REDIRECT_FLAG_APPEND_SLASH;
8778 }
8779 else if (strcmp(args[cur_arg], "if") == 0 ||
8780 strcmp(args[cur_arg], "unless") == 0) {
8781 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8782 if (!cond) {
8783 memprintf(errmsg, "error in condition: %s", *errmsg);
8784 return NULL;
8785 }
8786 break;
8787 }
8788 else {
8789 memprintf(errmsg,
8790 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8791 args[cur_arg]);
8792 return NULL;
8793 }
8794 cur_arg++;
8795 }
8796
8797 if (type == REDIRECT_TYPE_NONE) {
8798 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8799 return NULL;
8800 }
8801
8802 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8803 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008804 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008805
8806 if (!use_fmt) {
8807 /* old-style static redirect rule */
8808 rule->rdr_str = strdup(destination);
8809 rule->rdr_len = strlen(destination);
8810 }
8811 else {
8812 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008813
8814 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8815 * if prefix == "/", we don't want to add anything, otherwise it
8816 * makes it hard for the user to configure a self-redirection.
8817 */
8818 proxy->conf.args.ctx = ARGC_RDR;
8819 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008820 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008821 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008822 free(curproxy->conf.lfs_file);
8823 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8824 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008825 }
8826 }
8827
Willy Tarreau4baae242012-12-27 12:00:31 +01008828 if (cookie) {
8829 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8830 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8831 */
8832 rule->cookie_len = strlen(cookie);
8833 if (cookie_set) {
8834 rule->cookie_str = malloc(rule->cookie_len + 10);
8835 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8836 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8837 rule->cookie_len += 9;
8838 } else {
8839 rule->cookie_str = malloc(rule->cookie_len + 21);
8840 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8841 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8842 rule->cookie_len += 20;
8843 }
8844 }
8845 rule->type = type;
8846 rule->code = code;
8847 rule->flags = flags;
8848 LIST_INIT(&rule->list);
8849 return rule;
8850
8851 missing_arg:
8852 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8853 return NULL;
8854}
8855
Willy Tarreau8797c062007-05-07 00:55:35 +02008856/************************************************************************/
8857/* The code below is dedicated to ACL parsing and matching */
8858/************************************************************************/
8859
8860
Willy Tarreau14174bc2012-04-16 14:34:04 +02008861/* This function ensures that the prerequisites for an L7 fetch are ready,
8862 * which means that a request or response is ready. If some data is missing,
8863 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008864 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8865 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008866 *
8867 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008868 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8869 * decide whether or not an HTTP message is present ;
8870 * 0 if the requested data cannot be fetched or if it is certain that
8871 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008872 * 1 if an HTTP message is ready
8873 */
8874static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008875smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008876 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008877{
8878 struct http_txn *txn = l7;
8879 struct http_msg *msg = &txn->req;
8880
8881 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8882 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8883 */
8884
8885 if (unlikely(!s || !txn))
8886 return 0;
8887
8888 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008889 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008890
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008891 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008892 if (unlikely(!s->req))
8893 return 0;
8894
Willy Tarreauaae75e32013-03-29 12:31:49 +01008895 /* If the buffer does not leave enough free space at the end,
8896 * we must first realign it.
8897 */
8898 if (s->req->buf->p > s->req->buf->data &&
8899 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8900 buffer_slow_realign(s->req->buf);
8901
Willy Tarreau14174bc2012-04-16 14:34:04 +02008902 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008903 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008904 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008905
8906 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008907 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008908 http_msg_analyzer(msg, &txn->hdr_idx);
8909
8910 /* Still no valid request ? */
8911 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008912 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008913 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008914 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008915 }
8916 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008917 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008918 return 0;
8919 }
8920
8921 /* OK we just got a valid HTTP request. We have some minor
8922 * preparation to perform so that further checks can rely
8923 * on HTTP tests.
8924 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008925
8926 /* If the request was parsed but was too large, we must absolutely
8927 * return an error so that it is not processed. At the moment this
8928 * cannot happen, but if the parsers are to change in the future,
8929 * we want this check to be maintained.
8930 */
8931 if (unlikely(s->req->buf->i + s->req->buf->p >
8932 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8933 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008934 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008935 return 1;
8936 }
8937
Willy Tarreau9b28e032012-10-12 23:49:43 +02008938 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008939 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8940 s->flags |= SN_REDIRECTABLE;
8941
Willy Tarreau506d0502013-07-06 13:29:24 +02008942 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8943 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008944 }
8945
Willy Tarreau506d0502013-07-06 13:29:24 +02008946 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008947 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008948 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008949
8950 /* otherwise everything's ready for the request */
8951 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008952 else {
8953 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008954 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8955 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008956 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008957 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008958 }
8959
8960 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008961 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008962 return 1;
8963}
Willy Tarreau8797c062007-05-07 00:55:35 +02008964
Willy Tarreauc0239e02012-04-16 14:42:55 +02008965#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008966 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 +02008967
Willy Tarreau24e32d82012-04-23 23:55:44 +02008968#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008969 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 +02008970
Willy Tarreau8797c062007-05-07 00:55:35 +02008971
8972/* 1. Check on METHOD
8973 * We use the pre-parsed method if it is known, and store its number as an
8974 * integer. If it is unknown, we use the pointer and the length.
8975 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01008976static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008977{
8978 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008979 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008980
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01008981 len = strlen(text);
8982 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008983
8984 pattern->val.i = meth;
8985 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01008986 trash = get_trash_chunk();
8987 if (trash->size < len) {
8988 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8989 len, trash->size);
8990 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008991 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01008992 pattern->ptr.str = trash->str;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008993 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008994 pattern->len = len;
8995 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008996 else
8997 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008998 return 1;
8999}
9000
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009001/* This function fetches the method of current HTTP request and stores
9002 * it in the global pattern struct as a chunk. There are two possibilities :
9003 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9004 * in <len> and <ptr> is NULL ;
9005 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9006 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009007 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009008 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009009static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009010smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009011 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009012{
9013 int meth;
9014 struct http_txn *txn = l7;
9015
Willy Tarreau24e32d82012-04-23 23:55:44 +02009016 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009017
Willy Tarreau8797c062007-05-07 00:55:35 +02009018 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009019 smp->type = SMP_T_UINT;
9020 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009021 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009022 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9023 /* ensure the indexes are not affected */
9024 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009025 smp->type = SMP_T_CSTR;
9026 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009027 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009028 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009029 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009030 return 1;
9031}
9032
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009033/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009034static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009035{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009036 int icase;
9037
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009038
Willy Tarreauf853c462012-04-23 18:53:56 +02009039 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009040 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009041 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009042 return PAT_MATCH;
9043 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009044 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009045
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009046 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9047 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009048 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009049
9050 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009051 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009052 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009053
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009054 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009055 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9056 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009057 return PAT_NOMATCH;
9058 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009059}
9060
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009061static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009062smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009063 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009064{
9065 struct http_txn *txn = l7;
9066 char *ptr;
9067 int len;
9068
Willy Tarreauc0239e02012-04-16 14:42:55 +02009069 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009070
Willy Tarreau8797c062007-05-07 00:55:35 +02009071 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009072 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009073
9074 while ((len-- > 0) && (*ptr++ != '/'));
9075 if (len <= 0)
9076 return 0;
9077
Willy Tarreauf853c462012-04-23 18:53:56 +02009078 smp->type = SMP_T_CSTR;
9079 smp->data.str.str = ptr;
9080 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009081
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009082 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009083 return 1;
9084}
9085
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009086static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009087smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009088 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009089{
9090 struct http_txn *txn = l7;
9091 char *ptr;
9092 int len;
9093
Willy Tarreauc0239e02012-04-16 14:42:55 +02009094 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009095
Willy Tarreauf26b2522012-12-14 08:33:14 +01009096 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9097 return 0;
9098
Willy Tarreau8797c062007-05-07 00:55:35 +02009099 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009100 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009101
9102 while ((len-- > 0) && (*ptr++ != '/'));
9103 if (len <= 0)
9104 return 0;
9105
Willy Tarreauf853c462012-04-23 18:53:56 +02009106 smp->type = SMP_T_CSTR;
9107 smp->data.str.str = ptr;
9108 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009109
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009110 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009111 return 1;
9112}
9113
9114/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009115static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009116smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009117 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009118{
9119 struct http_txn *txn = l7;
9120 char *ptr;
9121 int len;
9122
Willy Tarreauc0239e02012-04-16 14:42:55 +02009123 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009124
Willy Tarreauf26b2522012-12-14 08:33:14 +01009125 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9126 return 0;
9127
Willy Tarreau8797c062007-05-07 00:55:35 +02009128 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009129 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009130
Willy Tarreauf853c462012-04-23 18:53:56 +02009131 smp->type = SMP_T_UINT;
9132 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009133 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009134 return 1;
9135}
9136
9137/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009138static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009139smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009140 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009141{
9142 struct http_txn *txn = l7;
9143
Willy Tarreauc0239e02012-04-16 14:42:55 +02009144 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009145
Willy Tarreauf853c462012-04-23 18:53:56 +02009146 smp->type = SMP_T_CSTR;
9147 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009148 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009149 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009150 return 1;
9151}
9152
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009153static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009154smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009155 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009156{
9157 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009158 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009159
Willy Tarreauc0239e02012-04-16 14:42:55 +02009160 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009161
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009162 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9163 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009164 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009165
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009166 smp->type = SMP_T_IPV4;
9167 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009168 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009169 return 1;
9170}
9171
9172static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009173smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009174 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009175{
9176 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009177 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009178
Willy Tarreauc0239e02012-04-16 14:42:55 +02009179 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009180
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009181 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9182 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9183 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009184
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009185 smp->type = SMP_T_UINT;
9186 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009187 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009188 return 1;
9189}
9190
Willy Tarreau185b5c42012-04-26 15:11:51 +02009191/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9192 * Accepts an optional argument of type string containing the header field name,
9193 * and an optional argument of type signed or unsigned integer to request an
9194 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009195 * headers are considered from the first one. It does not stop on commas and
9196 * returns full lines instead (useful for User-Agent or Date for example).
9197 */
9198static int
9199smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009200 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009201{
9202 struct http_txn *txn = l7;
9203 struct hdr_idx *idx = &txn->hdr_idx;
9204 struct hdr_ctx *ctx = smp->ctx.a[0];
9205 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9206 int occ = 0;
9207 const char *name_str = NULL;
9208 int name_len = 0;
9209
9210 if (!ctx) {
9211 /* first call */
9212 ctx = &static_hdr_ctx;
9213 ctx->idx = 0;
9214 smp->ctx.a[0] = ctx;
9215 }
9216
9217 if (args) {
9218 if (args[0].type != ARGT_STR)
9219 return 0;
9220 name_str = args[0].data.str.str;
9221 name_len = args[0].data.str.len;
9222
9223 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9224 occ = args[1].data.uint;
9225 }
9226
9227 CHECK_HTTP_MESSAGE_FIRST();
9228
9229 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9230 /* search for header from the beginning */
9231 ctx->idx = 0;
9232
9233 if (!occ && !(opt & SMP_OPT_ITERATE))
9234 /* no explicit occurrence and single fetch => last header by default */
9235 occ = -1;
9236
9237 if (!occ)
9238 /* prepare to report multiple occurrences for ACL fetches */
9239 smp->flags |= SMP_F_NOT_LAST;
9240
9241 smp->type = SMP_T_CSTR;
9242 smp->flags |= SMP_F_VOL_HDR;
9243 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9244 return 1;
9245
9246 smp->flags &= ~SMP_F_NOT_LAST;
9247 return 0;
9248}
9249
9250/* 6. Check on HTTP header count. The number of occurrences is returned.
9251 * Accepts exactly 1 argument of type string. It does not stop on commas and
9252 * returns full lines instead (useful for User-Agent or Date for example).
9253 */
9254static int
9255smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009256 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009257{
9258 struct http_txn *txn = l7;
9259 struct hdr_idx *idx = &txn->hdr_idx;
9260 struct hdr_ctx ctx;
9261 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9262 int cnt;
9263
9264 if (!args || args->type != ARGT_STR)
9265 return 0;
9266
9267 CHECK_HTTP_MESSAGE_FIRST();
9268
9269 ctx.idx = 0;
9270 cnt = 0;
9271 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9272 cnt++;
9273
9274 smp->type = SMP_T_UINT;
9275 smp->data.uint = cnt;
9276 smp->flags = SMP_F_VOL_HDR;
9277 return 1;
9278}
9279
9280/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9281 * Accepts an optional argument of type string containing the header field name,
9282 * and an optional argument of type signed or unsigned integer to request an
9283 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009284 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009285 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009286static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009287smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009288 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009289{
9290 struct http_txn *txn = l7;
9291 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009292 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009293 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009294 int occ = 0;
9295 const char *name_str = NULL;
9296 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009297
Willy Tarreaua890d072013-04-02 12:01:06 +02009298 if (!ctx) {
9299 /* first call */
9300 ctx = &static_hdr_ctx;
9301 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009302 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009303 }
9304
Willy Tarreau185b5c42012-04-26 15:11:51 +02009305 if (args) {
9306 if (args[0].type != ARGT_STR)
9307 return 0;
9308 name_str = args[0].data.str.str;
9309 name_len = args[0].data.str.len;
9310
9311 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9312 occ = args[1].data.uint;
9313 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009314
Willy Tarreaue333ec92012-04-16 16:26:40 +02009315 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009316
Willy Tarreau185b5c42012-04-26 15:11:51 +02009317 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009318 /* search for header from the beginning */
9319 ctx->idx = 0;
9320
Willy Tarreau185b5c42012-04-26 15:11:51 +02009321 if (!occ && !(opt & SMP_OPT_ITERATE))
9322 /* no explicit occurrence and single fetch => last header by default */
9323 occ = -1;
9324
9325 if (!occ)
9326 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009327 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009328
Willy Tarreau185b5c42012-04-26 15:11:51 +02009329 smp->type = SMP_T_CSTR;
9330 smp->flags |= SMP_F_VOL_HDR;
9331 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 +02009332 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009333
Willy Tarreau37406352012-04-23 16:16:37 +02009334 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009335 return 0;
9336}
9337
Willy Tarreauc11416f2007-06-17 16:58:38 +02009338/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009339 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009340 */
9341static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009342smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009343 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009344{
9345 struct http_txn *txn = l7;
9346 struct hdr_idx *idx = &txn->hdr_idx;
9347 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009348 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009349 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009350
Willy Tarreau24e32d82012-04-23 23:55:44 +02009351 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009352 return 0;
9353
Willy Tarreaue333ec92012-04-16 16:26:40 +02009354 CHECK_HTTP_MESSAGE_FIRST();
9355
Willy Tarreau33a7e692007-06-10 19:45:56 +02009356 ctx.idx = 0;
9357 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009358 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 +02009359 cnt++;
9360
Willy Tarreauf853c462012-04-23 18:53:56 +02009361 smp->type = SMP_T_UINT;
9362 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009363 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009364 return 1;
9365}
9366
Willy Tarreau185b5c42012-04-26 15:11:51 +02009367/* Fetch an HTTP header's integer value. The integer value is returned. It
9368 * takes a mandatory argument of type string and an optional one of type int
9369 * to designate a specific occurrence. It returns an unsigned integer, which
9370 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009371 */
9372static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009373smp_fetch_hdr_val(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)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009375{
Willy Tarreauef38c392013-07-22 16:29:32 +02009376 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009377
Willy Tarreauf853c462012-04-23 18:53:56 +02009378 if (ret > 0) {
9379 smp->type = SMP_T_UINT;
9380 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9381 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009382
Willy Tarreaud53e2422012-04-16 17:21:11 +02009383 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009384}
9385
Cyril Bonté69fa9922012-10-25 00:01:06 +02009386/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9387 * and an optional one of type int to designate a specific occurrence.
9388 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009389 */
9390static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009391smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009392 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009393{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009394 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009395
Willy Tarreauef38c392013-07-22 16:29:32 +02009396 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009397 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9398 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009399 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009400 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009401 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009402 if (smp->data.str.len < temp->size - 1) {
9403 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9404 temp->str[smp->data.str.len] = '\0';
9405 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9406 smp->type = SMP_T_IPV6;
9407 break;
9408 }
9409 }
9410 }
9411
Willy Tarreaud53e2422012-04-16 17:21:11 +02009412 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009413 if (!(smp->flags & SMP_F_NOT_LAST))
9414 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009415 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009416 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009417}
9418
Willy Tarreau737b0c12007-06-10 21:28:46 +02009419/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9420 * the first '/' after the possible hostname, and ends before the possible '?'.
9421 */
9422static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009423smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009424 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009425{
9426 struct http_txn *txn = l7;
9427 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009428
Willy Tarreauc0239e02012-04-16 14:42:55 +02009429 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009430
Willy Tarreau9b28e032012-10-12 23:49:43 +02009431 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009432 ptr = http_get_path(txn);
9433 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009434 return 0;
9435
9436 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009437 smp->type = SMP_T_CSTR;
9438 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009439
9440 while (ptr < end && *ptr != '?')
9441 ptr++;
9442
Willy Tarreauf853c462012-04-23 18:53:56 +02009443 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009444 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009445 return 1;
9446}
9447
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009448/* This produces a concatenation of the first occurrence of the Host header
9449 * followed by the path component if it begins with a slash ('/'). This means
9450 * that '*' will not be added, resulting in exactly the first Host entry.
9451 * If no Host header is found, then the path is returned as-is. The returned
9452 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009453 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009454 */
9455static int
9456smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009457 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009458{
9459 struct http_txn *txn = l7;
9460 char *ptr, *end, *beg;
9461 struct hdr_ctx ctx;
9462
9463 CHECK_HTTP_MESSAGE_FIRST();
9464
9465 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009466 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 +02009467 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009468 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009469
9470 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009471 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009472 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009473 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009474 smp->data.str.len = ctx.vlen;
9475
9476 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009477 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 +02009478 beg = http_get_path(txn);
9479 if (!beg)
9480 beg = end;
9481
9482 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9483
9484 if (beg < ptr && *beg == '/') {
9485 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9486 smp->data.str.len += ptr - beg;
9487 }
9488
9489 smp->flags = SMP_F_VOL_1ST;
9490 return 1;
9491}
9492
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009493/* This produces a 32-bit hash of the concatenation of the first occurrence of
9494 * the Host header followed by the path component if it begins with a slash ('/').
9495 * This means that '*' will not be added, resulting in exactly the first Host
9496 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009497 * is hashed using the path hash followed by a full avalanche hash and provides a
9498 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009499 * high-traffic sites without having to store whole paths.
9500 */
9501static int
9502smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009503 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009504{
9505 struct http_txn *txn = l7;
9506 struct hdr_ctx ctx;
9507 unsigned int hash = 0;
9508 char *ptr, *beg, *end;
9509 int len;
9510
9511 CHECK_HTTP_MESSAGE_FIRST();
9512
9513 ctx.idx = 0;
9514 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9515 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9516 ptr = ctx.line + ctx.val;
9517 len = ctx.vlen;
9518 while (len--)
9519 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9520 }
9521
9522 /* now retrieve the path */
9523 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9524 beg = http_get_path(txn);
9525 if (!beg)
9526 beg = end;
9527
9528 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9529
9530 if (beg < ptr && *beg == '/') {
9531 while (beg < ptr)
9532 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9533 }
9534 hash = full_hash(hash);
9535
9536 smp->type = SMP_T_UINT;
9537 smp->data.uint = hash;
9538 smp->flags = SMP_F_VOL_1ST;
9539 return 1;
9540}
9541
Willy Tarreau4a550602012-12-09 14:53:32 +01009542/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009543 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9544 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9545 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009546 * that in environments where IPv6 is insignificant, truncating the output to
9547 * 8 bytes would still work.
9548 */
9549static int
9550smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009551 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009552{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009553 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009554 struct connection *cli_conn = objt_conn(l4->si[0].end);
9555
9556 if (!cli_conn)
9557 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009558
Willy Tarreauef38c392013-07-22 16:29:32 +02009559 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009560 return 0;
9561
Willy Tarreau47ca5452012-12-23 20:22:19 +01009562 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009563 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9564 temp->len += sizeof(smp->data.uint);
9565
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009566 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009567 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009568 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009569 temp->len += 4;
9570 break;
9571 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009572 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009573 temp->len += 16;
9574 break;
9575 default:
9576 return 0;
9577 }
9578
9579 smp->data.str = *temp;
9580 smp->type = SMP_T_BIN;
9581 return 1;
9582}
9583
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009584static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009585smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009586 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009587{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009588 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9589 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9590 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009591
Willy Tarreau24e32d82012-04-23 23:55:44 +02009592 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009593
Willy Tarreauf853c462012-04-23 18:53:56 +02009594 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009595 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009596 return 1;
9597}
9598
Willy Tarreau7f18e522010-10-22 20:04:13 +02009599/* return a valid test if the current request is the first one on the connection */
9600static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009601smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009602 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009603{
9604 if (!s)
9605 return 0;
9606
Willy Tarreauf853c462012-04-23 18:53:56 +02009607 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009608 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009609 return 1;
9610}
9611
Willy Tarreau34db1082012-04-19 17:16:54 +02009612/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009613static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009614smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009615 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009616{
9617
Willy Tarreau24e32d82012-04-23 23:55:44 +02009618 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009619 return 0;
9620
Willy Tarreauc0239e02012-04-16 14:42:55 +02009621 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009622
Willy Tarreauc0239e02012-04-16 14:42:55 +02009623 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009624 return 0;
9625
Willy Tarreauf853c462012-04-23 18:53:56 +02009626 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009627 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009628 return 1;
9629}
Willy Tarreau8797c062007-05-07 00:55:35 +02009630
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009631/* Accepts exactly 1 argument of type userlist */
9632static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009633smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009634 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009635{
9636
9637 if (!args || args->type != ARGT_USR)
9638 return 0;
9639
9640 CHECK_HTTP_MESSAGE_FIRST();
9641
9642 if (!get_http_auth(l4))
9643 return 0;
9644
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009645 /* if the user does not belong to the userlist or has a wrong password,
9646 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009647 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009648 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009649 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9650 return 0;
9651
9652 /* pat_match_auth() will need the user list */
9653 smp->ctx.a[0] = args->data.usr;
9654
9655 smp->type = SMP_T_CSTR;
9656 smp->data.str.str = l4->txn.auth.user;
9657 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009658
9659 return 1;
9660}
9661
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009662/* Try to find the next occurrence of a cookie name in a cookie header value.
9663 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9664 * the cookie value is returned into *value and *value_l, and the function
9665 * returns a pointer to the next pointer to search from if the value was found.
9666 * Otherwise if the cookie was not found, NULL is returned and neither value
9667 * nor value_l are touched. The input <hdr> string should first point to the
9668 * header's value, and the <hdr_end> pointer must point to the first character
9669 * not part of the value. <list> must be non-zero if value may represent a list
9670 * of values (cookie headers). This makes it faster to abort parsing when no
9671 * list is expected.
9672 */
9673static char *
9674extract_cookie_value(char *hdr, const char *hdr_end,
9675 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009676 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009677{
9678 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9679 char *next;
9680
9681 /* we search at least a cookie name followed by an equal, and more
9682 * generally something like this :
9683 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9684 */
9685 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9686 /* Iterate through all cookies on this line */
9687
9688 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9689 att_beg++;
9690
9691 /* find att_end : this is the first character after the last non
9692 * space before the equal. It may be equal to hdr_end.
9693 */
9694 equal = att_end = att_beg;
9695
9696 while (equal < hdr_end) {
9697 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9698 break;
9699 if (http_is_spht[(unsigned char)*equal++])
9700 continue;
9701 att_end = equal;
9702 }
9703
9704 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9705 * is between <att_beg> and <equal>, both may be identical.
9706 */
9707
9708 /* look for end of cookie if there is an equal sign */
9709 if (equal < hdr_end && *equal == '=') {
9710 /* look for the beginning of the value */
9711 val_beg = equal + 1;
9712 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9713 val_beg++;
9714
9715 /* find the end of the value, respecting quotes */
9716 next = find_cookie_value_end(val_beg, hdr_end);
9717
9718 /* make val_end point to the first white space or delimitor after the value */
9719 val_end = next;
9720 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9721 val_end--;
9722 } else {
9723 val_beg = val_end = next = equal;
9724 }
9725
9726 /* We have nothing to do with attributes beginning with '$'. However,
9727 * they will automatically be removed if a header before them is removed,
9728 * since they're supposed to be linked together.
9729 */
9730 if (*att_beg == '$')
9731 continue;
9732
9733 /* Ignore cookies with no equal sign */
9734 if (equal == next)
9735 continue;
9736
9737 /* Now we have the cookie name between att_beg and att_end, and
9738 * its value between val_beg and val_end.
9739 */
9740
9741 if (att_end - att_beg == cookie_name_l &&
9742 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9743 /* let's return this value and indicate where to go on from */
9744 *value = val_beg;
9745 *value_l = val_end - val_beg;
9746 return next + 1;
9747 }
9748
9749 /* Set-Cookie headers only have the name in the first attr=value part */
9750 if (!list)
9751 break;
9752 }
9753
9754 return NULL;
9755}
9756
William Lallemanda43ba4e2014-01-28 18:14:25 +01009757/* Fetch a captured HTTP request header. The index is the position of
9758 * the "capture" option in the configuration file
9759 */
9760static int
9761smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9762 const struct arg *args, struct sample *smp, const char *kw)
9763{
9764 struct proxy *fe = l4->fe;
9765 struct http_txn *txn = l7;
9766 int idx;
9767
9768 if (!args || args->type != ARGT_UINT)
9769 return 0;
9770
9771 idx = args->data.uint;
9772
9773 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9774 return 0;
9775
9776 smp->type = SMP_T_CSTR;
9777 smp->data.str.str = txn->req.cap[idx];
9778 smp->data.str.len = strlen(txn->req.cap[idx]);
9779
9780 return 1;
9781}
9782
9783/* Fetch a captured HTTP response header. The index is the position of
9784 * the "capture" option in the configuration file
9785 */
9786static int
9787smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9788 const struct arg *args, struct sample *smp, const char *kw)
9789{
9790 struct proxy *fe = l4->fe;
9791 struct http_txn *txn = l7;
9792 int idx;
9793
9794 if (!args || args->type != ARGT_UINT)
9795 return 0;
9796
9797 idx = args->data.uint;
9798
9799 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9800 return 0;
9801
9802 smp->type = SMP_T_CSTR;
9803 smp->data.str.str = txn->rsp.cap[idx];
9804 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9805
9806 return 1;
9807}
9808
William Lallemand65ad6e12014-01-31 15:08:02 +01009809/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9810static int
9811smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9812 const struct arg *args, struct sample *smp, const char *kw)
9813{
9814 struct chunk *temp;
9815 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009816 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009817
9818 if (!txn->uri)
9819 return 0;
9820
William Lallemand96a77852014-02-05 00:30:02 +01009821 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009822
William Lallemand96a77852014-02-05 00:30:02 +01009823 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9824 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009825
William Lallemand96a77852014-02-05 00:30:02 +01009826 temp = get_trash_chunk();
9827 temp->str = txn->uri;
9828 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009829 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009830 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009831
9832 return 1;
9833
9834}
9835
9836/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9837static int
9838smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9839 const struct arg *args, struct sample *smp, const char *kw)
9840{
9841 struct chunk *temp;
9842 struct http_txn *txn = l7;
9843 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009844
9845 if (!txn->uri)
9846 return 0;
William Lallemand96a77852014-02-05 00:30:02 +01009847
William Lallemand65ad6e12014-01-31 15:08:02 +01009848 ptr = txn->uri;
9849
9850 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9851 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +01009852
William Lallemand65ad6e12014-01-31 15:08:02 +01009853 if (!*ptr)
9854 return 0;
9855
9856 ptr++; /* skip the space */
9857
9858 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +01009859 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +01009860 if (!ptr)
9861 return 0;
9862 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9863 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009864
9865 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009866 smp->data.str.len = ptr - temp->str;
9867 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009868
9869 return 1;
9870}
9871
9872
Willy Tarreaue333ec92012-04-16 16:26:40 +02009873/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009874 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009875 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009876 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009877 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009878 * Accepts exactly 1 argument of type string. If the input options indicate
9879 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009880 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009881 */
9882static int
Willy Tarreau51539362012-05-08 12:46:28 +02009883smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009884 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009885{
9886 struct http_txn *txn = l7;
9887 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009888 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009889 const struct http_msg *msg;
9890 const char *hdr_name;
9891 int hdr_name_len;
9892 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009893 int occ = 0;
9894 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009895
Willy Tarreau24e32d82012-04-23 23:55:44 +02009896 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009897 return 0;
9898
Willy Tarreaua890d072013-04-02 12:01:06 +02009899 if (!ctx) {
9900 /* first call */
9901 ctx = &static_hdr_ctx;
9902 ctx->idx = 0;
9903 smp->ctx.a[2] = ctx;
9904 }
9905
Willy Tarreaue333ec92012-04-16 16:26:40 +02009906 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009907
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009908 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009909 msg = &txn->req;
9910 hdr_name = "Cookie";
9911 hdr_name_len = 6;
9912 } else {
9913 msg = &txn->rsp;
9914 hdr_name = "Set-Cookie";
9915 hdr_name_len = 10;
9916 }
9917
Willy Tarreau28376d62012-04-26 21:26:10 +02009918 if (!occ && !(opt & SMP_OPT_ITERATE))
9919 /* no explicit occurrence and single fetch => last cookie by default */
9920 occ = -1;
9921
9922 /* OK so basically here, either we want only one value and it's the
9923 * last one, or we want to iterate over all of them and we fetch the
9924 * next one.
9925 */
9926
Willy Tarreau9b28e032012-10-12 23:49:43 +02009927 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009928 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009929 /* search for the header from the beginning, we must first initialize
9930 * the search parameters.
9931 */
Willy Tarreau37406352012-04-23 16:16:37 +02009932 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009933 ctx->idx = 0;
9934 }
9935
Willy Tarreau28376d62012-04-26 21:26:10 +02009936 smp->flags |= SMP_F_VOL_HDR;
9937
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009938 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009939 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9940 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009941 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9942 goto out;
9943
Willy Tarreau24e32d82012-04-23 23:55:44 +02009944 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009945 continue;
9946
Willy Tarreau37406352012-04-23 16:16:37 +02009947 smp->ctx.a[0] = ctx->line + ctx->val;
9948 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009949 }
9950
Willy Tarreauf853c462012-04-23 18:53:56 +02009951 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009952 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009953 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009954 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009955 &smp->data.str.str,
9956 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009957 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009958 found = 1;
9959 if (occ >= 0) {
9960 /* one value was returned into smp->data.str.{str,len} */
9961 smp->flags |= SMP_F_NOT_LAST;
9962 return 1;
9963 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009964 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009965 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009966 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009967 /* all cookie headers and values were scanned. If we're looking for the
9968 * last occurrence, we may return it now.
9969 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009970 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009971 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009972 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009973}
9974
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009975/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009976 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009977 * multiple cookies may be parsed on the same line. The returned sample is of
9978 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009979 */
9980static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009981smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009982 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009983{
9984 struct http_txn *txn = l7;
9985 struct hdr_idx *idx = &txn->hdr_idx;
9986 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009987 const struct http_msg *msg;
9988 const char *hdr_name;
9989 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009990 int cnt;
9991 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009992 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009993
Willy Tarreau24e32d82012-04-23 23:55:44 +02009994 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009995 return 0;
9996
Willy Tarreaue333ec92012-04-16 16:26:40 +02009997 CHECK_HTTP_MESSAGE_FIRST();
9998
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009999 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010000 msg = &txn->req;
10001 hdr_name = "Cookie";
10002 hdr_name_len = 6;
10003 } else {
10004 msg = &txn->rsp;
10005 hdr_name = "Set-Cookie";
10006 hdr_name_len = 10;
10007 }
10008
Willy Tarreau9b28e032012-10-12 23:49:43 +020010009 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010010 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010011 ctx.idx = 0;
10012 cnt = 0;
10013
10014 while (1) {
10015 /* Note: val_beg == NULL every time we need to fetch a new header */
10016 if (!val_beg) {
10017 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10018 break;
10019
Willy Tarreau24e32d82012-04-23 23:55:44 +020010020 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010021 continue;
10022
10023 val_beg = ctx.line + ctx.val;
10024 val_end = val_beg + ctx.vlen;
10025 }
10026
Willy Tarreauf853c462012-04-23 18:53:56 +020010027 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010028 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010029 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010030 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010031 &smp->data.str.str,
10032 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010033 cnt++;
10034 }
10035 }
10036
Willy Tarreaub169eba2013-12-16 15:14:43 +010010037 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010038 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010039 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010040 return 1;
10041}
10042
Willy Tarreau51539362012-05-08 12:46:28 +020010043/* Fetch an cookie's integer value. The integer value is returned. It
10044 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10045 */
10046static int
10047smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010048 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010049{
Willy Tarreauef38c392013-07-22 16:29:32 +020010050 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010051
10052 if (ret > 0) {
10053 smp->type = SMP_T_UINT;
10054 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10055 }
10056
10057 return ret;
10058}
10059
Willy Tarreau8797c062007-05-07 00:55:35 +020010060/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010061/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010062/************************************************************************/
10063
David Cournapeau16023ee2010-12-23 20:55:41 +090010064/*
10065 * Given a path string and its length, find the position of beginning of the
10066 * query string. Returns NULL if no query string is found in the path.
10067 *
10068 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10069 *
10070 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10071 */
bedis4c75cca2012-10-05 08:38:24 +020010072static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010073{
10074 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010075
bedis4c75cca2012-10-05 08:38:24 +020010076 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010077 return p ? p + 1 : NULL;
10078}
10079
bedis4c75cca2012-10-05 08:38:24 +020010080static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010081{
bedis4c75cca2012-10-05 08:38:24 +020010082 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010083}
10084
10085/*
10086 * Given a url parameter, find the starting position of the first occurence,
10087 * or NULL if the parameter is not found.
10088 *
10089 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10090 * the function will return query_string+8.
10091 */
10092static char*
10093find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010094 char* url_param_name, size_t url_param_name_l,
10095 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010096{
10097 char *pos, *last;
10098
10099 pos = query_string;
10100 last = query_string + query_string_l - url_param_name_l - 1;
10101
10102 while (pos <= last) {
10103 if (pos[url_param_name_l] == '=') {
10104 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10105 return pos;
10106 pos += url_param_name_l + 1;
10107 }
bedis4c75cca2012-10-05 08:38:24 +020010108 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010109 pos++;
10110 pos++;
10111 }
10112 return NULL;
10113}
10114
10115/*
10116 * Given a url parameter name, returns its value and size into *value and
10117 * *value_l respectively, and returns non-zero. If the parameter is not found,
10118 * zero is returned and value/value_l are not touched.
10119 */
10120static int
10121find_url_param_value(char* path, size_t path_l,
10122 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010123 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010124{
10125 char *query_string, *qs_end;
10126 char *arg_start;
10127 char *value_start, *value_end;
10128
bedis4c75cca2012-10-05 08:38:24 +020010129 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010130 if (!query_string)
10131 return 0;
10132
10133 qs_end = path + path_l;
10134 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010135 url_param_name, url_param_name_l,
10136 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010137 if (!arg_start)
10138 return 0;
10139
10140 value_start = arg_start + url_param_name_l + 1;
10141 value_end = value_start;
10142
bedis4c75cca2012-10-05 08:38:24 +020010143 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010144 value_end++;
10145
10146 *value = value_start;
10147 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010148 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010149}
10150
10151static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010152smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010153 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010154{
bedis4c75cca2012-10-05 08:38:24 +020010155 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010156 struct http_txn *txn = l7;
10157 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010158
bedis4c75cca2012-10-05 08:38:24 +020010159 if (!args || args[0].type != ARGT_STR ||
10160 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010161 return 0;
10162
10163 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010164
bedis4c75cca2012-10-05 08:38:24 +020010165 if (args[1].type)
10166 delim = *args[1].data.str.str;
10167
Willy Tarreau9b28e032012-10-12 23:49:43 +020010168 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010169 args->data.str.str, args->data.str.len,
10170 &smp->data.str.str, &smp->data.str.len,
10171 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010172 return 0;
10173
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010174 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010175 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010176 return 1;
10177}
10178
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010179/* Return the signed integer value for the specified url parameter (see url_param
10180 * above).
10181 */
10182static int
10183smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010184 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010185{
Willy Tarreauef38c392013-07-22 16:29:32 +020010186 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010187
10188 if (ret > 0) {
10189 smp->type = SMP_T_UINT;
10190 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10191 }
10192
10193 return ret;
10194}
10195
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010196/* This produces a 32-bit hash of the concatenation of the first occurrence of
10197 * the Host header followed by the path component if it begins with a slash ('/').
10198 * This means that '*' will not be added, resulting in exactly the first Host
10199 * entry. If no Host header is found, then the path is used. The resulting value
10200 * is hashed using the url hash followed by a full avalanche hash and provides a
10201 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10202 * high-traffic sites without having to store whole paths.
10203 * this differs from the base32 functions in that it includes the url parameters
10204 * as well as the path
10205 */
10206static int
10207smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010208 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010209{
10210 struct http_txn *txn = l7;
10211 struct hdr_ctx ctx;
10212 unsigned int hash = 0;
10213 char *ptr, *beg, *end;
10214 int len;
10215
10216 CHECK_HTTP_MESSAGE_FIRST();
10217
10218 ctx.idx = 0;
10219 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10220 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10221 ptr = ctx.line + ctx.val;
10222 len = ctx.vlen;
10223 while (len--)
10224 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10225 }
10226
10227 /* now retrieve the path */
10228 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10229 beg = http_get_path(txn);
10230 if (!beg)
10231 beg = end;
10232
10233 for (ptr = beg; ptr < end ; ptr++);
10234
10235 if (beg < ptr && *beg == '/') {
10236 while (beg < ptr)
10237 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10238 }
10239 hash = full_hash(hash);
10240
10241 smp->type = SMP_T_UINT;
10242 smp->data.uint = hash;
10243 smp->flags = SMP_F_VOL_1ST;
10244 return 1;
10245}
10246
10247/* This concatenates the source address with the 32-bit hash of the Host and
10248 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10249 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10250 * on the source address length. The URL hash is stored before the address so
10251 * that in environments where IPv6 is insignificant, truncating the output to
10252 * 8 bytes would still work.
10253 */
10254static int
10255smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010256 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010257{
10258 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010259 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010260
Willy Tarreaue155ec22013-11-18 18:33:22 +010010261 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010262 return 0;
10263
10264 temp = get_trash_chunk();
10265 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10266 temp->len += sizeof(smp->data.uint);
10267
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010268 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010269 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010270 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010271 temp->len += 4;
10272 break;
10273 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010274 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010275 temp->len += 16;
10276 break;
10277 default:
10278 return 0;
10279 }
10280
10281 smp->data.str = *temp;
10282 smp->type = SMP_T_BIN;
10283 return 1;
10284}
10285
Willy Tarreau185b5c42012-04-26 15:11:51 +020010286/* This function is used to validate the arguments passed to any "hdr" fetch
10287 * keyword. These keywords support an optional positive or negative occurrence
10288 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10289 * is assumed that the types are already the correct ones. Returns 0 on error,
10290 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10291 * error message in case of error, that the caller is responsible for freeing.
10292 * The initial location must either be freeable or NULL.
10293 */
10294static int val_hdr(struct arg *arg, char **err_msg)
10295{
10296 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010297 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010298 return 0;
10299 }
10300 return 1;
10301}
10302
Willy Tarreau276fae92013-07-25 14:36:01 +020010303/* takes an UINT value on input supposed to represent the time since EPOCH,
10304 * adds an optional offset found in args[0] and emits a string representing
10305 * the date in RFC-1123/5322 format.
10306 */
10307static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10308{
10309 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10310 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10311 struct chunk *temp;
10312 struct tm *tm;
10313 time_t curr_date = smp->data.uint;
10314
10315 /* add offset */
10316 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10317 curr_date += args[0].data.sint;
10318
10319 tm = gmtime(&curr_date);
10320
10321 temp = get_trash_chunk();
10322 temp->len = snprintf(temp->str, temp->size - temp->len,
10323 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10324 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10325 tm->tm_hour, tm->tm_min, tm->tm_sec);
10326
10327 smp->data.str = *temp;
10328 smp->type = SMP_T_STR;
10329 return 1;
10330}
10331
Willy Tarreau4a568972010-05-12 08:08:50 +020010332/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010333/* All supported ACL keywords must be declared here. */
10334/************************************************************************/
10335
10336/* Note: must not be declared <const> as its list will be overwritten.
10337 * Please take care of keeping this list alphabetically sorted.
10338 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010339static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010340 { "base", "base", pat_parse_str, pat_idx_tree_str, pat_match_str },
10341 { "base_beg", "base", pat_parse_str, pat_idx_list_str, pat_match_beg },
10342 { "base_dir", "base", pat_parse_str, pat_idx_list_str, pat_match_dir },
10343 { "base_dom", "base", pat_parse_str, pat_idx_list_str, pat_match_dom },
10344 { "base_end", "base", pat_parse_str, pat_idx_list_str, pat_match_end },
10345 { "base_len", "base", pat_parse_int, pat_idx_list_val, pat_match_len },
10346 { "base_reg", "base", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10347 { "base_sub", "base", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010348
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010349 { "cook", "req.cook", pat_parse_str, pat_idx_tree_str, pat_match_str },
10350 { "cook_beg", "req.cook", pat_parse_str, pat_idx_list_str, pat_match_beg },
10351 { "cook_dir", "req.cook", pat_parse_str, pat_idx_list_str, pat_match_dir },
10352 { "cook_dom", "req.cook", pat_parse_str, pat_idx_list_str, pat_match_dom },
10353 { "cook_end", "req.cook", pat_parse_str, pat_idx_list_str, pat_match_end },
10354 { "cook_len", "req.cook", pat_parse_int, pat_idx_list_val, pat_match_len },
10355 { "cook_reg", "req.cook", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10356 { "cook_sub", "req.cook", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010357
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010358 { "hdr", "req.hdr", pat_parse_str, pat_idx_tree_str, pat_match_str },
10359 { "hdr_beg", "req.hdr", pat_parse_str, pat_idx_list_str, pat_match_beg },
10360 { "hdr_dir", "req.hdr", pat_parse_str, pat_idx_list_str, pat_match_dir },
10361 { "hdr_dom", "req.hdr", pat_parse_str, pat_idx_list_str, pat_match_dom },
10362 { "hdr_end", "req.hdr", pat_parse_str, pat_idx_list_str, pat_match_end },
10363 { "hdr_len", "req.hdr", pat_parse_int, pat_idx_list_val, pat_match_len },
10364 { "hdr_reg", "req.hdr", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10365 { "hdr_sub", "req.hdr", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010366
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010367 { "http_auth_group", NULL, pat_parse_str, pat_idx_list_str, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010368
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010369 { "method", NULL, pat_parse_meth, pat_idx_list_str, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010370
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010371 { "path", "path", pat_parse_str, pat_idx_tree_str, pat_match_str },
10372 { "path_beg", "path", pat_parse_str, pat_idx_list_str, pat_match_beg },
10373 { "path_dir", "path", pat_parse_str, pat_idx_list_str, pat_match_dir },
10374 { "path_dom", "path", pat_parse_str, pat_idx_list_str, pat_match_dom },
10375 { "path_end", "path", pat_parse_str, pat_idx_list_str, pat_match_end },
10376 { "path_len", "path", pat_parse_int, pat_idx_list_val, pat_match_len },
10377 { "path_reg", "path", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10378 { "path_sub", "path", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010379
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010380 { "req_ver", "req.ver", pat_parse_str, pat_idx_tree_str, pat_match_str },
10381 { "resp_ver", "res.ver", pat_parse_str, pat_idx_tree_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010382
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010383 { "scook", "res.cook", pat_parse_str, pat_idx_tree_str, pat_match_str },
10384 { "scook_beg", "res.cook", pat_parse_str, pat_idx_list_str, pat_match_beg },
10385 { "scook_dir", "res.cook", pat_parse_str, pat_idx_list_str, pat_match_dir },
10386 { "scook_dom", "res.cook", pat_parse_str, pat_idx_list_str, pat_match_dom },
10387 { "scook_end", "res.cook", pat_parse_str, pat_idx_list_str, pat_match_end },
10388 { "scook_len", "res.cook", pat_parse_int, pat_idx_list_val, pat_match_len },
10389 { "scook_reg", "res.cook", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10390 { "scook_sub", "res.cook", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010391
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010392 { "shdr", "res.hdr", pat_parse_str, pat_idx_tree_str, pat_match_str },
10393 { "shdr_beg", "res.hdr", pat_parse_str, pat_idx_list_str, pat_match_beg },
10394 { "shdr_dir", "res.hdr", pat_parse_str, pat_idx_list_str, pat_match_dir },
10395 { "shdr_dom", "res.hdr", pat_parse_str, pat_idx_list_str, pat_match_dom },
10396 { "shdr_end", "res.hdr", pat_parse_str, pat_idx_list_str, pat_match_end },
10397 { "shdr_len", "res.hdr", pat_parse_int, pat_idx_list_val, pat_match_len },
10398 { "shdr_reg", "res.hdr", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10399 { "shdr_sub", "res.hdr", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010400
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010401 { "url", "url", pat_parse_str, pat_idx_tree_str, pat_match_str },
10402 { "url_beg", "url", pat_parse_str, pat_idx_list_str, pat_match_beg },
10403 { "url_dir", "url", pat_parse_str, pat_idx_list_str, pat_match_dir },
10404 { "url_dom", "url", pat_parse_str, pat_idx_list_str, pat_match_dom },
10405 { "url_end", "url", pat_parse_str, pat_idx_list_str, pat_match_end },
10406 { "url_len", "url", pat_parse_int, pat_idx_list_val, pat_match_len },
10407 { "url_reg", "url", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10408 { "url_sub", "url", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010409
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010010410 { "urlp", "urlp", pat_parse_str, pat_idx_tree_str, pat_match_str },
10411 { "urlp_beg", "urlp", pat_parse_str, pat_idx_list_str, pat_match_beg },
10412 { "urlp_dir", "urlp", pat_parse_str, pat_idx_list_str, pat_match_dir },
10413 { "urlp_dom", "urlp", pat_parse_str, pat_idx_list_str, pat_match_dom },
10414 { "urlp_end", "urlp", pat_parse_str, pat_idx_list_str, pat_match_end },
10415 { "urlp_len", "urlp", pat_parse_int, pat_idx_list_val, pat_match_len },
10416 { "urlp_reg", "urlp", pat_parse_reg, pat_idx_list_reg, pat_match_reg },
10417 { "urlp_sub", "urlp", pat_parse_str, pat_idx_list_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010418
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010419 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010420}};
10421
10422/************************************************************************/
10423/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010424/************************************************************************/
10425/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010426static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010427 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10428 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10429 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10430
William Lallemand65ad6e12014-01-31 15:08:02 +010010431 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10432 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10433
William Lallemanda43ba4e2014-01-28 18:14:25 +010010434 /* capture are allocated and are permanent in the session */
10435 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10436 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10437
Willy Tarreau409bcde2013-01-08 00:31:00 +010010438 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10439 * are only here to match the ACL's name, are request-only and are used
10440 * for ACL compatibility only.
10441 */
10442 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10443 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10444 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10445 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10446
10447 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10448 * only here to match the ACL's name, are request-only and are used for
10449 * ACL compatibility only.
10450 */
10451 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10452 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10453 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10454 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10455
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010456 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010457 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010458 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10459 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10460 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010461
10462 /* HTTP protocol on the request path */
10463 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010464 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010465
10466 /* HTTP version on the request path */
10467 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010468 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010469
10470 /* HTTP version on the response path */
10471 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010472 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10473
Willy Tarreau18ed2562013-01-14 15:56:36 +010010474 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10475 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10476 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10477 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10478
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010479 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10480 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010481 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10482 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10483 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10484 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10485
10486 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10487 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10488 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10489 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10490
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010491 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10492 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010493 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10494 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10495 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10496 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10497
Willy Tarreau409bcde2013-01-08 00:31:00 +010010498 /* scook is valid only on the response and is used for ACL compatibility */
10499 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10500 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10501 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10502 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10503
10504 /* shdr is valid only on the response and is used for ACL compatibility */
10505 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10506 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10507 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10508 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10509
10510 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10511 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010512 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10513 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010514 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10515 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10516 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10517 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10518 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10519 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010520}};
10521
Willy Tarreau8797c062007-05-07 00:55:35 +020010522
Willy Tarreau276fae92013-07-25 14:36:01 +020010523/* Note: must not be declared <const> as its list will be overwritten */
10524static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10525 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10526 { NULL, NULL, 0, 0, 0 },
10527}};
10528
Willy Tarreau8797c062007-05-07 00:55:35 +020010529__attribute__((constructor))
10530static void __http_protocol_init(void)
10531{
10532 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010533 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010534 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010535}
10536
10537
Willy Tarreau58f10d72006-12-04 02:26:12 +010010538/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010539 * Local variables:
10540 * c-indent-level: 8
10541 * c-basic-offset: 8
10542 * End:
10543 */