blob: 0ed2acb691764a411ac762611c1aec7689f88194 [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>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreauc90dc232015-02-20 13:51:36 +0100590/* Find the first or next header field in message buffer <sol> using headers
591 * index <idx>, and return it in the <ctx> structure. This structure holds
592 * everything necessary to use the header and find next occurrence. If its
593 * <idx> member is 0, the first header is retrieved. Otherwise, the next
594 * occurrence is returned. The function returns 1 when it finds a value, and
595 * 0 when there is no more. It is equivalent to http_find_full_header2() with
596 * no header name.
597 */
598int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
599{
600 char *eol, *sov;
601 int cur_idx, old_idx;
602 int len;
603
604 cur_idx = ctx->idx;
605 if (cur_idx) {
606 /* We have previously returned a header, let's search another one */
607 sol = ctx->line;
608 eol = sol + idx->v[cur_idx].len;
609 goto next_hdr;
610 }
611
612 /* first request for this header */
613 sol += hdr_idx_first_pos(idx);
614 old_idx = 0;
615 cur_idx = hdr_idx_first_idx(idx);
616 while (cur_idx) {
617 eol = sol + idx->v[cur_idx].len;
618
619 len = 0;
620 while (1) {
621 if (len >= eol - sol)
622 goto next_hdr;
623 if (sol[len] == ':')
624 break;
625 len++;
626 }
627
628 ctx->del = len;
629 sov = sol + len + 1;
630 while (sov < eol && http_is_lws[(unsigned char)*sov])
631 sov++;
632
633 ctx->line = sol;
634 ctx->prev = old_idx;
635 ctx->idx = cur_idx;
636 ctx->val = sov - sol;
637 ctx->tws = 0;
638
639 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
640 eol--;
641 ctx->tws++;
642 }
643 ctx->vlen = eol - sov;
644 return 1;
645
646 next_hdr:
647 sol = eol + idx->v[cur_idx].cr + 1;
648 old_idx = cur_idx;
649 cur_idx = idx->v[cur_idx].next;
650 }
651 return 0;
652}
653
Willy Tarreau68085d82010-01-18 14:54:04 +0100654/* Find the end of the header value contained between <s> and <e>. See RFC2616,
655 * par 2.2 for more information. Note that it requires a valid header to return
656 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100658char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200659{
660 int quoted, qdpair;
661
662 quoted = qdpair = 0;
663 for (; s < e; s++) {
664 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200665 else if (quoted) {
666 if (*s == '\\') qdpair = 1;
667 else if (*s == '"') quoted = 0;
668 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 else if (*s == '"') quoted = 1;
670 else if (*s == ',') return s;
671 }
672 return s;
673}
674
675/* Find the first or next occurrence of header <name> in message buffer <sol>
676 * using headers index <idx>, and return it in the <ctx> structure. This
677 * structure holds everything necessary to use the header and find next
678 * occurrence. If its <idx> member is 0, the header is searched from the
679 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * 1 when it finds a value, and 0 when there is no more. It is designed to work
681 * with headers defined as comma-separated lists. As a special case, if ctx->val
682 * is NULL when searching for a new values of a header, the current header is
683 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200684 */
685int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
Willy Tarreau68085d82010-01-18 14:54:04 +0100689 char *eol, *sov;
690 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 cur_idx = ctx->idx;
693 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200694 /* We have previously returned a value, let's search
695 * another one on the same line.
696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200697 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200698 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 eol = sol + idx->v[cur_idx].len;
701
702 if (sov >= eol)
703 /* no more values in this header */
704 goto next_hdr;
705
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 /* values remaining for this header, skip the comma but save it
707 * for later use (eg: for header deletion).
708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200709 sov++;
710 while (sov < eol && http_is_lws[(unsigned char)*sov])
711 sov++;
712
713 goto return_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200723 if (len == 0) {
724 /* No argument was passed, we want any header.
725 * To achieve this, we simply build a fake request. */
726 while (sol + len < eol && sol[len] != ':')
727 len++;
728 name = sol;
729 }
730
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 if ((len < eol - sol) &&
732 (sol[len] == ':') &&
733 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200735 sov = sol + len + 1;
736 while (sov < eol && http_is_lws[(unsigned char)*sov])
737 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100738
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 ctx->prev = old_idx;
741 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744
745 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200747 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200748 eol--;
749 ctx->tws++;
750 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 ctx->vlen = eol - sov;
752 return 1;
753 }
754 next_hdr:
755 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100756 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 cur_idx = idx->v[cur_idx].next;
758 }
759 return 0;
760}
761
762int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 struct hdr_ctx *ctx)
765{
766 return http_find_header2(name, strlen(name), sol, idx, ctx);
767}
768
Willy Tarreau68085d82010-01-18 14:54:04 +0100769/* Remove one value of a header. This only works on a <ctx> returned by one of
770 * the http_find_header functions. The value is removed, as well as surrounding
771 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * message <msg>. The new index is returned. If it is zero, it means there is
774 * no more header, so any processing may stop. The ctx is always left in a form
775 * that can be handled by http_find_header2() to find next occurrence.
776 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100778{
779 int cur_idx = ctx->idx;
780 char *sol = ctx->line;
781 struct hdr_idx_elem *hdr;
782 int delta, skip_comma;
783
784 if (!cur_idx)
785 return 0;
786
787 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 http_msg_move_end(msg, delta);
792 idx->used--;
793 hdr->len = 0; /* unused entry */
794 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100795 if (idx->tail == ctx->idx)
796 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100798 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200800 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 return ctx->idx;
802 }
803
804 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
806 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 */
808
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200809 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200811 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 NULL, 0);
813 hdr->len += delta;
814 http_msg_move_end(msg, delta);
815 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200816 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 return ctx->idx;
818}
819
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820/* This function handles a server error at the stream interface level. The
821 * stream interface is assumed to be already in a closed state. An optional
822 * message is copied into the input buffer, and an HTTP status code stored.
823 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100824 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200826static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100829 channel_auto_read(si_oc(si));
830 channel_abort(si_oc(si));
831 channel_auto_close(si_oc(si));
832 channel_erase(si_oc(si));
833 channel_auto_close(si_ic(si));
834 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100835 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200836 s->txn.status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100837 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200839 if (!(s->flags & SN_ERR_MASK))
840 s->flags |= err;
841 if (!(s->flags & SN_FINST_MASK))
842 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843}
844
Willy Tarreau80587432006-12-24 17:47:20 +0100845/* This function returns the appropriate error location for the given session
846 * and message.
847 */
848
Willy Tarreau783f2582012-09-04 12:19:04 +0200849struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100850{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 if (s->be->errmsg[msgnum].str)
852 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100853 else if (s->fe->errmsg[msgnum].str)
854 return &s->fe->errmsg[msgnum];
855 else
856 return &http_err_chunks[msgnum];
857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreau53b6c742006-12-17 13:37:46 +0100859/*
860 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
861 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
862 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100864{
865 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100867
868 m = ((unsigned)*str - 'A');
869
870 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 for (h = http_methods[m]; h->len > 0; h++) {
872 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100873 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100875 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 };
877 return HTTP_METH_OTHER;
878 }
879 return HTTP_METH_NONE;
880
881}
882
Willy Tarreau21d2af32008-02-14 20:25:24 +0100883/* Parse the URI from the given transaction (which is assumed to be in request
884 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
885 * It is returned otherwise.
886 */
887static char *
888http_get_path(struct http_txn *txn)
889{
890 char *ptr, *end;
891
Willy Tarreau9b28e032012-10-12 23:49:43 +0200892 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 end = ptr + txn->req.sl.rq.u_l;
894
895 if (ptr >= end)
896 return NULL;
897
898 /* RFC2616, par. 5.1.2 :
899 * Request-URI = "*" | absuri | abspath | authority
900 */
901
902 if (*ptr == '*')
903 return NULL;
904
905 if (isalpha((unsigned char)*ptr)) {
906 /* this is a scheme as described by RFC3986, par. 3.1 */
907 ptr++;
908 while (ptr < end &&
909 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
910 ptr++;
911 /* skip '://' */
912 if (ptr == end || *ptr++ != ':')
913 return NULL;
914 if (ptr == end || *ptr++ != '/')
915 return NULL;
916 if (ptr == end || *ptr++ != '/')
917 return NULL;
918 }
919 /* skip [user[:passwd]@]host[:[port]] */
920
921 while (ptr < end && *ptr != '/')
922 ptr++;
923
924 if (ptr == end)
925 return NULL;
926
927 /* OK, we got the '/' ! */
928 return ptr;
929}
930
William Lallemand65ad6e12014-01-31 15:08:02 +0100931/* Parse the URI from the given string and look for the "/" beginning the PATH.
932 * If not found, return NULL. It is returned otherwise.
933 */
934static char *
935http_get_path_from_string(char *str)
936{
937 char *ptr = str;
938
939 /* RFC2616, par. 5.1.2 :
940 * Request-URI = "*" | absuri | abspath | authority
941 */
942
943 if (*ptr == '*')
944 return NULL;
945
946 if (isalpha((unsigned char)*ptr)) {
947 /* this is a scheme as described by RFC3986, par. 3.1 */
948 ptr++;
949 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
950 ptr++;
951 /* skip '://' */
952 if (*ptr == '\0' || *ptr++ != ':')
953 return NULL;
954 if (*ptr == '\0' || *ptr++ != '/')
955 return NULL;
956 if (*ptr == '\0' || *ptr++ != '/')
957 return NULL;
958 }
959 /* skip [user[:passwd]@]host[:[port]] */
960
961 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
962 ptr++;
963
964 if (*ptr == '\0' || *ptr == ' ')
965 return NULL;
966
967 /* OK, we got the '/' ! */
968 return ptr;
969}
970
Willy Tarreau71241ab2012-12-27 11:30:54 +0100971/* Returns a 302 for a redirectable request that reaches a server working in
972 * in redirect mode. This may only be called just after the stream interface
973 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
974 * follow normal proxy processing. NOTE: this function is designed to support
975 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100977void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978{
979 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200982 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 trash.len = strlen(HTTP_302);
986 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100988 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100989
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100991 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992 return;
993
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100994 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100996 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
997 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100998 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001000 /* 3: add the request URI. Since it was already forwarded, we need
1001 * to temporarily rewind the buffer.
1002 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 txn = &s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001004 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001005
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001007 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 +02001008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001009 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001010
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 if (!path)
1012 return;
1013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001014 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 return;
1016
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 memcpy(trash.str + trash.len, path, len);
1018 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001019
1020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001021 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1022 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001023 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001024 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1025 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001026 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001027
1028 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->state = SI_ST_CLO;
1033
1034 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +02001035 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001036
1037 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001038 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001039 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040}
1041
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001043 * that the server side is closed. Note that err_type is actually a
1044 * bitmask, where almost only aborts may be cumulated with other
1045 * values. We consider that aborted operations are more important
1046 * than timeouts or errors due to the fact that nobody else in the
1047 * logs might explain incomplete retries. All others should avoid
1048 * being cumulated. It should normally not be possible to have multiple
1049 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001050 * Note that connection errors appearing on the second request of a keep-alive
1051 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001053void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001054{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001058 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001059 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001061 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001062 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1063 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001064 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001065 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001066 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001068 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001069 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001071 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001072 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1073 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001075 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001076 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001077 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001078 else if (err_type & SI_ET_CONN_RES)
1079 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001080 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1081 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001083 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001084 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085}
1086
Willy Tarreau42250582007-04-01 01:30:43 +02001087extern const char sess_term_cond[8];
1088extern const char sess_fin_state[8];
1089extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001090struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001091struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001092struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Willy Tarreau117f59e2007-03-04 18:17:17 +01001094/*
1095 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001096 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001097 */
1098void capture_headers(char *som, struct hdr_idx *idx,
1099 char **cap, struct cap_hdr *cap_hdr)
1100{
1101 char *eol, *sol, *col, *sov;
1102 int cur_idx;
1103 struct cap_hdr *h;
1104 int len;
1105
1106 sol = som + hdr_idx_first_pos(idx);
1107 cur_idx = hdr_idx_first_idx(idx);
1108
1109 while (cur_idx) {
1110 eol = sol + idx->v[cur_idx].len;
1111
1112 col = sol;
1113 while (col < eol && *col != ':')
1114 col++;
1115
1116 sov = col + 1;
1117 while (sov < eol && http_is_lws[(unsigned char)*sov])
1118 sov++;
1119
1120 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001121 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001122 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1123 if (cap[h->index] == NULL)
1124 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001125 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001126
1127 if (cap[h->index] == NULL) {
1128 Alert("HTTP capture : out of memory.\n");
1129 continue;
1130 }
1131
1132 len = eol - sov;
1133 if (len > h->len)
1134 len = h->len;
1135
1136 memcpy(cap[h->index], sov, len);
1137 cap[h->index][len]=0;
1138 }
1139 }
1140 sol = eol + idx->v[cur_idx].cr + 1;
1141 cur_idx = idx->v[cur_idx].next;
1142 }
1143}
1144
1145
Willy Tarreau42250582007-04-01 01:30:43 +02001146/* either we find an LF at <ptr> or we jump to <bad>.
1147 */
1148#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1149
1150/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1151 * otherwise to <http_msg_ood> with <state> set to <st>.
1152 */
1153#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1154 ptr++; \
1155 if (likely(ptr < end)) \
1156 goto good; \
1157 else { \
1158 state = (st); \
1159 goto http_msg_ood; \
1160 } \
1161 } while (0)
1162
1163
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001165 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1167 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1168 * will give undefined results.
1169 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1170 * and that msg->sol points to the beginning of the response.
1171 * If a complete line is found (which implies that at least one CR or LF is
1172 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1173 * returned indicating an incomplete line (which does not mean that parts have
1174 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1175 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1176 * upon next call.
1177 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001178 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1180 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001181 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001183const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001184 enum ht_state state, const char *ptr, const char *end,
1185 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001188
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001192 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1194
1195 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001196 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1198 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001199 state = HTTP_MSG_ERROR;
1200 break;
1201
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001203 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001205 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 goto http_msg_rpcode;
1207 }
1208 if (likely(HTTP_IS_SPHT(*ptr)))
1209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001215 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 if (likely(!HTTP_IS_LWS(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1218
1219 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1222 }
1223
1224 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 http_msg_rsp_reason:
1227 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001228 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 msg->sl.st.r_l = 0;
1230 goto http_msg_rpline_eol;
1231
Willy Tarreau8973c702007-01-21 23:58:29 +01001232 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 goto http_msg_rpreason;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1240 /* so it's a CR/LF, so there is no reason phrase */
1241 goto http_msg_rsp_reason;
1242
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (likely(!HTTP_IS_CRLF(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 http_msg_rpline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the response would not be
1252 * complete. We can then record the response length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 return ptr;
1257
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001259#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1261 exit(1);
1262#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001263 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 }
1265
1266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001273}
1274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 * This function parses a request line between <ptr> and <end>, starting with
1277 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1278 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the request.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (likely(HTTP_IS_TOKEN(*ptr)))
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_CRLF(*ptr))) {
1312 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001313 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sl.rq.v_l = 0;
1321 goto http_msg_rqline_eol;
1322 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
1325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001329 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rquri;
1331 }
1332 if (likely(HTTP_IS_SPHT(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1334 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1335 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001339 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001343 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001347 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001348 /* non-ASCII chars are forbidden unless option
1349 * accept-invalid-http-request is enabled in the frontend.
1350 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001351 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001352 if (msg->err_pos < -1)
1353 goto invalid_char;
1354 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001356 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1357 }
1358
1359 if (likely(HTTP_IS_CRLF(*ptr))) {
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri_e;
1362 }
1363
1364 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001365 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 state = HTTP_MSG_ERROR;
1368 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001373 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 goto http_msg_rqver;
1375 }
1376 if (likely(HTTP_IS_SPHT(*ptr)))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1378 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1379 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001383 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385
1386 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001387 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001388 http_msg_rqline_eol:
1389 /* We have seen the end of line. Note that we do not
1390 * necessarily have the \n yet, but at least we know that we
1391 * have EITHER \r OR \n, otherwise the request would not be
1392 * complete. We can then record the request length and return
1393 * to the caller which will be able to register it.
1394 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001395 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001396 return ptr;
1397 }
1398
1399 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001400 state = HTTP_MSG_ERROR;
1401 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001404#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
1407#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001408 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001412 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (ret_state)
1414 *ret_state = state;
1415 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001420/*
1421 * Returns the data from Authorization header. Function may be called more
1422 * than once so data is stored in txn->auth_data. When no header is found
1423 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001424 * searching again for something we are unable to find anyway. However, if
1425 * the result if valid, the cache is not reused because we would risk to
1426 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001427 */
1428
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001429/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1430 * set according to global.tune.bufsize.
1431 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001432char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001433
1434int
1435get_http_auth(struct session *s)
1436{
1437
1438 struct http_txn *txn = &s->txn;
1439 struct chunk auth_method;
1440 struct hdr_ctx ctx;
1441 char *h, *p;
1442 int len;
1443
1444#ifdef DEBUG_AUTH
1445 printf("Auth for session %p: %d\n", s, txn->auth.method);
1446#endif
1447
1448 if (txn->auth.method == HTTP_AUTH_WRONG)
1449 return 0;
1450
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001451 txn->auth.method = HTTP_AUTH_WRONG;
1452
1453 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001454
1455 if (txn->flags & TX_USE_PX_CONN) {
1456 h = "Proxy-Authorization";
1457 len = strlen(h);
1458 } else {
1459 h = "Authorization";
1460 len = strlen(h);
1461 }
1462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001463 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001464 return 0;
1465
1466 h = ctx.line + ctx.val;
1467
1468 p = memchr(h, ' ', ctx.vlen);
1469 if (!p || p == h)
1470 return 0;
1471
1472 chunk_initlen(&auth_method, h, 0, p-h);
1473 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1474
1475 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1476
1477 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001478 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001479
1480 if (len < 0)
1481 return 0;
1482
1483
1484 get_http_auth_buff[len] = '\0';
1485
1486 p = strchr(get_http_auth_buff, ':');
1487
1488 if (!p)
1489 return 0;
1490
1491 txn->auth.user = get_http_auth_buff;
1492 *p = '\0';
1493 txn->auth.pass = p+1;
1494
1495 txn->auth.method = HTTP_AUTH_BASIC;
1496 return 1;
1497 }
1498
1499 return 0;
1500}
1501
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503/*
1504 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001505 * depending on the initial msg->msg_state. The caller is responsible for
1506 * ensuring that the message does not wrap. The function can be preempted
1507 * everywhere when data are missing and recalled at the exact same location
1508 * with no information loss. The message may even be realigned between two
1509 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001510 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001511 * fields. Note that msg->sol will be initialized after completing the first
1512 * state, so that none of the msg pointers has to be initialized prior to the
1513 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001515void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516{
Willy Tarreau3770f232013-12-07 00:01:53 +01001517 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001522 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 ptr = buf->p + msg->next;
1524 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(ptr >= end))
1527 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /*
1531 * First, states that are specific to the response only.
1532 * We check them first so that request and headers are
1533 * closer to each other (accessed more often).
1534 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 hdr_idx_init(idx);
1551 state = HTTP_MSG_RPVER;
1552 goto http_msg_rpver;
1553 }
1554
1555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
1557
1558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1561 /* stop here */
1562
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1567 /* stop here */
1568
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER_SP:
1572 case HTTP_MSG_RPCODE:
1573 case HTTP_MSG_RPCODE_SP:
1574 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 if (unlikely(!ptr))
1579 return;
1580
1581 /* we have a full response and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1589 goto http_msg_rpline_end;
1590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* msg->sol must point to the first of CR or LF. */
1594 EXPECT_LF_HERE(ptr, http_msg_invalid);
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1596 /* stop here */
1597
1598 /*
1599 * Second, states that are specific to the request only
1600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001602 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001604 /* we have a start of message, but we have to check
1605 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001606 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 if (likely(ptr != buf->p)) {
1609 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001611 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001612 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 }
Willy Tarreau26927362012-05-18 23:22:52 +02001614 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001615 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 state = HTTP_MSG_RQMETH;
1617 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1621 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(*ptr == '\n'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1625 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH_SP:
1637 case HTTP_MSG_RQURI:
1638 case HTTP_MSG_RQURI_SP:
1639 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001640 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001641 state, ptr, end,
1642 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(!ptr))
1644 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* we have a full request and we know that we have either a CR
1647 * or an LF at <ptr>.
1648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 /* check for HTTP/0.9 request : no version information available.
1659 * msg->sol must point to the first of CR or LF.
1660 */
1661 if (unlikely(msg->sl.rq.v_l == 0))
1662 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /*
1669 * Common states below
1670 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 if (likely(!HTTP_IS_CRLF(*ptr))) {
1675 goto http_msg_hdr_name;
1676 }
1677
1678 if (likely(*ptr == '\r'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1680 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 /* assumes msg->sol points to the first char */
1685 if (likely(HTTP_IS_TOKEN(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001688 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001691 if (likely(msg->err_pos < -1) || *ptr == '\n')
1692 goto http_msg_invalid;
1693
1694 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001696
1697 /* and we still accept this non-token character */
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001701 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_SPHT(*ptr)))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_val;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1715 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 EXPECT_LF_HERE(ptr, http_msg_invalid);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr))) {
1725 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 for (; buf->p + msg->sov < ptr; msg->sov++)
1727 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto http_msg_hdr_l1_sp;
1729 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001730 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001731 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 goto http_msg_complete_header;
1733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001736 /* assumes msg->sol points to the first char, and msg->sov
1737 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 */
1739 if (likely(!HTTP_IS_CRLF(*ptr)))
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* Note: we could also copy eol into ->eoh so that we have the
1744 * real header end in case it ends with lots of LWS, but is this
1745 * really needed ?
1746 */
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1749 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 EXPECT_LF_HERE(ptr, http_msg_invalid);
1754 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1759 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001760 for (; buf->p + msg->eol < ptr; msg->eol++)
1761 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 goto http_msg_hdr_val;
1763 }
1764 http_msg_complete_header:
1765 /*
1766 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001767 * Assumes msg->sol points to the first char, msg->sov points
1768 * to the first character of the value and msg->eol to the
1769 * first CR or LF so we know how the line ends. We insert last
1770 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 idx, idx->tail) < 0))
1774 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001775
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001787 /* Assumes msg->sol points to the first of either CR or LF.
1788 * Sets ->sov and ->next to the total header length, ->eoh to
1789 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EXPECT_LF_HERE(ptr, http_msg_invalid);
1792 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001794 msg->eoh = msg->sol;
1795 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001796 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001797 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001799
1800 case HTTP_MSG_ERROR:
1801 /* this may only happen if we call http_msg_analyser() twice with an error */
1802 break;
1803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001805#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1807 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001809 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
1811 http_msg_ood:
1812 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001813 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 http_msg_invalid:
1818 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001819 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 return;
1822}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001823
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1825 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1826 * nothing is done and 1 is returned.
1827 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001828static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001829{
1830 int delta;
1831 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001832 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001833
1834 if (msg->sl.rq.v_l != 0)
1835 return 1;
1836
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001837 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1838 if (txn->meth != HTTP_METH_GET)
1839 return 0;
1840
Willy Tarreau9b28e032012-10-12 23:49:43 +02001841 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001842 delta = 0;
1843
1844 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001845 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1846 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001847 }
1848 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001850 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001851 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001852 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001854 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 NULL, NULL);
1856 if (unlikely(!cur_end))
1857 return 0;
1858
1859 /* we have a full HTTP/1.0 request now and we know that
1860 * we have either a CR or an LF at <ptr>.
1861 */
1862 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1863 return 1;
1864}
1865
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001867 * and "keep-alive" values. If we already know that some headers may safely
1868 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1870 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001871 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1873 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1874 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001878{
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 const char *hdr_val = "Connection";
1881 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 return;
1885
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1887 hdr_val = "Proxy-Connection";
1888 hdr_len = 16;
1889 }
1890
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001893 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1895 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 if (to_del & 2)
1897 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else
1899 txn->flags |= TX_CON_KAL_SET;
1900 }
1901 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1902 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 if (to_del & 1)
1904 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 else
1906 txn->flags |= TX_CON_CLO_SET;
1907 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001908 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1909 txn->flags |= TX_HDR_CONN_UPG;
1910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 }
1912
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags |= TX_HDR_CONN_PRS;
1914 return;
1915}
Willy Tarreau5b154472009-12-21 20:11:07 +01001916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001917/* Apply desired changes on the Connection: header. Values may be removed and/or
1918 * added depending on the <wanted> flags, which are exclusively composed of
1919 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1920 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1921 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001922void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001923{
1924 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001925 const char *hdr_val = "Connection";
1926 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001927
1928 ctx.idx = 0;
1929
Willy Tarreau88d349d2010-01-25 12:15:43 +01001930
1931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1932 hdr_val = "Proxy-Connection";
1933 hdr_len = 16;
1934 }
1935
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001936 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001937 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1939 if (wanted & TX_CON_KAL_SET)
1940 txn->flags |= TX_CON_KAL_SET;
1941 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1945 if (wanted & TX_CON_CLO_SET)
1946 txn->flags |= TX_CON_CLO_SET;
1947 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001948 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001949 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001950 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951
1952 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1953 return;
1954
1955 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1956 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 hdr_val = "Connection: close";
1958 hdr_len = 17;
1959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1960 hdr_val = "Proxy-Connection: close";
1961 hdr_len = 23;
1962 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 }
1965
1966 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1967 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001968 hdr_val = "Connection: keep-alive";
1969 hdr_len = 22;
1970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection: keep-alive";
1972 hdr_len = 28;
1973 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001974 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 }
1976 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001977}
1978
Willy Tarreauc24715e2014-04-17 15:21:20 +02001979/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1980 * the first byte of data after the chunk size, so that we know we can forward
1981 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1982 * the chunk size. That way it is always possible to differentiate between the
1983 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001987static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001988{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001989 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001991 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *end = buf->data + buf->size;
1993 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 unsigned int chunk = 0;
1995
1996 /* The chunk size is in the following form, though we are only
1997 * interested in the size and CRLF :
1998 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1999 */
2000 while (1) {
2001 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 if (c < 0) /* not a hex digit anymore */
2006 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002009 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002011 chunk = (chunk << 4) + c;
2012 }
2013
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002015 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017
2018 while (http_is_spht[(unsigned char)*ptr]) {
2019 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002021 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 }
2024
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 /* Up to there, we know that at least one byte is present at *ptr. Check
2026 * for the end of chunk size.
2027 */
2028 while (1) {
2029 if (likely(HTTP_IS_CRLF(*ptr))) {
2030 /* we now have a CR or an LF at ptr */
2031 if (likely(*ptr == '\r')) {
2032 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002037
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002039 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 /* done */
2043 break;
2044 }
2045 else if (*ptr == ';') {
2046 /* chunk extension, ends at next CRLF */
2047 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 while (!HTTP_IS_CRLF(*ptr)) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
2058 /* we have a CRLF now, loop above */
2059 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 }
2064
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002066 * which may or may not be present. We save that into ->next,
2067 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002069 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002070 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002073 msg->chunk_len = chunk;
2074 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002077 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080}
2081
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002082/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 * the trailers is found, it is automatically scheduled to be forwarded,
2085 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2086 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * except maybe msg->next if it could parse some lines, and returns zero.
2088 * If a parse error is encountered, the function returns < 0 and does not
2089 * change anything except maybe msg->next. Note that the message must
2090 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002091 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 * forwarding, and that msg->next exactly matches the length of trailers
2093 * already parsed and not forwarded. It is also important to note that this
2094 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002096static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002098 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002099
Willy Tarreaua458b672012-03-05 11:17:50 +01002100 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *ptr = b_ptr(buf, msg->next);
2104 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002105 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106
2107 /* scan current line and stop at LF or CRLF */
2108 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002109 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 return 0;
2111
2112 if (*ptr == '\n') {
2113 if (!p1)
2114 p1 = ptr;
2115 p2 = ptr;
2116 break;
2117 }
2118
2119 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (p1) {
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 p1 = ptr;
2125 }
2126
2127 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 if (ptr >= buf->data + buf->size)
2129 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 }
2131
2132 /* after LF; point to beginning of next line */
2133 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (p2 >= buf->data + buf->size)
2135 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002137 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002138 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002140
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 /* LF/CRLF at beginning of line => end of trailers at p2.
2143 * Everything was scheduled for forwarding, there's nothing
2144 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_DONE;
2148 return 1;
2149 }
2150 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 }
2153}
2154
Willy Tarreauc24715e2014-04-17 15:21:20 +02002155/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2156 * or a possible LF alone at the end of a chunk. It automatically adjusts
2157 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002158 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2160 * not enough data are available, the function does not change anything and
2161 * returns zero. If a parse error is encountered, the function returns < 0 and
2162 * does not change anything. Note: this function is designed to parse wrapped
2163 * CRLF at the end of the buffer.
2164 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002165static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002167 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002168 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169 int bytes;
2170
2171 /* NB: we'll check data availabilty at the end. It's not a
2172 * problem because whatever we match first will be checked
2173 * against the correct length.
2174 */
2175 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002176 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 if (*ptr == '\r') {
2178 bytes++;
2179 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 if (ptr >= buf->data + buf->size)
2181 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 }
2183
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002184 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002185 return 0;
2186
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002187 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002188 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191
2192 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002193 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002194 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2198 return 1;
2199}
Willy Tarreau5b154472009-12-21 20:11:07 +01002200
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002201/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2202 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2203 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2204 * Unparsable qvalues return 1000 as "q=1.000".
2205 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002206int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207{
2208 int q = 1000;
2209
Willy Tarreau506c69a2014-07-08 00:59:48 +02002210 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002211 goto out;
2212 q = (*qvalue++ - '0') * 1000;
2213
2214 if (*qvalue++ != '.')
2215 goto out;
2216
Willy Tarreau506c69a2014-07-08 00:59:48 +02002217 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002218 goto out;
2219 q += (*qvalue++ - '0') * 100;
2220
Willy Tarreau506c69a2014-07-08 00:59:48 +02002221 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 goto out;
2223 q += (*qvalue++ - '0') * 10;
2224
Willy Tarreau506c69a2014-07-08 00:59:48 +02002225 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 goto out;
2227 q += (*qvalue++ - '0') * 1;
2228 out:
2229 if (q > 1000)
2230 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002231 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002232 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 return q;
2234}
William Lallemand82fe75c2012-10-23 10:25:10 +02002235
2236/*
2237 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002238 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002239int select_compression_request_header(struct session *s, struct buffer *req)
2240{
2241 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 struct hdr_ctx ctx;
2244 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002245 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002247 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2248 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002249 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2250 */
2251 ctx.idx = 0;
2252 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2253 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002254 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2255 (ctx.vlen < 31 ||
2256 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2257 ctx.line[ctx.val + 30] < '6' ||
2258 (ctx.line[ctx.val + 30] == '6' &&
2259 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2260 s->comp_algo = NULL;
2261 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002262 }
2263
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 int best_q = 0;
2267
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 ctx.idx = 0;
2269 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002270 const char *qval;
2271 int q;
2272 int toklen;
2273
2274 /* try to isolate the token from the optional q-value */
2275 toklen = 0;
2276 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2277 toklen++;
2278
2279 qval = ctx.line + ctx.val + toklen;
2280 while (1) {
2281 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2282 qval++;
2283
2284 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2285 qval = NULL;
2286 break;
2287 }
2288 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002289
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2291 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002292
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002293 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2294 qval = NULL;
2295 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002297 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2298 break;
2299
2300 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2301 qval++;
2302 }
2303
2304 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002305 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306
2307 if (q <= best_q)
2308 continue;
2309
2310 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2311 if (*(ctx.line + ctx.val) == '*' ||
2312 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2313 s->comp_algo = comp_algo;
2314 best_q = q;
2315 break;
2316 }
2317 }
2318 }
2319 }
2320
2321 /* remove all occurrences of the header when "compression offload" is set */
2322 if (s->comp_algo) {
2323 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2324 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2325 ctx.idx = 0;
2326 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2327 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 }
2329 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002330 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
2332
2333 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2335 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 if (comp_algo->add_data == identity_add_data) {
2337 s->comp_algo = comp_algo;
2338 return 1;
2339 }
2340 }
2341 }
2342
2343 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 return 0;
2345}
2346
2347/*
2348 * Selects a comression algorithm depending of the server response.
2349 */
2350int select_compression_response_header(struct session *s, struct buffer *res)
2351{
2352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->rsp;
2354 struct hdr_ctx ctx;
2355 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002356
2357 /* no common compression algorithm was found in request header */
2358 if (s->comp_algo == NULL)
2359 goto fail;
2360
2361 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002362 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 goto fail;
2364
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002365 /* compress 200,201,202,203 responses only */
2366 if ((txn->status != 200) &&
2367 (txn->status != 201) &&
2368 (txn->status != 202) &&
2369 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002370 goto fail;
2371
William Lallemand82fe75c2012-10-23 10:25:10 +02002372 /* Content-Length is null */
2373 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2374 goto fail;
2375
2376 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002377 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002378 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2379 goto fail;
2380
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002381 /* no compression when Cache-Control: no-transform is present in the message */
2382 ctx.idx = 0;
2383 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2384 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2385 goto fail;
2386 }
2387
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 comp_type = NULL;
2389
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002390 /* we don't want to compress multipart content-types, nor content-types that are
2391 * not listed in the "compression type" directive if any. If no content-type was
2392 * found but configuration requires one, we don't compress either. Backend has
2393 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002394 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002395 ctx.idx = 0;
2396 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2397 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2398 goto fail;
2399
2400 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2401 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002403 if (ctx.vlen >= comp_type->name_len &&
2404 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002405 /* this Content-Type should be compressed */
2406 break;
2407 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002408 /* this Content-Type should not be compressed */
2409 if (comp_type == NULL)
2410 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002411 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002412 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002413 else { /* no content-type header */
2414 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2415 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002416 }
2417
William Lallemandd85f9172012-11-09 17:05:39 +01002418 /* limit compression rate */
2419 if (global.comp_rate_lim > 0)
2420 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2421 goto fail;
2422
William Lallemand072a2bf2012-11-20 17:01:01 +01002423 /* limit cpu usage */
2424 if (idle_pct < compress_min_idle)
2425 goto fail;
2426
William Lallemand4c49fae2012-11-07 15:00:23 +01002427 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002428 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002429 goto fail;
2430
William Lallemandec3e3892012-11-12 17:02:18 +01002431 s->flags |= SN_COMP_READY;
2432
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002434 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002435 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2436 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2437
2438 /* add Transfer-Encoding header */
2439 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2440 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2441
2442 /*
2443 * Add Content-Encoding header when it's not identity encoding.
2444 * RFC 2616 : Identity encoding: This content-coding is used only in the
2445 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2446 * header.
2447 */
2448 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002449 trash.len = 18;
2450 memcpy(trash.str, "Content-Encoding: ", trash.len);
2451 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2452 trash.len += s->comp_algo->name_len;
2453 trash.str[trash.len] = '\0';
2454 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002456 return 1;
2457
2458fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002459 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002460 return 0;
2461}
2462
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002463void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2464{
2465 int tmp = TX_CON_WANT_KAL;
2466
2467 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2468 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2469 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2470 tmp = TX_CON_WANT_TUN;
2471
2472 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2473 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2474 tmp = TX_CON_WANT_TUN;
2475 }
2476
2477 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2479 /* option httpclose + server_close => forceclose */
2480 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2482 tmp = TX_CON_WANT_CLO;
2483 else
2484 tmp = TX_CON_WANT_SCL;
2485 }
2486
2487 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2488 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2489 tmp = TX_CON_WANT_CLO;
2490
2491 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2492 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2493
2494 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2495 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2496 /* parse the Connection header and possibly clean it */
2497 int to_del = 0;
2498 if ((msg->flags & HTTP_MSGF_VER_11) ||
2499 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2500 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2501 to_del |= 2; /* remove "keep-alive" */
2502 if (!(msg->flags & HTTP_MSGF_VER_11))
2503 to_del |= 1; /* remove "close" */
2504 http_parse_connection_header(txn, msg, to_del);
2505 }
2506
2507 /* check if client or config asks for explicit close in KAL/SCL */
2508 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2509 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2510 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2511 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2512 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2513 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2514 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2515}
William Lallemand82fe75c2012-10-23 10:25:10 +02002516
Willy Tarreaud787e662009-07-07 10:14:51 +02002517/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2518 * processing can continue on next analysers, or zero if it either needs more
2519 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002520 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 * when it has nothing left to do, and may remove any analyser when it wants to
2522 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002523 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002524int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002525{
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 /*
2527 * We will parse the partial (or complete) lines.
2528 * We will check the request syntax, and also join multi-line
2529 * headers. An index of all the lines will be elaborated while
2530 * parsing.
2531 *
2532 * For the parsing, we use a 28 states FSM.
2533 *
2534 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002535 * req->buf->p = beginning of request
2536 * req->buf->p + msg->eoh = end of processed headers / start of current one
2537 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002538 * msg->eol = end of current header or line (LF or CRLF)
2539 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 *
2541 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002542 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2544 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002546
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002548 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 struct http_txn *txn = &s->txn;
2550 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002551 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002552
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002553 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 +01002554 now_ms, __FUNCTION__,
2555 s,
2556 req,
2557 req->rex, req->wex,
2558 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002559 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002560 req->analysers);
2561
Willy Tarreau52a0c602009-08-16 22:45:38 +02002562 /* we're speaking HTTP here, so let's speak HTTP to the client */
2563 s->srv_error = http_return_srv_error;
2564
Willy Tarreau83e3af02009-12-28 17:39:57 +01002565 /* There's a protected area at the end of the buffer for rewriting
2566 * purposes. We don't want to start to parse the request if the
2567 * protected area is affected, because we may have to move processed
2568 * data later, which is much more complicated.
2569 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002570 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002571 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002572 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002573 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002574 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002575 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002576 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002577 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002578 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002579 return 0;
2580 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002581 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2582 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2583 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002584 }
2585
Willy Tarreau065e8332010-01-08 00:30:20 +01002586 /* Note that we have the same problem with the response ; we
2587 * may want to send a redirect, error or anything which requires
2588 * some spare space. So we'll ensure that we have at least
2589 * maxrewrite bytes available in the response buffer before
2590 * processing that one. This will only affect pipelined
2591 * keep-alive requests.
2592 */
2593 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002594 unlikely(!channel_is_rewritable(&s->res) ||
2595 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2596 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2597 if (s->res.buf->o) {
2598 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002599 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002600 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002601 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002602 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2603 s->res.flags |= CF_WAKE_WRITE;
2604 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002605 return 0;
2606 }
2607 }
2608
Willy Tarreau9b28e032012-10-12 23:49:43 +02002609 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002610 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002611 }
2612
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 /* 1: we might have to print this header in debug mode */
2614 if (unlikely((global.mode & MODE_DEBUG) &&
2615 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002616 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002618
Willy Tarreau9b28e032012-10-12 23:49:43 +02002619 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002620 /* this is a bit complex : in case of error on the request line,
2621 * we know that rq.l is still zero, so we display only the part
2622 * up to the end of the line (truncated by debug_hdr).
2623 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002624 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 sol += hdr_idx_first_pos(&txn->hdr_idx);
2628 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002629
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 while (cur_idx) {
2631 eol = sol + txn->hdr_idx.v[cur_idx].len;
2632 debug_hdr("clihdr", s, sol, eol);
2633 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2634 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
2637
Willy Tarreau58f10d72006-12-04 02:26:12 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /*
2640 * Now we quickly check if we have found a full valid request.
2641 * If not so, we check the FD and buffer states before leaving.
2642 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002643 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002644 * requests are checked first. When waiting for a second request
2645 * on a keep-alive session, if we encounter and error, close, t/o,
2646 * we note the error in the session flags but don't set any state.
2647 * Since the error will be noted there, it will not be counted by
2648 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002649 * Last, we may increase some tracked counters' http request errors on
2650 * the cases that are deliberately the client's fault. For instance,
2651 * a timeout or connection reset is not counted as an error. However
2652 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002654
Willy Tarreau655dce92009-11-08 13:10:58 +01002655 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002656 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002658 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002660 session_inc_http_req_ctr(s);
2661 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002662 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002664 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002665
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 /* 1: Since we are in header mode, if there's no space
2667 * left for headers, we won't be able to free more
2668 * later, so the session will never terminate. We
2669 * must terminate it now.
2670 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 /* FIXME: check if URI is set and return Status
2673 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002674 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002675 session_inc_http_req_ctr(s);
2676 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002677 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002678 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002679 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 goto return_bad_req;
2681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002684 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002685 if (!(s->flags & SN_ERR_MASK))
2686 s->flags |= SN_ERR_CLICL;
2687
Willy Tarreaufcffa692010-01-10 14:21:19 +01002688 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002689 goto failed_keep_alive;
2690
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002692 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002693 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 session_inc_http_err_ctr(s);
2695 }
2696
Willy Tarreaudc979f22012-12-04 10:39:01 +01002697 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002698 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002699 msg->msg_state = HTTP_MSG_ERROR;
2700 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002701
Willy Tarreauda7ff642010-06-23 11:44:09 +02002702 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002703 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002704 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002705 if (s->listener->counters)
2706 s->listener->counters->failed_req++;
2707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 if (!(s->flags & SN_FINST_MASK))
2709 s->flags |= SN_FINST_R;
2710 return 0;
2711 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002714 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002715 if (!(s->flags & SN_ERR_MASK))
2716 s->flags |= SN_ERR_CLITO;
2717
Willy Tarreaufcffa692010-01-10 14:21:19 +01002718 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002719 goto failed_keep_alive;
2720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002722 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002723 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002724 session_inc_http_err_ctr(s);
2725 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002727 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 msg->msg_state = HTTP_MSG_ERROR;
2729 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002730
Willy Tarreauda7ff642010-06-23 11:44:09 +02002731 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002732 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002733 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002734 if (s->listener->counters)
2735 s->listener->counters->failed_req++;
2736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 if (!(s->flags & SN_FINST_MASK))
2738 s->flags |= SN_FINST_R;
2739 return 0;
2740 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002743 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002744 if (!(s->flags & SN_ERR_MASK))
2745 s->flags |= SN_ERR_CLICL;
2746
Willy Tarreaufcffa692010-01-10 14:21:19 +01002747 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002748 goto failed_keep_alive;
2749
Willy Tarreau4076a152009-04-02 15:18:36 +02002750 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002751 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002753 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 msg->msg_state = HTTP_MSG_ERROR;
2755 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002756
Willy Tarreauda7ff642010-06-23 11:44:09 +02002757 session_inc_http_err_ctr(s);
2758 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002759 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002760 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002761 if (s->listener->counters)
2762 s->listener->counters->failed_req++;
2763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 if (!(s->flags & SN_FINST_MASK))
2765 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002766 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002767 }
2768
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002769 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002770 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002771 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002772#ifdef TCP_QUICKACK
Willy Tarreau350f4872014-11-28 14:42:25 +01002773 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->si[0].end) && conn_ctrl_ready(__objt_conn(s->si[0].end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002774 /* We need more data, we have to re-enable quick-ack in case we
2775 * previously disabled it, otherwise we might cause the client
2776 * to delay next data.
2777 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002778 setsockopt(__objt_conn(s->si[0].end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002779 }
2780#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002781
Willy Tarreaufcffa692010-01-10 14:21:19 +01002782 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2783 /* If the client starts to talk, let's fall back to
2784 * request timeout processing.
2785 */
2786 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002787 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002788 }
2789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002791 if (!tick_isset(req->analyse_exp)) {
2792 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2793 (txn->flags & TX_WAIT_NEXT_RQ) &&
2794 tick_isset(s->be->timeout.httpka))
2795 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2796 else
2797 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2798 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* we're not ready yet */
2801 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002802
2803 failed_keep_alive:
2804 /* Here we process low-level errors for keep-alive requests. In
2805 * short, if the request is not the first one and it experiences
2806 * a timeout, read error or shutdown, we just silently close so
2807 * that the client can try again.
2808 */
2809 txn->status = 0;
2810 msg->msg_state = HTTP_MSG_RQBEFORE;
2811 req->analysers = 0;
2812 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002813 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002814 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002815 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002816 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002818
Willy Tarreaud787e662009-07-07 10:14:51 +02002819 /* OK now we have a complete HTTP request with indexed headers. Let's
2820 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002821 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002822 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002823 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002824 * byte after the last LF. msg->sov points to the first byte of data.
2825 * msg->eol cannot be trusted because it may have been left uninitialized
2826 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002827 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002828
Willy Tarreauda7ff642010-06-23 11:44:09 +02002829 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002830 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2831
Willy Tarreaub16a5742010-01-10 14:46:16 +01002832 if (txn->flags & TX_WAIT_NEXT_RQ) {
2833 /* kill the pending keep-alive timeout */
2834 txn->flags &= ~TX_WAIT_NEXT_RQ;
2835 req->analyse_exp = TICK_ETERNITY;
2836 }
2837
2838
Willy Tarreaud787e662009-07-07 10:14:51 +02002839 /* Maybe we found in invalid header name while we were configured not
2840 * to block on that, so we have to capture it now.
2841 */
2842 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002843 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002844
Willy Tarreau59234e92008-11-30 23:51:27 +01002845 /*
2846 * 1: identify the method
2847 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002848 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002849
2850 /* we can make use of server redirect on GET and HEAD */
2851 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2852 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /*
2855 * 2: check if the URI matches the monitor_uri.
2856 * We have to do this for every request which gets in, because
2857 * the monitor-uri is defined by the frontend.
2858 */
2859 if (unlikely((s->fe->monitor_uri_len != 0) &&
2860 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002861 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 s->fe->monitor_uri,
2863 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002864 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002865 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002866 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002870 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002871
Willy Tarreau59234e92008-11-30 23:51:27 +01002872 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002873 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002874 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 ret = acl_pass(ret);
2877 if (cond->pol == ACL_COND_UNLESS)
2878 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002879
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 if (ret) {
2881 /* we fail this request, let's return 503 service unavail */
2882 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002883 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002884 if (!(s->flags & SN_ERR_MASK))
2885 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002886 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002887 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002888 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002889
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 /* nothing to fail, let's reply normaly */
2891 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002892 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002893 if (!(s->flags & SN_ERR_MASK))
2894 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 goto return_prx_cond;
2896 }
2897
2898 /*
2899 * 3: Maybe we have to copy the original REQURI for the logs ?
2900 * Note: we cannot log anymore if the request has been
2901 * classified as invalid.
2902 */
2903 if (unlikely(s->logs.logwait & LW_REQ)) {
2904 /* we have a complete HTTP request that we must log */
2905 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2906 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002907
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 if (urilen >= REQURI_LEN)
2909 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002910 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002912
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002913 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 s->do_log(s);
2915 } else {
2916 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002917 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 }
Willy Tarreau06619262006-12-17 08:37:22 +01002919
Willy Tarreau59234e92008-11-30 23:51:27 +01002920 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002921 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002922 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002923
Willy Tarreau5b154472009-12-21 20:11:07 +01002924 /* ... and check if the request is HTTP/1.1 or above */
2925 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002926 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2927 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2928 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002929 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002930
2931 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002932 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 +01002933
Willy Tarreau88d349d2010-01-25 12:15:43 +01002934 /* if the frontend has "option http-use-proxy-header", we'll check if
2935 * we have what looks like a proxied connection instead of a connection,
2936 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2937 * Note that this is *not* RFC-compliant, however browsers and proxies
2938 * happen to do that despite being non-standard :-(
2939 * We consider that a request not beginning with either '/' or '*' is
2940 * a proxied connection, which covers both "scheme://location" and
2941 * CONNECT ip:port.
2942 */
2943 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002944 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002945 txn->flags |= TX_USE_PX_CONN;
2946
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002947 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002948 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002949
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002951 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002952 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002954
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002955 /* 6: determine the transfer-length.
2956 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2957 * the presence of a message-body in a REQUEST and its transfer length
2958 * must be determined that way (in order of precedence) :
2959 * 1. The presence of a message-body in a request is signaled by the
2960 * inclusion of a Content-Length or Transfer-Encoding header field
2961 * in the request's header fields. When a request message contains
2962 * both a message-body of non-zero length and a method that does
2963 * not define any semantics for that request message-body, then an
2964 * origin server SHOULD either ignore the message-body or respond
2965 * with an appropriate error message (e.g., 413). A proxy or
2966 * gateway, when presented the same request, SHOULD either forward
2967 * the request inbound with the message- body or ignore the
2968 * message-body when determining a response.
2969 *
2970 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2971 * and the "chunked" transfer-coding (Section 6.2) is used, the
2972 * transfer-length is defined by the use of this transfer-coding.
2973 * If a Transfer-Encoding header field is present and the "chunked"
2974 * transfer-coding is not present, the transfer-length is defined
2975 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002976 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002977 * 3. If a Content-Length header field is present, its decimal value in
2978 * OCTETs represents both the entity-length and the transfer-length.
2979 * If a message is received with both a Transfer-Encoding header
2980 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002981 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002982 * 4. By the server closing the connection. (Closing the connection
2983 * cannot be used to indicate the end of a request body, since that
2984 * would leave no possibility for the server to send back a response.)
2985 *
2986 * Whenever a transfer-coding is applied to a message-body, the set of
2987 * transfer-codings MUST include "chunked", unless the message indicates
2988 * it is terminated by closing the connection. When the "chunked"
2989 * transfer-coding is used, it MUST be the last transfer-coding applied
2990 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002991 */
2992
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002994 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002997 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3000 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 /* bad transfer-encoding (chunked followed by something else) */
3002 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003003 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003004 break;
3005 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 }
3007
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003009 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003010 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 signed long long cl;
3012
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003014 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003015 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003016 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003019 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003021 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003025 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003026 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003028 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003029 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003030 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003031 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003033 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003034 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003035 }
3036
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003037 /* bodyless requests have a known length */
3038 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003039 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003040
Willy Tarreau179085c2014-04-28 16:48:56 +02003041 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3042 * only change if both the request and the config reference something else.
3043 * Option httpclose by itself sets tunnel mode where headers are mangled.
3044 * However, if another mode is set, it will affect it (eg: server-close/
3045 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3046 * if FE and BE have the same settings (common). The method consists in
3047 * checking if options changed between the two calls (implying that either
3048 * one is non-null, or one of them is non-null and we are there for the first
3049 * time.
3050 */
3051 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003052 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3053 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003054
Willy Tarreaud787e662009-07-07 10:14:51 +02003055 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003056 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003057 req->analyse_exp = TICK_ETERNITY;
3058 return 1;
3059
3060 return_bad_req:
3061 /* We centralize bad requests processing here */
3062 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3063 /* we detected a parsing error. We want to archive this request
3064 * in the dedicated proxy area for later troubleshooting.
3065 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003066 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003067 }
3068
3069 txn->req.msg_state = HTTP_MSG_ERROR;
3070 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003071 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003073 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003074 if (s->listener->counters)
3075 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003076
3077 return_prx_cond:
3078 if (!(s->flags & SN_ERR_MASK))
3079 s->flags |= SN_ERR_PRXCOND;
3080 if (!(s->flags & SN_FINST_MASK))
3081 s->flags |= SN_FINST_R;
3082
3083 req->analysers = 0;
3084 req->analyse_exp = TICK_ETERNITY;
3085 return 0;
3086}
3087
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003088
Willy Tarreau347a35d2013-11-22 17:51:09 +01003089/* This function prepares an applet to handle the stats. It can deal with the
3090 * "100-continue" expectation, check that admin rules are met for POST requests,
3091 * and program a response message if something was unexpected. It cannot fail
3092 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003093 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 * s->target which is supposed to already point to the stats applet. The caller
3095 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003096 */
3097int http_handle_stats(struct session *s, struct channel *req)
3098{
3099 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003100 struct stream_interface *si = &s->si[1];
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101 struct http_txn *txn = &s->txn;
3102 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103 struct uri_auth *uri_auth = s->be->uri_auth;
3104 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 appctx = si_appctx(si);
3108 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3109 appctx->st1 = appctx->st2 = 0;
3110 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3111 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003112 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3113 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003114
3115 uri = msg->chn->buf->p + msg->sl.rq.u;
3116 lookup = uri + uri_auth->uri_len;
3117
3118 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3119 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003120 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 break;
3122 }
3123 }
3124
3125 if (uri_auth->refresh) {
3126 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3127 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003128 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003129 break;
3130 }
3131 }
3132 }
3133
3134 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3135 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003137 break;
3138 }
3139 }
3140
3141 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3142 if (memcmp(h, ";st=", 4) == 0) {
3143 int i;
3144 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3147 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 break;
3150 }
3151 }
3152 break;
3153 }
3154 }
3155
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003156 appctx->ctx.stats.scope_str = 0;
3157 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003158 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3159 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3160 int itx = 0;
3161 const char *h2;
3162 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3163 const char *err;
3164
3165 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3166 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003167 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003168 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3169 itx++;
3170 h++;
3171 }
3172
3173 if (itx > STAT_SCOPE_TXT_MAXLEN)
3174 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003177 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003178 memcpy(scope_txt, h2, itx);
3179 scope_txt[itx] = '\0';
3180 err = invalid_char(scope_txt);
3181 if (err) {
3182 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003183 appctx->ctx.stats.scope_str = 0;
3184 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003185 }
3186 break;
3187 }
3188 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189
3190 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003191 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003192 int ret = 1;
3193
3194 if (stats_admin_rule->cond) {
3195 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3196 ret = acl_pass(ret);
3197 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3198 ret = !ret;
3199 }
3200
3201 if (ret) {
3202 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003204 break;
3205 }
3206 }
3207
3208 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003209 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003211 /* we'll need the request body, possibly after sending 100-continue */
3212 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003214 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003215 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3217 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003218 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003219 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003220 else {
3221 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003222 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003223 }
3224
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003225 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003226 return 1;
3227}
3228
Lukas Tribus67db8df2013-06-23 17:37:13 +02003229/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3230 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3231 */
3232static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3233{
3234#ifdef IP_TOS
3235 if (from.ss_family == AF_INET)
3236 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3237#endif
3238#ifdef IPV6_TCLASS
3239 if (from.ss_family == AF_INET6) {
3240 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3241 /* v4-mapped addresses need IP_TOS */
3242 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3243 else
3244 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3245 }
3246#endif
3247}
3248
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003249static int http_transform_header(struct session* s, struct http_msg *msg,
3250 const char* name, unsigned int name_len,
3251 struct list *fmt, struct my_regex *re,
3252 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003253{
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003254 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3255 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003256 struct hdr_ctx ctx;
3257 char *buf = msg->chn->buf->p;
3258 struct hdr_idx *idx = &s->txn.hdr_idx;
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003259 struct chunk *replace = get_trash_chunk();
3260 struct chunk *output = get_trash_chunk();
3261
3262 replace->len = build_logline(s, replace->str, replace->size, fmt);
3263 if (replace->len >= replace->size - 1)
3264 return -1;
3265
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003266 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003267
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003268 /* Choose the header browsing function. */
3269 switch (action) {
3270 case HTTP_REQ_ACT_REPLACE_VAL:
3271 case HTTP_RES_ACT_REPLACE_VAL:
3272 http_find_hdr_func = http_find_header2;
3273 break;
3274 case HTTP_REQ_ACT_REPLACE_HDR:
3275 case HTTP_RES_ACT_REPLACE_HDR:
3276 http_find_hdr_func = http_find_full_header2;
3277 break;
3278 default: /* impossible */
3279 return -1;
3280 }
3281
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003282 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3283 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003284 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003285 char *val = ctx.line + ctx.val;
3286 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003287
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003288 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3289 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003290
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003291 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3292 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003293 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003294
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003295 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003296
3297 hdr->len += delta;
3298 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003299
3300 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003301 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003302 }
3303
3304 return 0;
3305}
3306
Willy Tarreau20b0de52012-12-24 15:45:22 +01003307/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003308 * transaction <txn>. Returns the verdict of the first rule that prevents
3309 * further processing of the request (auth, deny, ...), and defaults to
3310 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3311 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3312 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003313 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003314enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003315http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003316{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003317 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003318 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003319 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003320 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003322 /* If "the current_rule_list" match the executed rule list, we are in
3323 * resume condition. If a resume is needed it is always in the action
3324 * and never in the ACL or converters. In this case, we initialise the
3325 * current rule, and go to the action execution point.
3326 */
3327 if (s->current_rule_list == rules) {
3328 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3329 goto resume_execution;
3330 }
3331 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003332 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003333 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003334 continue;
3335
Willy Tarreau96257ec2012-12-27 10:46:37 +01003336 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003337 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003338 int ret;
3339
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003340 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003341 ret = acl_pass(ret);
3342
Willy Tarreauff011f22011-01-06 17:51:27 +01003343 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003344 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003345
3346 if (!ret) /* condition not matched */
3347 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003348 }
3349
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003350resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003351
Willy Tarreau96257ec2012-12-27 10:46:37 +01003352 switch (rule->action) {
3353 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003354 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003355
3356 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003357 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003358
Willy Tarreauccbcc372012-12-27 12:37:57 +01003359 case HTTP_REQ_ACT_TARPIT:
3360 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003361 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003362
Willy Tarreau96257ec2012-12-27 10:46:37 +01003363 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003364 /* Auth might be performed on regular http-req rules as well as on stats */
3365 auth_realm = rule->arg.auth.realm;
3366 if (!auth_realm) {
3367 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3368 auth_realm = STATS_DEFAULT_REALM;
3369 else
3370 auth_realm = px->id;
3371 }
3372 /* send 401/407 depending on whether we use a proxy or not. We still
3373 * count one error, because normal browsing won't significantly
3374 * increase the counter but brute force attempts will.
3375 */
3376 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3377 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3378 stream_int_retnclose(&s->si[0], &trash);
3379 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003380 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003381
Willy Tarreau81499eb2012-12-27 12:19:02 +01003382 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003383 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3384 return HTTP_RULE_RES_BADREQ;
3385 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003386
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003387 case HTTP_REQ_ACT_SET_NICE:
3388 s->task->nice = rule->arg.nice;
3389 break;
3390
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003391 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003392 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003393 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003394 break;
3395
Willy Tarreau51347ed2013-06-11 19:34:13 +02003396 case HTTP_REQ_ACT_SET_MARK:
3397#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003398 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003399 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003400#endif
3401 break;
3402
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003403 case HTTP_REQ_ACT_SET_LOGL:
3404 s->logs.level = rule->arg.loglevel;
3405 break;
3406
Sasha Pachev218f0642014-06-16 12:05:59 -06003407 case HTTP_REQ_ACT_REPLACE_HDR:
3408 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003409 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3410 rule->arg.hdr_add.name_len,
3411 &rule->arg.hdr_add.fmt,
3412 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003413 return HTTP_RULE_RES_BADREQ;
3414 break;
3415
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003416 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003417 ctx.idx = 0;
3418 /* remove all occurrences of the header */
3419 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3420 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3421 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003422 }
Willy Tarreau85603282015-01-21 20:39:27 +01003423 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003424
Willy Tarreau85603282015-01-21 20:39:27 +01003425 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003426 case HTTP_REQ_ACT_ADD_HDR:
3427 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3428 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3429 trash.len = rule->arg.hdr_add.name_len;
3430 trash.str[trash.len++] = ':';
3431 trash.str[trash.len++] = ' ';
3432 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003433
3434 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3435 /* remove all occurrences of the header */
3436 ctx.idx = 0;
3437 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3438 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3439 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3440 }
3441 }
3442
Willy Tarreau96257ec2012-12-27 10:46:37 +01003443 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3444 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003445
3446 case HTTP_REQ_ACT_DEL_ACL:
3447 case HTTP_REQ_ACT_DEL_MAP: {
3448 struct pat_ref *ref;
3449 char *key;
3450 int len;
3451
3452 /* collect reference */
3453 ref = pat_ref_lookup(rule->arg.map.ref);
3454 if (!ref)
3455 continue;
3456
3457 /* collect key */
3458 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3459 key = trash.str;
3460 key[len] = '\0';
3461
3462 /* perform update */
3463 /* returned code: 1=ok, 0=ko */
3464 pat_ref_delete(ref, key);
3465
3466 break;
3467 }
3468
3469 case HTTP_REQ_ACT_ADD_ACL: {
3470 struct pat_ref *ref;
3471 char *key;
3472 struct chunk *trash_key;
3473 int len;
3474
3475 trash_key = get_trash_chunk();
3476
3477 /* collect reference */
3478 ref = pat_ref_lookup(rule->arg.map.ref);
3479 if (!ref)
3480 continue;
3481
3482 /* collect key */
3483 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3484 key = trash_key->str;
3485 key[len] = '\0';
3486
3487 /* perform update */
3488 /* add entry only if it does not already exist */
3489 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003490 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003491
3492 break;
3493 }
3494
3495 case HTTP_REQ_ACT_SET_MAP: {
3496 struct pat_ref *ref;
3497 char *key, *value;
3498 struct chunk *trash_key, *trash_value;
3499 int len;
3500
3501 trash_key = get_trash_chunk();
3502 trash_value = get_trash_chunk();
3503
3504 /* collect reference */
3505 ref = pat_ref_lookup(rule->arg.map.ref);
3506 if (!ref)
3507 continue;
3508
3509 /* collect key */
3510 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3511 key = trash_key->str;
3512 key[len] = '\0';
3513
3514 /* collect value */
3515 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3516 value = trash_value->str;
3517 value[len] = '\0';
3518
3519 /* perform update */
3520 if (pat_ref_find_elt(ref, key) != NULL)
3521 /* update entry if it exists */
3522 pat_ref_set(ref, key, value, NULL);
3523 else
3524 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003525 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003526
3527 break;
3528 }
William Lallemand73025dd2014-04-24 14:38:37 +02003529
3530 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003531 if (!rule->action_ptr(rule, px, s, txn)) {
3532 s->current_rule = &rule->list;
3533 return HTTP_RULE_RES_YIELD;
3534 }
William Lallemand73025dd2014-04-24 14:38:37 +02003535 break;
3536
3537 case HTTP_REQ_ACT_CUSTOM_STOP:
3538 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003539 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003540
3541 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3542 /* Note: only the first valid tracking parameter of each
3543 * applies.
3544 */
3545
3546 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3547 struct stktable *t;
3548 struct stksess *ts;
3549 struct stktable_key *key;
3550 void *ptr;
3551
3552 t = rule->act_prm.trk_ctr.table.t;
3553 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3554
3555 if (key && (ts = stktable_get_entry(t, key))) {
3556 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3557
3558 /* let's count a new HTTP request as it's the first time we do it */
3559 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3560 if (ptr)
3561 stktable_data_cast(ptr, http_req_cnt)++;
3562
3563 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3564 if (ptr)
3565 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3566 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3567
3568 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3569 if (s->fe != s->be)
3570 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3571 }
3572 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003573 }
3574 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003575
3576 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003577 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003578}
3579
Willy Tarreau71241ab2012-12-27 11:30:54 +01003580
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003581/* Executes the http-response rules <rules> for session <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003582 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3583 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3584 * evaluation of next rule list. If *STOP is returned, the process must stop
3585 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3586 * rule. If *YIELD is returned, the czller must call again the function with
3587 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003588 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003589static enum rule_result
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003590http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3591{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003592 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003593 struct http_res_rule *rule;
3594 struct hdr_ctx ctx;
3595
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003596 /* If "the current_rule_list" match the executed rule list, we are in
3597 * resume condition. If a resume is needed it is always in the action
3598 * and never in the ACL or converters. In this case, we initialise the
3599 * current rule, and go to the action execution point.
3600 */
3601 if (s->current_rule_list == rules) {
3602 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3603 goto resume_execution;
3604 }
3605 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003606 list_for_each_entry(rule, rules, list) {
3607 if (rule->action >= HTTP_RES_ACT_MAX)
3608 continue;
3609
3610 /* check optional condition */
3611 if (rule->cond) {
3612 int ret;
3613
3614 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3615 ret = acl_pass(ret);
3616
3617 if (rule->cond->pol == ACL_COND_UNLESS)
3618 ret = !ret;
3619
3620 if (!ret) /* condition not matched */
3621 continue;
3622 }
3623
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003624resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003625
3626 switch (rule->action) {
3627 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003628 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003629
3630 case HTTP_RES_ACT_DENY:
3631 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003632 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003633
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003634 case HTTP_RES_ACT_SET_NICE:
3635 s->task->nice = rule->arg.nice;
3636 break;
3637
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003638 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003639 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003640 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003641 break;
3642
Willy Tarreau51347ed2013-06-11 19:34:13 +02003643 case HTTP_RES_ACT_SET_MARK:
3644#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003645 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003646 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003647#endif
3648 break;
3649
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003650 case HTTP_RES_ACT_SET_LOGL:
3651 s->logs.level = rule->arg.loglevel;
3652 break;
3653
Sasha Pachev218f0642014-06-16 12:05:59 -06003654 case HTTP_RES_ACT_REPLACE_HDR:
3655 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003656 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3657 rule->arg.hdr_add.name_len,
3658 &rule->arg.hdr_add.fmt,
3659 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003660 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003661 break;
3662
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003663 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003664 ctx.idx = 0;
3665 /* remove all occurrences of the header */
3666 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3667 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3668 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3669 }
Willy Tarreau85603282015-01-21 20:39:27 +01003670 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003671
Willy Tarreau85603282015-01-21 20:39:27 +01003672 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003673 case HTTP_RES_ACT_ADD_HDR:
3674 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3675 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3676 trash.len = rule->arg.hdr_add.name_len;
3677 trash.str[trash.len++] = ':';
3678 trash.str[trash.len++] = ' ';
3679 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003680
3681 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3682 /* remove all occurrences of the header */
3683 ctx.idx = 0;
3684 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3685 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3686 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3687 }
3688 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003689 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3690 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003691
3692 case HTTP_RES_ACT_DEL_ACL:
3693 case HTTP_RES_ACT_DEL_MAP: {
3694 struct pat_ref *ref;
3695 char *key;
3696 int len;
3697
3698 /* collect reference */
3699 ref = pat_ref_lookup(rule->arg.map.ref);
3700 if (!ref)
3701 continue;
3702
3703 /* collect key */
3704 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3705 key = trash.str;
3706 key[len] = '\0';
3707
3708 /* perform update */
3709 /* returned code: 1=ok, 0=ko */
3710 pat_ref_delete(ref, key);
3711
3712 break;
3713 }
3714
3715 case HTTP_RES_ACT_ADD_ACL: {
3716 struct pat_ref *ref;
3717 char *key;
3718 struct chunk *trash_key;
3719 int len;
3720
3721 trash_key = get_trash_chunk();
3722
3723 /* collect reference */
3724 ref = pat_ref_lookup(rule->arg.map.ref);
3725 if (!ref)
3726 continue;
3727
3728 /* collect key */
3729 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3730 key = trash_key->str;
3731 key[len] = '\0';
3732
3733 /* perform update */
3734 /* check if the entry already exists */
3735 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003736 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003737
3738 break;
3739 }
3740
3741 case HTTP_RES_ACT_SET_MAP: {
3742 struct pat_ref *ref;
3743 char *key, *value;
3744 struct chunk *trash_key, *trash_value;
3745 int len;
3746
3747 trash_key = get_trash_chunk();
3748 trash_value = get_trash_chunk();
3749
3750 /* collect reference */
3751 ref = pat_ref_lookup(rule->arg.map.ref);
3752 if (!ref)
3753 continue;
3754
3755 /* collect key */
3756 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3757 key = trash_key->str;
3758 key[len] = '\0';
3759
3760 /* collect value */
3761 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3762 value = trash_value->str;
3763 value[len] = '\0';
3764
3765 /* perform update */
3766 if (pat_ref_find_elt(ref, key) != NULL)
3767 /* update entry if it exists */
3768 pat_ref_set(ref, key, value, NULL);
3769 else
3770 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003771 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003772
3773 break;
3774 }
William Lallemand73025dd2014-04-24 14:38:37 +02003775
3776 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003777 if (!rule->action_ptr(rule, px, s, txn)) {
3778 s->current_rule = &rule->list;
3779 return HTTP_RULE_RES_YIELD;
3780 }
William Lallemand73025dd2014-04-24 14:38:37 +02003781 break;
3782
3783 case HTTP_RES_ACT_CUSTOM_STOP:
3784 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003785 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003786 }
3787 }
3788
3789 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003790 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003791}
3792
3793
Willy Tarreau71241ab2012-12-27 11:30:54 +01003794/* Perform an HTTP redirect based on the information in <rule>. The function
3795 * returns non-zero on success, or zero in case of a, irrecoverable error such
3796 * as too large a request to build a valid response.
3797 */
3798static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3799{
3800 struct http_msg *msg = &txn->req;
3801 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003802 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003803
3804 /* build redirect message */
3805 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003806 case 308:
3807 msg_fmt = HTTP_308;
3808 break;
3809 case 307:
3810 msg_fmt = HTTP_307;
3811 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003812 case 303:
3813 msg_fmt = HTTP_303;
3814 break;
3815 case 301:
3816 msg_fmt = HTTP_301;
3817 break;
3818 case 302:
3819 default:
3820 msg_fmt = HTTP_302;
3821 break;
3822 }
3823
3824 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3825 return 0;
3826
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003827 location = trash.str + trash.len;
3828
Willy Tarreau71241ab2012-12-27 11:30:54 +01003829 switch(rule->type) {
3830 case REDIRECT_TYPE_SCHEME: {
3831 const char *path;
3832 const char *host;
3833 struct hdr_ctx ctx;
3834 int pathlen;
3835 int hostlen;
3836
3837 host = "";
3838 hostlen = 0;
3839 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003840 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003841 host = ctx.line + ctx.val;
3842 hostlen = ctx.vlen;
3843 }
3844
3845 path = http_get_path(txn);
3846 /* build message using path */
3847 if (path) {
3848 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3849 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3850 int qs = 0;
3851 while (qs < pathlen) {
3852 if (path[qs] == '?') {
3853 pathlen = qs;
3854 break;
3855 }
3856 qs++;
3857 }
3858 }
3859 } else {
3860 path = "/";
3861 pathlen = 1;
3862 }
3863
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003864 if (rule->rdr_str) { /* this is an old "redirect" rule */
3865 /* check if we can add scheme + "://" + host + path */
3866 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3867 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003868
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003869 /* add scheme */
3870 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3871 trash.len += rule->rdr_len;
3872 }
3873 else {
3874 /* add scheme with executing log format */
3875 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003876
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003877 /* check if we can add scheme + "://" + host + path */
3878 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3879 return 0;
3880 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003881 /* add "://" */
3882 memcpy(trash.str + trash.len, "://", 3);
3883 trash.len += 3;
3884
3885 /* add host */
3886 memcpy(trash.str + trash.len, host, hostlen);
3887 trash.len += hostlen;
3888
3889 /* add path */
3890 memcpy(trash.str + trash.len, path, pathlen);
3891 trash.len += pathlen;
3892
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003893 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003894 if (trash.len && trash.str[trash.len - 1] != '/' &&
3895 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3896 if (trash.len > trash.size - 5)
3897 return 0;
3898 trash.str[trash.len] = '/';
3899 trash.len++;
3900 }
3901
3902 break;
3903 }
3904 case REDIRECT_TYPE_PREFIX: {
3905 const char *path;
3906 int pathlen;
3907
3908 path = http_get_path(txn);
3909 /* build message using path */
3910 if (path) {
3911 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3912 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3913 int qs = 0;
3914 while (qs < pathlen) {
3915 if (path[qs] == '?') {
3916 pathlen = qs;
3917 break;
3918 }
3919 qs++;
3920 }
3921 }
3922 } else {
3923 path = "/";
3924 pathlen = 1;
3925 }
3926
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003927 if (rule->rdr_str) { /* this is an old "redirect" rule */
3928 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3929 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003930
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003931 /* add prefix. Note that if prefix == "/", we don't want to
3932 * add anything, otherwise it makes it hard for the user to
3933 * configure a self-redirection.
3934 */
3935 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3936 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3937 trash.len += rule->rdr_len;
3938 }
3939 }
3940 else {
3941 /* add prefix with executing log format */
3942 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3943
3944 /* Check length */
3945 if (trash.len + pathlen > trash.size - 4)
3946 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003947 }
3948
3949 /* add path */
3950 memcpy(trash.str + trash.len, path, pathlen);
3951 trash.len += pathlen;
3952
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003953 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003954 if (trash.len && trash.str[trash.len - 1] != '/' &&
3955 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3956 if (trash.len > trash.size - 5)
3957 return 0;
3958 trash.str[trash.len] = '/';
3959 trash.len++;
3960 }
3961
3962 break;
3963 }
3964 case REDIRECT_TYPE_LOCATION:
3965 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003966 if (rule->rdr_str) { /* this is an old "redirect" rule */
3967 if (trash.len + rule->rdr_len > trash.size - 4)
3968 return 0;
3969
3970 /* add location */
3971 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3972 trash.len += rule->rdr_len;
3973 }
3974 else {
3975 /* add location with executing log format */
3976 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003977
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003978 /* Check left length */
3979 if (trash.len > trash.size - 4)
3980 return 0;
3981 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003982 break;
3983 }
3984
3985 if (rule->cookie_len) {
3986 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3987 trash.len += 14;
3988 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3989 trash.len += rule->cookie_len;
3990 memcpy(trash.str + trash.len, "\r\n", 2);
3991 trash.len += 2;
3992 }
3993
3994 /* add end of headers and the keep-alive/close status.
3995 * We may choose to set keep-alive if the Location begins
3996 * with a slash, because the client will come back to the
3997 * same server.
3998 */
3999 txn->status = rule->code;
4000 /* let's log the request time */
4001 s->logs.tv_request = now;
4002
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004003 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004004 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4005 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4006 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4007 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4008 /* keep-alive possible */
4009 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4010 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4011 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4012 trash.len += 30;
4013 } else {
4014 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4015 trash.len += 24;
4016 }
4017 }
4018 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4019 trash.len += 4;
4020 bo_inject(txn->rsp.chn, trash.str, trash.len);
4021 /* "eat" the request */
4022 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004023 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004024 msg->sov = 0;
4025 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004026 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027 txn->req.msg_state = HTTP_MSG_CLOSED;
4028 txn->rsp.msg_state = HTTP_MSG_DONE;
4029 } else {
4030 /* keep-alive not possible */
4031 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4032 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4033 trash.len += 29;
4034 } else {
4035 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4036 trash.len += 23;
4037 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004038 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004039 txn->req.chn->analysers = 0;
4040 }
4041
4042 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004043 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044 if (!(s->flags & SN_FINST_MASK))
4045 s->flags |= SN_FINST_R;
4046
4047 return 1;
4048}
4049
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004050/* This stream analyser runs all HTTP request processing which is common to
4051 * frontends and backends, which means blocking ACLs, filters, connection-close,
4052 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004053 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004054 * either needs more data or wants to immediately abort the request (eg: deny,
4055 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004056 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004057int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004058{
Willy Tarreaud787e662009-07-07 10:14:51 +02004059 struct http_txn *txn = &s->txn;
4060 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004061 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004062 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004063 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004064
Willy Tarreau655dce92009-11-08 13:10:58 +01004065 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004066 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004067 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004068 }
4069
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004070 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 +02004071 now_ms, __FUNCTION__,
4072 s,
4073 req,
4074 req->rex, req->wex,
4075 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004076 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004077 req->analysers);
4078
Willy Tarreau65410832014-04-28 21:25:43 +02004079 /* just in case we have some per-backend tracking */
4080 session_inc_be_http_req_ctr(s);
4081
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004082 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004083 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4084 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004085
Willy Tarreau0b748332014-04-29 00:13:29 +02004086 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004087 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4088 goto return_prx_yield;
4089
Willy Tarreau0b748332014-04-29 00:13:29 +02004090 case HTTP_RULE_RES_CONT:
4091 case HTTP_RULE_RES_STOP: /* nothing to do */
4092 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004093
Willy Tarreau0b748332014-04-29 00:13:29 +02004094 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4095 if (txn->flags & TX_CLTARPIT)
4096 goto tarpit;
4097 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004098
Willy Tarreau0b748332014-04-29 00:13:29 +02004099 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4100 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004101
Willy Tarreau0b748332014-04-29 00:13:29 +02004102 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004103 goto done;
4104
Willy Tarreau0b748332014-04-29 00:13:29 +02004105 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4106 goto return_bad_req;
4107 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004108 }
4109
Willy Tarreau52542592014-04-28 18:33:26 +02004110 /* OK at this stage, we know that the request was accepted according to
4111 * the http-request rules, we can check for the stats. Note that the
4112 * URI is detected *before* the req* rules in order not to be affected
4113 * by a possible reqrep, while they are processed *after* so that a
4114 * reqdeny can still block them. This clearly needs to change in 1.6!
4115 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004116 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004117 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004118 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004119 txn->status = 500;
4120 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004121 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004122
Willy Tarreau52542592014-04-28 18:33:26 +02004123 if (!(s->flags & SN_ERR_MASK))
4124 s->flags |= SN_ERR_RESOURCE;
4125 goto return_prx_cond;
4126 }
4127
4128 /* parse the whole stats request and extract the relevant information */
4129 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004130 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4131 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004132
Willy Tarreau0b748332014-04-29 00:13:29 +02004133 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4134 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004135
Willy Tarreau0b748332014-04-29 00:13:29 +02004136 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4137 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004138 }
4139
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004140 /* evaluate the req* rules except reqadd */
4141 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004142 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004143 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004144
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004145 if (txn->flags & TX_CLDENY)
4146 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004147
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004148 if (txn->flags & TX_CLTARPIT)
4149 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004150 }
Willy Tarreau06619262006-12-17 08:37:22 +01004151
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004152 /* add request headers from the rule sets in the same order */
4153 list_for_each_entry(wl, &px->req_add, list) {
4154 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004155 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004156 ret = acl_pass(ret);
4157 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4158 ret = !ret;
4159 if (!ret)
4160 continue;
4161 }
4162
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004163 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004164 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004165 }
4166
Willy Tarreau52542592014-04-28 18:33:26 +02004167
4168 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004169 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004170 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004171 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4172 s->fe->fe_counters.intercepted_req++;
4173
4174 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4175 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4176 if (!(s->flags & SN_FINST_MASK))
4177 s->flags |= SN_FINST_R;
4178
Willy Tarreau70730dd2014-04-24 18:06:27 +02004179 /* we may want to compress the stats page */
4180 if (s->fe->comp || s->be->comp)
4181 select_compression_request_header(s, req->buf);
4182
4183 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004184 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004185 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004186 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004187
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004188 /* check whether we have some ACLs set to redirect this request */
4189 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004190 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191 int ret;
4192
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004193 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004194 ret = acl_pass(ret);
4195 if (rule->cond->pol == ACL_COND_UNLESS)
4196 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004197 if (!ret)
4198 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004199 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004200 if (!http_apply_redirect_rule(rule, s, txn))
4201 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004202 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004204
Willy Tarreau2be39392010-01-03 17:24:51 +01004205 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4206 * If this happens, then the data will not come immediately, so we must
4207 * send all what we have without waiting. Note that due to the small gain
4208 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004209 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004210 * itself once used.
4211 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004212 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004213
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004214 done: /* done with this analyser, continue with next ones that the calling
4215 * points will have set, if any.
4216 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004217 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004218 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4219 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004220 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004221
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004222 tarpit:
4223 /* When a connection is tarpitted, we use the tarpit timeout,
4224 * which may be the same as the connect timeout if unspecified.
4225 * If unset, then set it to zero because we really want it to
4226 * eventually expire. We build the tarpit as an analyser.
4227 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004228 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004229
4230 /* wipe the request out so that we can drop the connection early
4231 * if the client closes first.
4232 */
4233 channel_dont_connect(req);
4234 req->analysers = 0; /* remove switching rules etc... */
4235 req->analysers |= AN_REQ_HTTP_TARPIT;
4236 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4237 if (!req->analyse_exp)
4238 req->analyse_exp = tick_add(now_ms, 0);
4239 session_inc_http_err_ctr(s);
4240 s->fe->fe_counters.denied_req++;
4241 if (s->fe != s->be)
4242 s->be->be_counters.denied_req++;
4243 if (s->listener->counters)
4244 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004245 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004246
4247 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004248 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004249 txn->status = 403;
4250 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004251 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004252 session_inc_http_err_ctr(s);
4253 s->fe->fe_counters.denied_req++;
4254 if (s->fe != s->be)
4255 s->be->be_counters.denied_req++;
4256 if (s->listener->counters)
4257 s->listener->counters->denied_req++;
4258 goto return_prx_cond;
4259
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004260 return_bad_req:
4261 /* We centralize bad requests processing here */
4262 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4263 /* we detected a parsing error. We want to archive this request
4264 * in the dedicated proxy area for later troubleshooting.
4265 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004266 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004267 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004268
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004269 txn->req.msg_state = HTTP_MSG_ERROR;
4270 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004271 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004272
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004273 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004274 if (s->listener->counters)
4275 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004277 return_prx_cond:
4278 if (!(s->flags & SN_ERR_MASK))
4279 s->flags |= SN_ERR_PRXCOND;
4280 if (!(s->flags & SN_FINST_MASK))
4281 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004282
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004283 req->analysers = 0;
4284 req->analyse_exp = TICK_ETERNITY;
4285 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004286
4287 return_prx_yield:
4288 channel_dont_connect(req);
4289 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292/* This function performs all the processing enabled for the current request.
4293 * It returns 1 if the processing can continue on next analysers, or zero if it
4294 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004295 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004296 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004297int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004298{
4299 struct http_txn *txn = &s->txn;
4300 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004301 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004302
Willy Tarreau655dce92009-11-08 13:10:58 +01004303 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004304 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004305 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004306 return 0;
4307 }
4308
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004309 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 +02004310 now_ms, __FUNCTION__,
4311 s,
4312 req,
4313 req->rex, req->wex,
4314 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004315 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004317
William Lallemand82fe75c2012-10-23 10:25:10 +02004318 if (s->fe->comp || s->be->comp)
4319 select_compression_request_header(s, req->buf);
4320
Willy Tarreau59234e92008-11-30 23:51:27 +01004321 /*
4322 * Right now, we know that we have processed the entire headers
4323 * and that unwanted requests have been filtered out. We can do
4324 * whatever we want with the remaining request. Also, now we
4325 * may have separate values for ->fe, ->be.
4326 */
Willy Tarreau06619262006-12-17 08:37:22 +01004327
Willy Tarreau59234e92008-11-30 23:51:27 +01004328 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004329 * If HTTP PROXY is set we simply get remote server address parsing
4330 * incoming request. Note that this requires that a connection is
4331 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004332 */
4333 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004334 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004335 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004336
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004337 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004338 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004339 txn->req.msg_state = HTTP_MSG_ERROR;
4340 txn->status = 500;
4341 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004342 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004343
4344 if (!(s->flags & SN_ERR_MASK))
4345 s->flags |= SN_ERR_RESOURCE;
4346 if (!(s->flags & SN_FINST_MASK))
4347 s->flags |= SN_FINST_R;
4348
4349 return 0;
4350 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004351
4352 path = http_get_path(txn);
4353 url2sa(req->buf->p + msg->sl.rq.u,
4354 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004355 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004356 /* if the path was found, we have to remove everything between
4357 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4358 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4359 * u_l characters by a single "/".
4360 */
4361 if (path) {
4362 char *cur_ptr = req->buf->p;
4363 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4364 int delta;
4365
4366 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4367 http_msg_move_end(&txn->req, delta);
4368 cur_end += delta;
4369 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4370 goto return_bad_req;
4371 }
4372 else {
4373 char *cur_ptr = req->buf->p;
4374 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4375 int delta;
4376
4377 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4378 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4379 http_msg_move_end(&txn->req, delta);
4380 cur_end += delta;
4381 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4382 goto return_bad_req;
4383 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004384 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004385
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004387 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004388 * Note that doing so might move headers in the request, but
4389 * the fields will stay coherent and the URI will not move.
4390 * This should only be performed in the backend.
4391 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004392 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004393 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4394 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004395
Willy Tarreau59234e92008-11-30 23:51:27 +01004396 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004397 * 8: the appsession cookie was looked up very early in 1.2,
4398 * so let's do the same now.
4399 */
4400
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004401 /* It needs to look into the URI unless persistence must be ignored */
4402 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004403 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 +01004404 }
4405
William Lallemanda73203e2012-03-12 12:48:57 +01004406 /* add unique-id if "header-unique-id" is specified */
4407
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004408 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4409 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4410 goto return_bad_req;
4411 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004412 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004413 }
William Lallemanda73203e2012-03-12 12:48:57 +01004414
4415 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004416 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4417 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004418 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004419 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004420 goto return_bad_req;
4421 }
4422
Cyril Bontéb21570a2009-11-29 20:04:48 +01004423 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004424 * 9: add X-Forwarded-For if either the frontend or the backend
4425 * asks for it.
4426 */
4427 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004428 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004429 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004430 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4431 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004432 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004433 /* The header is set to be added only if none is present
4434 * and we found it, so don't do anything.
4435 */
4436 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004437 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004438 /* Add an X-Forwarded-For header unless the source IP is
4439 * in the 'except' network range.
4440 */
4441 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004442 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004443 != s->fe->except_net.s_addr) &&
4444 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004445 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004446 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004447 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004448 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004449 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004450
4451 /* Note: we rely on the backend to get the header name to be used for
4452 * x-forwarded-for, because the header is really meant for the backends.
4453 * However, if the backend did not specify any option, we have to rely
4454 * on the frontend's header name.
4455 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004456 if (s->be->fwdfor_hdr_len) {
4457 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004458 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004459 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004460 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004461 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004462 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004463 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004464
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004465 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004466 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004467 }
4468 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004469 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004470 /* FIXME: for the sake of completeness, we should also support
4471 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004472 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 int len;
4474 char pn[INET6_ADDRSTRLEN];
4475 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004476 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004477 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004478
Willy Tarreau59234e92008-11-30 23:51:27 +01004479 /* Note: we rely on the backend to get the header name to be used for
4480 * x-forwarded-for, because the header is really meant for the backends.
4481 * However, if the backend did not specify any option, we have to rely
4482 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004483 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004484 if (s->be->fwdfor_hdr_len) {
4485 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 } else {
4488 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004489 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004490 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004491 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004492
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004493 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 goto return_bad_req;
4495 }
4496 }
4497
4498 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004499 * 10: add X-Original-To if either the frontend or the backend
4500 * asks for it.
4501 */
4502 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4503
4504 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004505 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004506 /* Add an X-Original-To header unless the destination IP is
4507 * in the 'except' network range.
4508 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004509 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004510
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004511 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004512 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004513 (((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 +02004514 != s->fe->except_to.s_addr) &&
4515 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004516 (((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 +02004517 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004518 int len;
4519 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004520 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004521
4522 /* Note: we rely on the backend to get the header name to be used for
4523 * x-original-to, because the header is really meant for the backends.
4524 * However, if the backend did not specify any option, we have to rely
4525 * on the frontend's header name.
4526 */
4527 if (s->be->orgto_hdr_len) {
4528 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004529 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004530 } else {
4531 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004532 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004533 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004534 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004535
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004536 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004537 goto return_bad_req;
4538 }
4539 }
4540 }
4541
Willy Tarreau50fc7772012-11-11 22:19:57 +01004542 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4543 * If an "Upgrade" token is found, the header is left untouched in order not to have
4544 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4545 * "Upgrade" is present in the Connection header.
4546 */
4547 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4548 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004549 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4550 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004551 unsigned int want_flags = 0;
4552
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004553 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004554 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004555 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4556 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004557 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004558 want_flags |= TX_CON_CLO_SET;
4559 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004560 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004561 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4562 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004563 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004564 want_flags |= TX_CON_KAL_SET;
4565 }
4566
4567 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004568 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004569 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004570
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004571
Willy Tarreau522d6c02009-12-06 18:49:18 +01004572 /* If we have no server assigned yet and we're balancing on url_param
4573 * with a POST request, we may be interested in checking the body for
4574 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004575 */
4576 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4577 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004578 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004579 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004580 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004582
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004583 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004584 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004585#ifdef TCP_QUICKACK
4586 /* We expect some data from the client. Unless we know for sure
4587 * we already have a full request, we have to re-enable quick-ack
4588 * in case we previously disabled it, otherwise we might cause
4589 * the client to delay further data.
4590 */
4591 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004592 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004593 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004594 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004595 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004596#endif
4597 }
Willy Tarreau03945942009-12-22 16:50:27 +01004598
Willy Tarreau59234e92008-11-30 23:51:27 +01004599 /*************************************************************
4600 * OK, that's finished for the headers. We have done what we *
4601 * could. Let's switch to the DATA state. *
4602 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004603 req->analyse_exp = TICK_ETERNITY;
4604 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004605
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004606 /* if the server closes the connection, we want to immediately react
4607 * and close the socket to save packets and syscalls.
4608 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004609 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004610 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004611
Willy Tarreau59234e92008-11-30 23:51:27 +01004612 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004613 /* OK let's go on with the BODY now */
4614 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004615
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004617 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004618 /* we detected a parsing error. We want to archive this request
4619 * in the dedicated proxy area for later troubleshooting.
4620 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004621 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004622 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004623
Willy Tarreau59234e92008-11-30 23:51:27 +01004624 txn->req.msg_state = HTTP_MSG_ERROR;
4625 txn->status = 400;
4626 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004627 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004628
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004629 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004630 if (s->listener->counters)
4631 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004632
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 if (!(s->flags & SN_ERR_MASK))
4634 s->flags |= SN_ERR_PRXCOND;
4635 if (!(s->flags & SN_FINST_MASK))
4636 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004637 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004638}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004639
Willy Tarreau60b85b02008-11-30 23:28:40 +01004640/* This function is an analyser which processes the HTTP tarpit. It always
4641 * returns zero, at the beginning because it prevents any other processing
4642 * from occurring, and at the end because it terminates the request.
4643 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004644int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004645{
4646 struct http_txn *txn = &s->txn;
4647
4648 /* This connection is being tarpitted. The CLIENT side has
4649 * already set the connect expiration date to the right
4650 * timeout. We just have to check that the client is still
4651 * there and that the timeout has not expired.
4652 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004654 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004655 !tick_is_expired(req->analyse_exp, now_ms))
4656 return 0;
4657
4658 /* We will set the queue timer to the time spent, just for
4659 * logging purposes. We fake a 500 server error, so that the
4660 * attacker will not suspect his connection has been tarpitted.
4661 * It will not cause trouble to the logs because we can exclude
4662 * the tarpitted connections by filtering on the 'PT' status flags.
4663 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004664 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4665
4666 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004667 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004668 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004669
4670 req->analysers = 0;
4671 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004672
Willy Tarreau60b85b02008-11-30 23:28:40 +01004673 if (!(s->flags & SN_ERR_MASK))
4674 s->flags |= SN_ERR_PRXCOND;
4675 if (!(s->flags & SN_FINST_MASK))
4676 s->flags |= SN_FINST_T;
4677 return 0;
4678}
4679
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004680/* This function is an analyser which waits for the HTTP request body. It waits
4681 * for either the buffer to be full, or the full advertised contents to have
4682 * reached the buffer. It must only be called after the standard HTTP request
4683 * processing has occurred, because it expects the request to be parsed and will
4684 * look for the Expect header. It may send a 100-Continue interim response. It
4685 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4686 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4687 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004688 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004689int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004690{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004691 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004693
4694 /* We have to parse the HTTP request body to find any required data.
4695 * "balance url_param check_post" should have been the only way to get
4696 * into this. We were brought here after HTTP header analysis, so all
4697 * related structures are ready.
4698 */
4699
Willy Tarreau890988f2014-04-10 11:59:33 +02004700 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4701 /* This is the first call */
4702 if (msg->msg_state < HTTP_MSG_BODY)
4703 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004704
Willy Tarreau890988f2014-04-10 11:59:33 +02004705 if (msg->msg_state < HTTP_MSG_100_SENT) {
4706 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4707 * send an HTTP/1.1 100 Continue intermediate response.
4708 */
4709 if (msg->flags & HTTP_MSGF_VER_11) {
4710 struct hdr_ctx ctx;
4711 ctx.idx = 0;
4712 /* Expect is allowed in 1.1, look for it */
4713 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4714 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004715 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004716 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004718 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004720
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004721 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004722 * req->buf->p still points to the beginning of the message. We
4723 * must save the body in msg->next because it survives buffer
4724 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004725 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004726 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004727
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004728 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004729 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4730 else
4731 msg->msg_state = HTTP_MSG_DATA;
4732 }
4733
Willy Tarreau890988f2014-04-10 11:59:33 +02004734 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4735 /* We're in content-length mode, we just have to wait for enough data. */
4736 if (req->buf->i - msg->sov < msg->body_len)
4737 goto missing_data;
4738
4739 /* OK we have everything we need now */
4740 goto http_end;
4741 }
4742
4743 /* OK here we're parsing a chunked-encoded message */
4744
Willy Tarreau522d6c02009-12-06 18:49:18 +01004745 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004746 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004747 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004748 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004749 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004750 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004751
Willy Tarreau115acb92009-12-26 13:56:06 +01004752 if (!ret)
4753 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004754 else if (ret < 0) {
4755 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004756 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004757 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004758 }
4759
Willy Tarreaud98cf932009-12-27 22:54:55 +01004760 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004761 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004762 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004763 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004764 if (msg->msg_state == HTTP_MSG_TRAILERS)
4765 goto http_end;
4766
Willy Tarreau226071e2014-04-10 11:55:45 +02004767 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004768 goto http_end;
4769
4770 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004771 /* we get here if we need to wait for more data. If the buffer is full,
4772 * we have the maximum we can expect.
4773 */
4774 if (buffer_full(req->buf, global.tune.maxrewrite))
4775 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004776
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004777 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004778 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004779 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004780
4781 if (!(s->flags & SN_ERR_MASK))
4782 s->flags |= SN_ERR_CLITO;
4783 if (!(s->flags & SN_FINST_MASK))
4784 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004785 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004786 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004787
4788 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004789 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004790 /* Not enough data. We'll re-use the http-request
4791 * timeout here. Ideally, we should set the timeout
4792 * relative to the accept() date. We just set the
4793 * request timeout once at the beginning of the
4794 * request.
4795 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004796 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004797 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004798 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004799 return 0;
4800 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004801
4802 http_end:
4803 /* The situation will not evolve, so let's give up on the analysis. */
4804 s->logs.tv_request = now; /* update the request timer to reflect full request */
4805 req->analysers &= ~an_bit;
4806 req->analyse_exp = TICK_ETERNITY;
4807 return 1;
4808
4809 return_bad_req: /* let's centralize all bad requests */
4810 txn->req.msg_state = HTTP_MSG_ERROR;
4811 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004812 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004813
Willy Tarreau79ebac62010-06-07 13:47:49 +02004814 if (!(s->flags & SN_ERR_MASK))
4815 s->flags |= SN_ERR_PRXCOND;
4816 if (!(s->flags & SN_FINST_MASK))
4817 s->flags |= SN_FINST_R;
4818
Willy Tarreau522d6c02009-12-06 18:49:18 +01004819 return_err_msg:
4820 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004821 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004822 if (s->listener->counters)
4823 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004824 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004825}
4826
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004827/* send a server's name with an outgoing request over an established connection.
4828 * Note: this function is designed to be called once the request has been scheduled
4829 * for being forwarded. This is the reason why it rewinds the buffer before
4830 * proceeding.
4831 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004832int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004833
4834 struct hdr_ctx ctx;
4835
Mark Lamourinec2247f02012-01-04 13:02:01 -05004836 char *hdr_name = be->server_id_hdr_name;
4837 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004838 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004839 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004840 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004841
William Lallemandd9e90662012-01-30 17:27:17 +01004842 ctx.idx = 0;
4843
Willy Tarreau211cdec2014-04-17 20:18:08 +02004844 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004845 if (old_o) {
4846 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004847 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004848 txn->req.next += old_o;
4849 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004850 }
4851
Willy Tarreau9b28e032012-10-12 23:49:43 +02004852 old_i = chn->buf->i;
4853 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 -05004854 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004855 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004856 }
4857
4858 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004859 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004860 memcpy(hdr_val, hdr_name, hdr_name_len);
4861 hdr_val += hdr_name_len;
4862 *hdr_val++ = ':';
4863 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004864 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4865 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004866
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004867 if (old_o) {
4868 /* If this was a forwarded request, we must readjust the amount of
4869 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004870 * variations. Note that the current state is >= HTTP_MSG_BODY,
4871 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004872 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004873 old_o += chn->buf->i - old_i;
4874 b_adv(chn->buf, old_o);
4875 txn->req.next -= old_o;
4876 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004877 }
4878
Mark Lamourinec2247f02012-01-04 13:02:01 -05004879 return 0;
4880}
4881
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882/* Terminate current transaction and prepare a new one. This is very tricky
4883 * right now but it works.
4884 */
4885void http_end_txn_clean_session(struct session *s)
4886{
Willy Tarreau068621e2013-12-23 15:11:25 +01004887 int prev_status = s->txn.status;
4888
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889 /* FIXME: We need a more portable way of releasing a backend's and a
4890 * server's connections. We need a safer way to reinitialize buffer
4891 * flags. We also need a more accurate method for computing per-request
4892 * data.
4893 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894
Willy Tarreau4213a112013-12-15 10:25:42 +01004895 /* unless we're doing keep-alive, we want to quickly close the connection
4896 * to the server.
4897 */
4898 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004899 !si_conn_ready(&s->si[1])) {
4900 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4901 si_shutr(&s->si[1]);
4902 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004903 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004905 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004906 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004907 if (unlikely(s->srv_conn))
4908 sess_change_server(s, NULL);
4909 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004910
4911 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4912 session_process_counters(s);
4913
4914 if (s->txn.status) {
4915 int n;
4916
4917 n = s->txn.status / 100;
4918 if (n < 1 || n > 5)
4919 n = 0;
4920
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004921 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004922 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004923 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004924 s->fe->fe_counters.p.http.comp_rsp++;
4925 }
Willy Tarreau24657792010-02-26 10:30:28 +01004926 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004927 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004928 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004929 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004930 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004931 s->be->be_counters.p.http.comp_rsp++;
4932 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933 }
4934
4935 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004936 s->logs.bytes_in -= s->req.buf->i;
4937 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938
4939 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004940 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 !(s->flags & SN_MONITOR) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004942 (!(s->fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004943 s->do_log(s);
4944 }
4945
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004946 /* stop tracking content-based counters */
4947 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004948 session_update_time_stats(s);
4949
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 s->logs.accept_date = date; /* user-visible date for logging */
4951 s->logs.tv_accept = now; /* corrected date for internal use */
4952 tv_zero(&s->logs.tv_request);
4953 s->logs.t_queue = -1;
4954 s->logs.t_connect = -1;
4955 s->logs.t_data = -1;
4956 s->logs.t_close = 0;
4957 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4958 s->logs.srv_queue_size = 0; /* we will get this number soon */
4959
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004960 s->logs.bytes_in = s->req.total = s->req.buf->i;
4961 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962
4963 if (s->pend_pos)
4964 pendconn_free(s->pend_pos);
4965
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004966 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 if (s->flags & SN_CURR_SESS) {
4968 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004969 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004971 if (may_dequeue_tasks(objt_server(s->target), s->be))
4972 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973 }
4974
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004975 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004976
Willy Tarreau4213a112013-12-15 10:25:42 +01004977 /* only release our endpoint if we don't intend to reuse the
4978 * connection.
4979 */
4980 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004981 !si_conn_ready(&s->si[1])) {
4982 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004983 }
4984
Willy Tarreau350f4872014-11-28 14:42:25 +01004985 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4986 s->si[1].err_type = SI_ET_NONE;
4987 s->si[1].conn_retries = 0; /* used for logging too */
4988 s->si[1].exp = TICK_ETERNITY;
4989 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004990 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
4991 s->res.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|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004992 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 +01004993 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02004994 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004995
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996 s->txn.meth = 0;
4997 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004998 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004999
5000 if (prev_status == 401 || prev_status == 407) {
5001 /* In HTTP keep-alive mode, if we receive a 401, we still have
5002 * a chance of being able to send the visitor again to the same
5003 * server over the same connection. This is required by some
5004 * broken protocols such as NTLM, and anyway whenever there is
5005 * an opportunity for sending the challenge to the proper place,
5006 * it's better to do it (at least it helps with debugging).
5007 */
5008 s->txn.flags |= TX_PREFER_LAST;
5009 }
5010
Willy Tarreauee55dc02010-06-01 10:56:34 +02005011 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005012 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005013
Willy Tarreau96e31212011-05-30 18:10:30 +02005014 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005015 s->req.flags |= CF_NEVER_WAIT;
5016 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005017 }
5018
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 /* if the request buffer is not empty, it means we're
5020 * about to process another request, so send pending
5021 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005022 * Just don't do this if the buffer is close to be full,
5023 * because the request will wait for it to flush a little
5024 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005026 if (s->req.buf->i) {
5027 if (s->res.buf->o &&
5028 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5029 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5030 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005031 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005032
5033 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005034 channel_auto_read(&s->req);
5035 channel_auto_close(&s->req);
5036 channel_auto_read(&s->res);
5037 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038
Willy Tarreau27375622013-12-17 00:00:28 +01005039 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005040 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005041
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005042 s->req.analysers = s->listener->analysers;
5043 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005044}
5045
5046
5047/* This function updates the request state machine according to the response
5048 * state machine and buffer flags. It returns 1 if it changes anything (flag
5049 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5050 * it is only used to find when a request/response couple is complete. Both
5051 * this function and its equivalent should loop until both return zero. It
5052 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5053 */
5054int http_sync_req_state(struct session *s)
5055{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005056 struct channel *chn = &s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005058 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 unsigned int old_state = txn->req.msg_state;
5060
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5062 return 0;
5063
5064 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005065 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005066 * We can shut the read side unless we want to abort_on_close,
5067 * or we have a POST request. The issue with POST requests is
5068 * that some browsers still send a CRLF after the request, and
5069 * this CRLF must be read so that it does not remain in the kernel
5070 * buffers, otherwise a close could cause an RST on some systems
5071 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005072 * Note that if we're using keep-alive on the client side, we'd
5073 * rather poll now and keep the polling enabled for the whole
5074 * session's life than enabling/disabling it between each
5075 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005076 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005077 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5078 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5079 !(s->be->options & PR_O_ABRT_CLOSE) &&
5080 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005081 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005082
Willy Tarreau40f151a2012-12-20 12:10:09 +01005083 /* if the server closes the connection, we want to immediately react
5084 * and close the socket to save packets and syscalls.
5085 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005086 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005087
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5089 goto wait_other_side;
5090
5091 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5092 /* The server has not finished to respond, so we
5093 * don't want to move in order not to upset it.
5094 */
5095 goto wait_other_side;
5096 }
5097
5098 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5099 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005100 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005102 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 goto wait_other_side;
5104 }
5105
5106 /* When we get here, it means that both the request and the
5107 * response have finished receiving. Depending on the connection
5108 * mode, we'll have to wait for the last bytes to leave in either
5109 * direction, and sometimes for a close to be effective.
5110 */
5111
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005112 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5113 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005114 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5115 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005116 }
5117 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5118 /* Option forceclose is set, or either side wants to close,
5119 * let's enforce it now that we're not expecting any new
5120 * data to come. The caller knows the session is complete
5121 * once both states are CLOSED.
5122 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005123 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5124 channel_shutr_now(chn);
5125 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005127 }
5128 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005129 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5130 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005131 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5133 channel_auto_read(chn);
5134 txn->req.msg_state = HTTP_MSG_TUNNEL;
5135 chn->flags |= CF_NEVER_WAIT;
5136 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 }
5138
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005139 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005141 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005142
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005143 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 txn->req.msg_state = HTTP_MSG_CLOSING;
5145 goto http_msg_closing;
5146 }
5147 else {
5148 txn->req.msg_state = HTTP_MSG_CLOSED;
5149 goto http_msg_closed;
5150 }
5151 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005152 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 }
5154
5155 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5156 http_msg_closing:
5157 /* nothing else to forward, just waiting for the output buffer
5158 * to be empty and for the shutw_now to take effect.
5159 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005160 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 txn->req.msg_state = HTTP_MSG_CLOSED;
5162 goto http_msg_closed;
5163 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005164 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165 txn->req.msg_state = HTTP_MSG_ERROR;
5166 goto wait_other_side;
5167 }
5168 }
5169
5170 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5171 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005172 /* see above in MSG_DONE why we only do this in these states */
5173 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5174 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5175 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005176 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 goto wait_other_side;
5178 }
5179
5180 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005181 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182}
5183
5184
5185/* This function updates the response state machine according to the request
5186 * state machine and buffer flags. It returns 1 if it changes anything (flag
5187 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5188 * it is only used to find when a request/response couple is complete. Both
5189 * this function and its equivalent should loop until both return zero. It
5190 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5191 */
5192int http_sync_res_state(struct session *s)
5193{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005194 struct channel *chn = &s->res;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005196 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 unsigned int old_state = txn->rsp.msg_state;
5198
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5200 return 0;
5201
5202 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5203 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005204 * still monitor the server connection for a possible close
5205 * while the request is being uploaded, so we don't disable
5206 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005208 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209
5210 if (txn->req.msg_state == HTTP_MSG_ERROR)
5211 goto wait_other_side;
5212
5213 if (txn->req.msg_state < HTTP_MSG_DONE) {
5214 /* The client seems to still be sending data, probably
5215 * because we got an error response during an upload.
5216 * We have the choice of either breaking the connection
5217 * or letting it pass through. Let's do the later.
5218 */
5219 goto wait_other_side;
5220 }
5221
5222 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5223 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005224 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005226 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 goto wait_other_side;
5228 }
5229
5230 /* When we get here, it means that both the request and the
5231 * response have finished receiving. Depending on the connection
5232 * mode, we'll have to wait for the last bytes to leave in either
5233 * direction, and sometimes for a close to be effective.
5234 */
5235
5236 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5237 /* Server-close mode : shut read and wait for the request
5238 * side to close its output buffer. The caller will detect
5239 * when we're in DONE and the other is in CLOSED and will
5240 * catch that for the final cleanup.
5241 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005242 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5243 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005245 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5246 /* Option forceclose is set, or either side wants to close,
5247 * let's enforce it now that we're not expecting any new
5248 * data to come. The caller knows the session is complete
5249 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005251 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5252 channel_shutr_now(chn);
5253 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005254 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 }
5256 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005257 /* The last possible modes are keep-alive and tunnel. Tunnel will
5258 * need to forward remaining data. Keep-alive will need to monitor
5259 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005260 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005261 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005262 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005263 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5264 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005265 }
5266
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005267 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005269 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5271 goto http_msg_closing;
5272 }
5273 else {
5274 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5275 goto http_msg_closed;
5276 }
5277 }
5278 goto wait_other_side;
5279 }
5280
5281 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5282 http_msg_closing:
5283 /* nothing else to forward, just waiting for the output buffer
5284 * to be empty and for the shutw_now to take effect.
5285 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005286 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005287 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5288 goto http_msg_closed;
5289 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005290 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005292 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005293 if (objt_server(s->target))
5294 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295 goto wait_other_side;
5296 }
5297 }
5298
5299 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5300 http_msg_closed:
5301 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005302 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 channel_auto_close(chn);
5304 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 goto wait_other_side;
5306 }
5307
5308 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005309 /* We force the response to leave immediately if we're waiting for the
5310 * other side, since there is no pending shutdown to push it out.
5311 */
5312 if (!channel_is_empty(chn))
5313 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005314 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315}
5316
5317
5318/* Resync the request and response state machines. Return 1 if either state
5319 * changes.
5320 */
5321int http_resync_states(struct session *s)
5322{
5323 struct http_txn *txn = &s->txn;
5324 int old_req_state = txn->req.msg_state;
5325 int old_res_state = txn->rsp.msg_state;
5326
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 http_sync_req_state(s);
5328 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005329 if (!http_sync_res_state(s))
5330 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 if (!http_sync_req_state(s))
5332 break;
5333 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005334
Willy Tarreau610ecce2010-01-04 21:15:02 +01005335 /* OK, both state machines agree on a compatible state.
5336 * There are a few cases we're interested in :
5337 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5338 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5339 * directions, so let's simply disable both analysers.
5340 * - HTTP_MSG_CLOSED on the response only means we must abort the
5341 * request.
5342 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5343 * with server-close mode means we've completed one request and we
5344 * must re-initialize the server connection.
5345 */
5346
5347 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5348 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5349 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5350 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005351 s->req.analysers = 0;
5352 channel_auto_close(&s->req);
5353 channel_auto_read(&s->req);
5354 s->res.analysers = 0;
5355 channel_auto_close(&s->res);
5356 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005357 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005358 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005359 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005360 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005361 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005362 s->res.analysers = 0;
5363 channel_auto_close(&s->res);
5364 channel_auto_read(&s->res);
5365 s->req.analysers = 0;
5366 channel_abort(&s->req);
5367 channel_auto_close(&s->req);
5368 channel_auto_read(&s->req);
5369 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005371 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5372 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005373 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005374 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5375 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5376 /* server-close/keep-alive: terminate this transaction,
5377 * possibly killing the server connection and reinitialize
5378 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 */
5380 http_end_txn_clean_session(s);
5381 }
5382
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 return txn->req.msg_state != old_req_state ||
5384 txn->rsp.msg_state != old_res_state;
5385}
5386
Willy Tarreaud98cf932009-12-27 22:54:55 +01005387/* This function is an analyser which forwards request body (including chunk
5388 * sizes if any). It is called as soon as we must forward, even if we forward
5389 * zero byte. The only situation where it must not be called is when we're in
5390 * tunnel mode and we want to forward till the close. It's used both to forward
5391 * remaining data and to resync after end of body. It expects the msg_state to
5392 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5393 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005394 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005395 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005397int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005398{
5399 struct http_txn *txn = &s->txn;
5400 struct http_msg *msg = &s->txn.req;
5401
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005402 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5403 return 0;
5404
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005405 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005406 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005407 /* Output closed while we were sending data. We must abort and
5408 * wake the other side up.
5409 */
5410 msg->msg_state = HTTP_MSG_ERROR;
5411 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005412 return 1;
5413 }
5414
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 /* Note that we don't have to send 100-continue back because we don't
5416 * need the data to complete our job, and it's up to the server to
5417 * decide whether to return 100, 417 or anything else in return of
5418 * an "Expect: 100-continue" header.
5419 */
5420
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005421 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005422 /* we have msg->sov which points to the first byte of message
5423 * body, and req->buf.p still points to the beginning of the
5424 * message. We forward the headers now, as we don't need them
5425 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005427 b_adv(req->buf, msg->sov);
5428 msg->next -= msg->sov;
5429 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005430
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005431 /* The previous analysers guarantee that the state is somewhere
5432 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5433 * msg->next are always correct.
5434 */
5435 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5436 if (msg->flags & HTTP_MSGF_TE_CHNK)
5437 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5438 else
5439 msg->msg_state = HTTP_MSG_DATA;
5440 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441 }
5442
Willy Tarreau7ba23542014-04-17 21:50:00 +02005443 /* Some post-connect processing might want us to refrain from starting to
5444 * forward data. Currently, the only reason for this is "balance url_param"
5445 * whichs need to parse/process the request after we've enabled forwarding.
5446 */
5447 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005448 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005449 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005450 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005451 goto missing_data;
5452 }
5453 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5454 }
5455
Willy Tarreau80a92c02014-03-12 10:41:13 +01005456 /* in most states, we should abort in case of early close */
5457 channel_auto_close(req);
5458
Willy Tarreauefdf0942014-04-24 20:08:57 +02005459 if (req->to_forward) {
5460 /* We can't process the buffer's contents yet */
5461 req->flags |= CF_WAKE_WRITE;
5462 goto missing_data;
5463 }
5464
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005466 if (msg->msg_state == HTTP_MSG_DATA) {
5467 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005468 /* we may have some pending data starting at req->buf->p */
5469 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005470 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005471 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005472 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005473 msg->next += msg->chunk_len;
5474 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005475
5476 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005477 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005478 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005479 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005480 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005481 }
5482 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005483 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005484 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005485 * TRAILERS state.
5486 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005487 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005488
Willy Tarreau54d23df2012-10-25 19:04:45 +02005489 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005490 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005491 else if (ret < 0) {
5492 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005493 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005494 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005495 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005496 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005499 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005501 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502
5503 if (ret == 0)
5504 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005505 else if (ret < 0) {
5506 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005507 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005508 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005510 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 /* we're in MSG_CHUNK_SIZE now */
5512 }
5513 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005514 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515
5516 if (ret == 0)
5517 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005518 else if (ret < 0) {
5519 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005520 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005521 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005523 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we're in HTTP_MSG_DONE now */
5525 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005527 int old_state = msg->msg_state;
5528
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005530
5531 /* we may have some pending data starting at req->buf->p
5532 * such as last chunk of data or trailers.
5533 */
5534 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005535 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005536 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005537 msg->next = 0;
5538
Willy Tarreau4fe41902010-06-07 22:27:41 +02005539 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005540 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5541 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005542 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 if (http_resync_states(s)) {
5544 /* some state changes occurred, maybe the analyser
5545 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005546 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005547 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005548 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005549 /* request errors are most likely due to
5550 * the server aborting the transfer.
5551 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005552 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005553 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005554 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005555 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005557 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005559 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005560
5561 /* If "option abortonclose" is set on the backend, we
5562 * want to monitor the client's connection and forward
5563 * any shutdown notification to the server, which will
5564 * decide whether to close or to go on processing the
5565 * request.
5566 */
5567 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005568 channel_auto_read(req);
5569 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005570 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005571 else if (s->txn.meth == HTTP_METH_POST) {
5572 /* POST requests may require to read extra CRLF
5573 * sent by broken browsers and which could cause
5574 * an RST to be sent upon close on some systems
5575 * (eg: Linux).
5576 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005577 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005578 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005579
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005581 }
5582 }
5583
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005585 /* we may have some pending data starting at req->buf->p */
5586 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005587 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005588 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5589
Willy Tarreaubed410e2014-04-22 08:19:34 +02005590 msg->next = 0;
5591 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5592
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005593 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005594 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005595 if (!(s->flags & SN_ERR_MASK))
5596 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005597 if (!(s->flags & SN_FINST_MASK)) {
5598 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5599 s->flags |= SN_FINST_H;
5600 else
5601 s->flags |= SN_FINST_D;
5602 }
5603
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005604 s->fe->fe_counters.cli_aborts++;
5605 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005606 if (objt_server(s->target))
5607 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005608
5609 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005610 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005611
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005612 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005613 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005614 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005615
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005616 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005617 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005618 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005619 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005620 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005621
Willy Tarreau5c620922011-05-11 19:56:11 +02005622 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005623 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005624 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005625 * modes are already handled by the stream sock layer. We must not do
5626 * this in content-length mode because it could present the MSG_MORE
5627 * flag with the last block of forwarded data, which would cause an
5628 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005629 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005630 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005631 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005632
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005633 return 0;
5634
Willy Tarreaud98cf932009-12-27 22:54:55 +01005635 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005636 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005637 if (s->listener->counters)
5638 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005639
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005640 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005641 /* we may have some pending data starting at req->buf->p */
5642 b_adv(req->buf, msg->next);
5643 msg->next = 0;
5644
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005645 txn->req.msg_state = HTTP_MSG_ERROR;
5646 if (txn->status) {
5647 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005648 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005649 } else {
5650 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005651 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005652 }
5653 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005654 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655
5656 if (!(s->flags & SN_ERR_MASK))
5657 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005658 if (!(s->flags & SN_FINST_MASK)) {
5659 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5660 s->flags |= SN_FINST_H;
5661 else
5662 s->flags |= SN_FINST_D;
5663 }
5664 return 0;
5665
5666 aborted_xfer:
5667 txn->req.msg_state = HTTP_MSG_ERROR;
5668 if (txn->status) {
5669 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005670 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005671 } else {
5672 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005673 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005674 }
5675 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005676 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005677
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005678 s->fe->fe_counters.srv_aborts++;
5679 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005680 if (objt_server(s->target))
5681 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005682
5683 if (!(s->flags & SN_ERR_MASK))
5684 s->flags |= SN_ERR_SRVCL;
5685 if (!(s->flags & SN_FINST_MASK)) {
5686 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5687 s->flags |= SN_FINST_H;
5688 else
5689 s->flags |= SN_FINST_D;
5690 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005691 return 0;
5692}
5693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5695 * processing can continue on next analysers, or zero if it either needs more
5696 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005697 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005698 * when it has nothing left to do, and may remove any analyser when it wants to
5699 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005700 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005701int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005702{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005703 struct http_txn *txn = &s->txn;
5704 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005705 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005706 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005708 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005709
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005710 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 +02005711 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005712 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005713 rep,
5714 rep->rex, rep->wex,
5715 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005716 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005717 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005719 /*
5720 * Now parse the partial (or complete) lines.
5721 * We will check the response syntax, and also join multi-line
5722 * headers. An index of all the lines will be elaborated while
5723 * parsing.
5724 *
5725 * For the parsing, we use a 28 states FSM.
5726 *
5727 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005728 * rep->buf->p = beginning of response
5729 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5730 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005731 * msg->eol = end of current header or line (LF or CRLF)
5732 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 */
5734
Willy Tarreau628c40c2014-04-24 19:11:26 +02005735 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005736 /* There's a protected area at the end of the buffer for rewriting
5737 * purposes. We don't want to start to parse the request if the
5738 * protected area is affected, because we may have to move processed
5739 * data later, which is much more complicated.
5740 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005741 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005742 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005743 /* some data has still not left the buffer, wake us once that's done */
5744 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5745 goto abort_response;
5746 channel_dont_close(rep);
5747 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005748 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005749 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005750 }
5751
Willy Tarreau379357a2013-06-08 12:55:46 +02005752 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5753 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5754 buffer_slow_realign(rep->buf);
5755
Willy Tarreau9b28e032012-10-12 23:49:43 +02005756 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005757 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005758 }
5759
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005760 /* 1: we might have to print this header in debug mode */
5761 if (unlikely((global.mode & MODE_DEBUG) &&
5762 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005763 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005764 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765
Willy Tarreau9b28e032012-10-12 23:49:43 +02005766 sol = rep->buf->p;
5767 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005770 sol += hdr_idx_first_pos(&txn->hdr_idx);
5771 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773 while (cur_idx) {
5774 eol = sol + txn->hdr_idx.v[cur_idx].len;
5775 debug_hdr("srvhdr", s, sol, eol);
5776 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5777 cur_idx = txn->hdr_idx.v[cur_idx].next;
5778 }
5779 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 /*
5782 * Now we quickly check if we have found a full valid response.
5783 * If not so, we check the FD and buffer states before leaving.
5784 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005785 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 * responses are checked first.
5787 *
5788 * Depending on whether the client is still there or not, we
5789 * may send an error response back or not. Note that normally
5790 * we should only check for HTTP status there, and check I/O
5791 * errors somewhere else.
5792 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793
Willy Tarreau655dce92009-11-08 13:10:58 +01005794 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 /* Invalid response */
5796 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5797 /* we detected a parsing error. We want to archive this response
5798 * in the dedicated proxy area for later troubleshooting.
5799 */
5800 hdr_response_bad:
5801 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005802 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005803
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005804 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005805 if (objt_server(s->target)) {
5806 objt_server(s->target)->counters.failed_resp++;
5807 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005808 }
Willy Tarreau64648412010-03-05 10:41:54 +01005809 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005810 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 rep->analysers = 0;
5812 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005813 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005814 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005815 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816
5817 if (!(s->flags & SN_ERR_MASK))
5818 s->flags |= SN_ERR_PRXCOND;
5819 if (!(s->flags & SN_FINST_MASK))
5820 s->flags |= SN_FINST_H;
5821
5822 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005823 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005824
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005825 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005826 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005827 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005828 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005829 goto hdr_response_bad;
5830 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005831
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005832 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005833 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005834 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005835 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005836 else if (txn->flags & TX_NOT_FIRST)
5837 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005838
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005839 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005840 if (objt_server(s->target)) {
5841 objt_server(s->target)->counters.failed_resp++;
5842 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005843 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005844
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005845 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005846 rep->analysers = 0;
5847 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005848 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005849 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005850 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005851
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005852 if (!(s->flags & SN_ERR_MASK))
5853 s->flags |= SN_ERR_SRVCL;
5854 if (!(s->flags & SN_FINST_MASK))
5855 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005856 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005857 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005858
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005859 /* read timeout : return a 504 to the client. */
5860 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005862 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005863 else if (txn->flags & TX_NOT_FIRST)
5864 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005865
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005866 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005867 if (objt_server(s->target)) {
5868 objt_server(s->target)->counters.failed_resp++;
5869 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005870 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005871
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005872 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005873 rep->analysers = 0;
5874 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005875 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005876 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005877 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005878
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005879 if (!(s->flags & SN_ERR_MASK))
5880 s->flags |= SN_ERR_SRVTO;
5881 if (!(s->flags & SN_FINST_MASK))
5882 s->flags |= SN_FINST_H;
5883 return 0;
5884 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005885
Willy Tarreauf003d372012-11-26 13:35:37 +01005886 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005887 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005888 s->fe->fe_counters.cli_aborts++;
5889 s->be->be_counters.cli_aborts++;
5890 if (objt_server(s->target))
5891 objt_server(s->target)->counters.cli_aborts++;
5892
5893 rep->analysers = 0;
5894 channel_auto_close(rep);
5895
5896 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005897 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005898 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005899
5900 if (!(s->flags & SN_ERR_MASK))
5901 s->flags |= SN_ERR_CLICL;
5902 if (!(s->flags & SN_FINST_MASK))
5903 s->flags |= SN_FINST_H;
5904
5905 /* process_session() will take care of the error */
5906 return 0;
5907 }
5908
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005909 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005910 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005911 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005912 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005913 else if (txn->flags & TX_NOT_FIRST)
5914 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005915
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005916 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005917 if (objt_server(s->target)) {
5918 objt_server(s->target)->counters.failed_resp++;
5919 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005920 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005921
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005922 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923 rep->analysers = 0;
5924 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005925 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005926 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005927 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005928
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005929 if (!(s->flags & SN_ERR_MASK))
5930 s->flags |= SN_ERR_SRVCL;
5931 if (!(s->flags & SN_FINST_MASK))
5932 s->flags |= SN_FINST_H;
5933 return 0;
5934 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005937 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005939 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005940 else if (txn->flags & TX_NOT_FIRST)
5941 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005942
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005943 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005944 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005945 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005946
5947 if (!(s->flags & SN_ERR_MASK))
5948 s->flags |= SN_ERR_CLICL;
5949 if (!(s->flags & SN_FINST_MASK))
5950 s->flags |= SN_FINST_H;
5951
5952 /* process_session() will take care of the error */
5953 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005954 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005955
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005956 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005957 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 return 0;
5959 }
5960
5961 /* More interesting part now : we know that we have a complete
5962 * response which at least looks like HTTP. We have an indicator
5963 * of each header's length, so we can parse them quickly.
5964 */
5965
5966 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005967 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 /*
5970 * 1: get the status code
5971 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005972 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 if (n < 1 || n > 5)
5974 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005975 /* when the client triggers a 4xx from the server, it's most often due
5976 * to a missing object or permission. These events should be tracked
5977 * because if they happen often, it may indicate a brute force or a
5978 * vulnerability scan.
5979 */
5980 if (n == 4)
5981 session_inc_http_err_ctr(s);
5982
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005983 if (objt_server(s->target))
5984 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985
Willy Tarreau5b154472009-12-21 20:11:07 +01005986 /* check if the response is HTTP/1.1 or above */
5987 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005988 ((rep->buf->p[5] > '1') ||
5989 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005990 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005991
5992 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005993 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 +01005994
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005995 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005996 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005997
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999
Willy Tarreau39650402010-03-15 19:44:39 +01006000 /* Adjust server's health based on status code. Note: status codes 501
6001 * and 505 are triggered on demand by client request, so we must not
6002 * count them as server failures.
6003 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006004 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006005 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006006 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006007 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006008 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006009 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006010
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011 /*
6012 * 2: check for cacheability.
6013 */
6014
6015 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006016 case 100:
6017 /*
6018 * We may be facing a 100-continue response, in which case this
6019 * is not the right response, and we're waiting for the next one.
6020 * Let's allow this response to go to the client and wait for the
6021 * next one.
6022 */
6023 hdr_idx_init(&txn->hdr_idx);
6024 msg->next -= channel_forward(rep, msg->next);
6025 msg->msg_state = HTTP_MSG_RPBEFORE;
6026 txn->status = 0;
6027 s->logs.t_data = -1; /* was not a response yet */
6028 goto next_one;
6029
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006030 case 200:
6031 case 203:
6032 case 206:
6033 case 300:
6034 case 301:
6035 case 410:
6036 /* RFC2616 @13.4:
6037 * "A response received with a status code of
6038 * 200, 203, 206, 300, 301 or 410 MAY be stored
6039 * by a cache (...) unless a cache-control
6040 * directive prohibits caching."
6041 *
6042 * RFC2616 @9.5: POST method :
6043 * "Responses to this method are not cacheable,
6044 * unless the response includes appropriate
6045 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006046 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006048 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006049 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6050 break;
6051 default:
6052 break;
6053 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 /*
6056 * 3: we may need to capture headers
6057 */
6058 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006059 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006060 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006061 txn->rsp.cap, s->fe->rsp_cap);
6062
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006063 /* 4: determine the transfer-length.
6064 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6065 * the presence of a message-body in a RESPONSE and its transfer length
6066 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006067 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006068 * All responses to the HEAD request method MUST NOT include a
6069 * message-body, even though the presence of entity-header fields
6070 * might lead one to believe they do. All 1xx (informational), 204
6071 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6072 * message-body. All other responses do include a message-body,
6073 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006074 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006075 * 1. Any response which "MUST NOT" include a message-body (such as the
6076 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6077 * always terminated by the first empty line after the header fields,
6078 * regardless of the entity-header fields present in the message.
6079 *
6080 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6081 * the "chunked" transfer-coding (Section 6.2) is used, the
6082 * transfer-length is defined by the use of this transfer-coding.
6083 * If a Transfer-Encoding header field is present and the "chunked"
6084 * transfer-coding is not present, the transfer-length is defined by
6085 * the sender closing the connection.
6086 *
6087 * 3. If a Content-Length header field is present, its decimal value in
6088 * OCTETs represents both the entity-length and the transfer-length.
6089 * If a message is received with both a Transfer-Encoding header
6090 * field and a Content-Length header field, the latter MUST be ignored.
6091 *
6092 * 4. If the message uses the media type "multipart/byteranges", and
6093 * the transfer-length is not otherwise specified, then this self-
6094 * delimiting media type defines the transfer-length. This media
6095 * type MUST NOT be used unless the sender knows that the recipient
6096 * can parse it; the presence in a request of a Range header with
6097 * multiple byte-range specifiers from a 1.1 client implies that the
6098 * client can parse multipart/byteranges responses.
6099 *
6100 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006101 */
6102
6103 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006104 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006105 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006106 * FIXME: should we parse anyway and return an error on chunked encoding ?
6107 */
6108 if (txn->meth == HTTP_METH_HEAD ||
6109 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006110 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006111 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006112 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006113 goto skip_content_length;
6114 }
6115
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006116 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006117 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006118 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006119 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006120 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006121 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6122 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006123 /* bad transfer-encoding (chunked followed by something else) */
6124 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006125 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006126 break;
6127 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006128 }
6129
6130 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6131 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006132 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006133 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006134 signed long long cl;
6135
Willy Tarreauad14f752011-09-02 20:33:27 +02006136 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006137 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006138 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006139 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006140
Willy Tarreauad14f752011-09-02 20:33:27 +02006141 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006142 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006143 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006144 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006145
Willy Tarreauad14f752011-09-02 20:33:27 +02006146 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006147 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006148 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006149 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006151 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006152 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006154 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006156 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006157 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 }
6159
William Lallemand82fe75c2012-10-23 10:25:10 +02006160 if (s->fe->comp || s->be->comp)
6161 select_compression_response_header(s, rep->buf);
6162
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006163skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006164 /* Now we have to check if we need to modify the Connection header.
6165 * This is more difficult on the response than it is on the request,
6166 * because we can have two different HTTP versions and we don't know
6167 * how the client will interprete a response. For instance, let's say
6168 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6169 * HTTP/1.1 response without any header. Maybe it will bound itself to
6170 * HTTP/1.0 because it only knows about it, and will consider the lack
6171 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6172 * the lack of header as a keep-alive. Thus we will use two flags
6173 * indicating how a request MAY be understood by the client. In case
6174 * of multiple possibilities, we'll fix the header to be explicit. If
6175 * ambiguous cases such as both close and keepalive are seen, then we
6176 * will fall back to explicit close. Note that we won't take risks with
6177 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006178 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006179 */
6180
Willy Tarreaudc008c52010-02-01 16:20:08 +01006181 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6182 txn->status == 101)) {
6183 /* Either we've established an explicit tunnel, or we're
6184 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006185 * to understand the next protocols. We have to switch to tunnel
6186 * mode, so that we transfer the request and responses then let
6187 * this protocol pass unmodified. When we later implement specific
6188 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006189 * header which contains information about that protocol for
6190 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006191 */
6192 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6193 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006194 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6195 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006196 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6197 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006198 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006199
Willy Tarreau70dffda2014-01-30 03:07:23 +01006200 /* this situation happens when combining pretend-keepalive with httpclose. */
6201 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006202 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6203 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006204 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6205
Willy Tarreau60466522010-01-18 19:08:45 +01006206 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006207 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006208 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6209 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006210
Willy Tarreau60466522010-01-18 19:08:45 +01006211 /* now adjust header transformations depending on current state */
6212 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6213 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6214 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006215 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006216 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006217 }
Willy Tarreau60466522010-01-18 19:08:45 +01006218 else { /* SCL / KAL */
6219 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006220 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006221 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006222 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006223
Willy Tarreau60466522010-01-18 19:08:45 +01006224 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006225 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006226
Willy Tarreau60466522010-01-18 19:08:45 +01006227 /* Some keep-alive responses are converted to Server-close if
6228 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006229 */
Willy Tarreau60466522010-01-18 19:08:45 +01006230 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6231 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006232 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006233 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006234 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006235 }
6236
Willy Tarreau7959a552013-09-23 16:44:27 +02006237 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006238 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006239
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006240 /* end of job, return OK */
6241 rep->analysers &= ~an_bit;
6242 rep->analyse_exp = TICK_ETERNITY;
6243 channel_auto_close(rep);
6244 return 1;
6245
6246 abort_keep_alive:
6247 /* A keep-alive request to the server failed on a network error.
6248 * The client is required to retry. We need to close without returning
6249 * any other information so that the client retries.
6250 */
6251 txn->status = 0;
6252 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006253 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006254 channel_auto_close(rep);
6255 s->logs.logwait = 0;
6256 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006257 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006258 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006259 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006260 return 0;
6261}
6262
6263/* This function performs all the processing enabled for the current response.
6264 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006265 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006266 * other functions. It works like process_request (see indications above).
6267 */
6268int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6269{
6270 struct http_txn *txn = &s->txn;
6271 struct http_msg *msg = &txn->rsp;
6272 struct proxy *cur_proxy;
6273 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006274 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006275
6276 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6277 now_ms, __FUNCTION__,
6278 s,
6279 rep,
6280 rep->rex, rep->wex,
6281 rep->flags,
6282 rep->buf->i,
6283 rep->analysers);
6284
6285 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6286 return 0;
6287
Willy Tarreau70730dd2014-04-24 18:06:27 +02006288 /* The stats applet needs to adjust the Connection header but we don't
6289 * apply any filter there.
6290 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006291 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6292 rep->analysers &= ~an_bit;
6293 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006294 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006295 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006296
Willy Tarreau58975672014-04-24 21:13:57 +02006297 /*
6298 * We will have to evaluate the filters.
6299 * As opposed to version 1.2, now they will be evaluated in the
6300 * filters order and not in the header order. This means that
6301 * each filter has to be validated among all headers.
6302 *
6303 * Filters are tried with ->be first, then with ->fe if it is
6304 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006305 *
6306 * Maybe we are in resume condiion. In this case I choose the
6307 * "struct proxy" which contains the rule list matching the resume
6308 * pointer. If none of theses "struct proxy" match, I initialise
6309 * the process with the first one.
6310 *
6311 * In fact, I check only correspondance betwwen the current list
6312 * pointer and the ->fe rule list. If it doesn't match, I initialize
6313 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006314 */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006315 if (s->current_rule_list == &s->fe->http_res_rules)
6316 cur_proxy = s->fe;
6317 else
6318 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006319 while (1) {
6320 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006321
Willy Tarreau58975672014-04-24 21:13:57 +02006322 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006323 if (ret == HTTP_RULE_RES_CONT)
6324 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006325
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006326 /* we need to be called again. */
6327 if (ret == HTTP_RULE_RES_YIELD) {
6328 channel_dont_close(rep);
6329 return 0;
6330 }
6331
Willy Tarreau58975672014-04-24 21:13:57 +02006332 /* try headers filters */
6333 if (rule_set->rsp_exp != NULL) {
6334 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6335 return_bad_resp:
6336 if (objt_server(s->target)) {
6337 objt_server(s->target)->counters.failed_resp++;
6338 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006339 }
Willy Tarreau58975672014-04-24 21:13:57 +02006340 s->be->be_counters.failed_resp++;
6341 return_srv_prx_502:
6342 rep->analysers = 0;
6343 txn->status = 502;
6344 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006345 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006346 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006347 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau58975672014-04-24 21:13:57 +02006348 if (!(s->flags & SN_ERR_MASK))
6349 s->flags |= SN_ERR_PRXCOND;
6350 if (!(s->flags & SN_FINST_MASK))
6351 s->flags |= SN_FINST_H;
6352 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006353 }
Willy Tarreau58975672014-04-24 21:13:57 +02006354 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006355
Willy Tarreau58975672014-04-24 21:13:57 +02006356 /* has the response been denied ? */
6357 if (txn->flags & TX_SVDENY) {
6358 if (objt_server(s->target))
6359 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006360
Willy Tarreau58975672014-04-24 21:13:57 +02006361 s->be->be_counters.denied_resp++;
6362 s->fe->fe_counters.denied_resp++;
6363 if (s->listener->counters)
6364 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006365
Willy Tarreau58975672014-04-24 21:13:57 +02006366 goto return_srv_prx_502;
6367 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006368
Willy Tarreau58975672014-04-24 21:13:57 +02006369 /* add response headers from the rule sets in the same order */
6370 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006371 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006372 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006373 if (wl->cond) {
6374 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6375 ret = acl_pass(ret);
6376 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6377 ret = !ret;
6378 if (!ret)
6379 continue;
6380 }
6381 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6382 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006383 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006384
Willy Tarreau58975672014-04-24 21:13:57 +02006385 /* check whether we're already working on the frontend */
6386 if (cur_proxy == s->fe)
6387 break;
6388 cur_proxy = s->fe;
6389 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006390
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006391 /* After this point, this anayzer can't return yield, so we can
6392 * remove the bit corresponding to this analyzer from the list.
6393 *
6394 * Note that the intermediate returns and goto found previously
6395 * reset the analyzers.
6396 */
6397 rep->analysers &= ~an_bit;
6398 rep->analyse_exp = TICK_ETERNITY;
6399
Willy Tarreau58975672014-04-24 21:13:57 +02006400 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006401 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006402 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006403
Willy Tarreau58975672014-04-24 21:13:57 +02006404 /*
6405 * Now check for a server cookie.
6406 */
6407 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6408 (s->be->options & PR_O_CHK_CACHE))
6409 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006410
Willy Tarreau58975672014-04-24 21:13:57 +02006411 /*
6412 * Check for cache-control or pragma headers if required.
6413 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006414 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02006415 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006416
Willy Tarreau58975672014-04-24 21:13:57 +02006417 /*
6418 * Add server cookie in the response if needed
6419 */
6420 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6421 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6422 (!(s->flags & SN_DIRECT) ||
6423 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6424 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6425 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6426 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6427 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6428 !(s->flags & SN_IGNORE_PRST)) {
6429 /* the server is known, it's not the one the client requested, or the
6430 * cookie's last seen date needs to be refreshed. We have to
6431 * insert a set-cookie here, except if we want to insert only on POST
6432 * requests and this one isn't. Note that servers which don't have cookies
6433 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006434 */
Willy Tarreau58975672014-04-24 21:13:57 +02006435 if (!objt_server(s->target)->cookie) {
6436 chunk_printf(&trash,
6437 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6438 s->be->cookie_name);
6439 }
6440 else {
6441 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6444 /* emit last_date, which is mandatory */
6445 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6446 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6447 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006448
Willy Tarreau58975672014-04-24 21:13:57 +02006449 if (s->be->cookie_maxlife) {
6450 /* emit first_date, which is either the original one or
6451 * the current date.
6452 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006453 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006454 s30tob64(txn->cookie_first_date ?
6455 txn->cookie_first_date >> 2 :
6456 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006457 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006458 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006459 }
Willy Tarreau58975672014-04-24 21:13:57 +02006460 chunk_appendf(&trash, "; path=/");
6461 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006462
Willy Tarreau58975672014-04-24 21:13:57 +02006463 if (s->be->cookie_domain)
6464 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006465
Willy Tarreau58975672014-04-24 21:13:57 +02006466 if (s->be->ck_opts & PR_CK_HTTPONLY)
6467 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006468
Willy Tarreau58975672014-04-24 21:13:57 +02006469 if (s->be->ck_opts & PR_CK_SECURE)
6470 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006471
Willy Tarreau58975672014-04-24 21:13:57 +02006472 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6473 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006474
Willy Tarreau58975672014-04-24 21:13:57 +02006475 txn->flags &= ~TX_SCK_MASK;
6476 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6477 /* the server did not change, only the date was updated */
6478 txn->flags |= TX_SCK_UPDATED;
6479 else
6480 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006481
Willy Tarreau58975672014-04-24 21:13:57 +02006482 /* Here, we will tell an eventual cache on the client side that we don't
6483 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6484 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6485 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006486 */
Willy Tarreau58975672014-04-24 21:13:57 +02006487 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006488
Willy Tarreau58975672014-04-24 21:13:57 +02006489 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006490
Willy Tarreau58975672014-04-24 21:13:57 +02006491 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6492 "Cache-control: private", 22) < 0))
6493 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006494 }
Willy Tarreau58975672014-04-24 21:13:57 +02006495 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006496
Willy Tarreau58975672014-04-24 21:13:57 +02006497 /*
6498 * Check if result will be cacheable with a cookie.
6499 * We'll block the response if security checks have caught
6500 * nasty things such as a cacheable cookie.
6501 */
6502 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6503 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6504 (s->be->options & PR_O_CHK_CACHE)) {
6505 /* we're in presence of a cacheable response containing
6506 * a set-cookie header. We'll block it as requested by
6507 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508 */
Willy Tarreau58975672014-04-24 21:13:57 +02006509 if (objt_server(s->target))
6510 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006511
Willy Tarreau58975672014-04-24 21:13:57 +02006512 s->be->be_counters.denied_resp++;
6513 s->fe->fe_counters.denied_resp++;
6514 if (s->listener->counters)
6515 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6518 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6519 send_log(s->be, LOG_ALERT,
6520 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6521 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6522 goto return_srv_prx_502;
6523 }
Willy Tarreau03945942009-12-22 16:50:27 +01006524
Willy Tarreau70730dd2014-04-24 18:06:27 +02006525 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006526 /*
6527 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6528 * If an "Upgrade" token is found, the header is left untouched in order
6529 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006530 * if anything but "Upgrade" is present in the Connection header. We don't
6531 * want to touch any 101 response either since it's switching to another
6532 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006533 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006534 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006535 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6536 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6537 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6538 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6541 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6542 /* we want a keep-alive response here. Keep-alive header
6543 * required if either side is not 1.1.
6544 */
6545 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6546 want_flags |= TX_CON_KAL_SET;
6547 }
6548 else {
6549 /* we want a close response here. Close header required if
6550 * the server is 1.1, regardless of the client.
6551 */
6552 if (msg->flags & HTTP_MSGF_VER_11)
6553 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006554 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6557 http_change_connection_header(txn, msg, want_flags);
6558 }
6559
6560 skip_header_mangling:
6561 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6562 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6563 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564
Willy Tarreau58975672014-04-24 21:13:57 +02006565 /* if the user wants to log as soon as possible, without counting
6566 * bytes from the server, then this is the right moment. We have
6567 * to temporarily assign bytes_out to log what we currently have.
6568 */
6569 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6570 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6571 s->logs.bytes_out = txn->rsp.eoh;
6572 s->do_log(s);
6573 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006574 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006575 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006576}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577
Willy Tarreaud98cf932009-12-27 22:54:55 +01006578/* This function is an analyser which forwards response body (including chunk
6579 * sizes if any). It is called as soon as we must forward, even if we forward
6580 * zero byte. The only situation where it must not be called is when we're in
6581 * tunnel mode and we want to forward till the close. It's used both to forward
6582 * remaining data and to resync after end of body. It expects the msg_state to
6583 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6584 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006585 *
6586 * It is capable of compressing response data both in content-length mode and
6587 * in chunked mode. The state machines follows different flows depending on
6588 * whether content-length and chunked modes are used, since there are no
6589 * trailers in content-length :
6590 *
6591 * chk-mode cl-mode
6592 * ,----- BODY -----.
6593 * / \
6594 * V size > 0 V chk-mode
6595 * .--> SIZE -------------> DATA -------------> CRLF
6596 * | | size == 0 | last byte |
6597 * | v final crlf v inspected |
6598 * | TRAILERS -----------> DONE |
6599 * | |
6600 * `----------------------------------------------'
6601 *
6602 * Compression only happens in the DATA state, and must be flushed in final
6603 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6604 * is performed at once on final states for all bytes parsed, or when leaving
6605 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006606 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006607int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006608{
6609 struct http_txn *txn = &s->txn;
6610 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006611 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006612 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006613 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006614
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006615 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6616 return 0;
6617
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006618 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006619 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006620 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006621 /* Output closed while we were sending data. We must abort and
6622 * wake the other side up.
6623 */
6624 msg->msg_state = HTTP_MSG_ERROR;
6625 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006626 return 1;
6627 }
6628
Willy Tarreau4fe41902010-06-07 22:27:41 +02006629 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006630 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006631
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006632 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006633 /* we have msg->sov which points to the first byte of message
6634 * body, and res->buf.p still points to the beginning of the
6635 * message. We forward the headers now, as we don't need them
6636 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006637 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006638 b_adv(res->buf, msg->sov);
6639 msg->next -= msg->sov;
6640 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006641
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006642 /* The previous analysers guarantee that the state is somewhere
6643 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6644 * msg->next are always correct.
6645 */
6646 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6647 if (msg->flags & HTTP_MSGF_TE_CHNK)
6648 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6649 else
6650 msg->msg_state = HTTP_MSG_DATA;
6651 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006652 }
6653
Willy Tarreauefdf0942014-04-24 20:08:57 +02006654 if (res->to_forward) {
6655 /* We can't process the buffer's contents yet */
6656 res->flags |= CF_WAKE_WRITE;
6657 goto missing_data;
6658 }
6659
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006660 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6661 /* We need a compression buffer in the DATA state to put the
6662 * output of compressed data, and in CRLF state to let the
6663 * TRAILERS state finish the job of removing the trailing CRLF.
6664 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006665 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006666 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006667 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006668 goto aborted_xfer; /* no memory */
6669 }
6670
6671 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006672 if (ret < 0) {
6673 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006674 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006675 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006676 compressing = 1;
6677 }
6678
Willy Tarreaud98cf932009-12-27 22:54:55 +01006679 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006680 switch (msg->msg_state - HTTP_MSG_DATA) {
6681 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006682 /* we may have some pending data starting at res->buf->p */
6683 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006684 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006685 if (ret < 0)
6686 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006687
Willy Tarreaud5a67832014-04-21 10:54:27 +02006688 if (msg->chunk_len) {
6689 /* input empty or output full */
6690 if (res->buf->i > msg->next)
6691 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006692 goto missing_data;
6693 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006694 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006695 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006696 if (msg->chunk_len > res->buf->i - msg->next) {
6697 /* output full */
6698 res->flags |= CF_WAKE_WRITE;
6699 goto missing_data;
6700 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006701 msg->next += msg->chunk_len;
6702 msg->chunk_len = 0;
6703 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006704
6705 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006706 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006707 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006708 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006709 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006710 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006711 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006712 /* fall through for HTTP_MSG_CHUNK_CRLF */
6713
6714 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6715 /* we want the CRLF after the data */
6716
6717 ret = http_skip_chunk_crlf(msg);
6718 if (ret == 0)
6719 goto missing_data;
6720 else if (ret < 0) {
6721 if (msg->err_pos >= 0)
6722 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6723 goto return_bad_res;
6724 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006725 /* we're in MSG_CHUNK_SIZE now, fall through */
6726
6727 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006728 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006729 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006730 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006731 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006732
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006733 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006734 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006735 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006736 else if (ret < 0) {
6737 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006738 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006739 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006740 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006741 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006742 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006743
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006744 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006745 if (unlikely(compressing)) {
6746 /* we need to flush output contents before syncing FSMs */
6747 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6748 compressing = 0;
6749 }
6750
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006751 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006752 if (ret == 0)
6753 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006754 else if (ret < 0) {
6755 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006756 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006757 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006758 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006759 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006760
6761 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006762 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006763 if (unlikely(compressing)) {
6764 /* we need to flush output contents before syncing FSMs */
6765 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6766 compressing = 0;
6767 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006768
Willy Tarreauc623c172014-04-18 09:53:50 +02006769 /* we may have some pending data starting at res->buf->p
6770 * such as a last chunk of data or trailers.
6771 */
6772 b_adv(res->buf, msg->next);
6773 msg->next = 0;
6774
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006775 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006776 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006777 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6778 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006779 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006780
Willy Tarreau610ecce2010-01-04 21:15:02 +01006781 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006782 /* some state changes occurred, maybe the analyser
6783 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006784 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006785 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006786 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006787 /* response errors are most likely due to
6788 * the client aborting the transfer.
6789 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006790 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006791 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006792 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006793 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006794 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006795 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006796 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006797 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006798 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006799 }
6800 }
6801
Willy Tarreaud98cf932009-12-27 22:54:55 +01006802 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006803 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006804 if (unlikely(compressing)) {
6805 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006806 compressing = 0;
6807 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006808
Willy Tarreauc623c172014-04-18 09:53:50 +02006809 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6810 b_adv(res->buf, msg->next);
6811 msg->next = 0;
6812 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6813 }
6814
Willy Tarreauf003d372012-11-26 13:35:37 +01006815 if (res->flags & CF_SHUTW)
6816 goto aborted_xfer;
6817
6818 /* stop waiting for data if the input is closed before the end. If the
6819 * client side was already closed, it means that the client has aborted,
6820 * so we don't want to count this as a server abort. Otherwise it's a
6821 * server abort.
6822 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006823 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006824 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006825 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006826 if (!(s->flags & SN_ERR_MASK))
6827 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006828 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006829 if (objt_server(s->target))
6830 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006831 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006832 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006833
Willy Tarreau40dba092010-03-04 18:14:51 +01006834 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006835 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006836 goto return_bad_res;
6837
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006838 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006839 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006840 * Similarly, with keep-alive on the client side, we don't want to forward a
6841 * close.
6842 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006843 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006844 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6845 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006846 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006847
Willy Tarreau5c620922011-05-11 19:56:11 +02006848 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006849 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006850 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006851 * modes are already handled by the stream sock layer. We must not do
6852 * this in content-length mode because it could present the MSG_MORE
6853 * flag with the last block of forwarded data, which would cause an
6854 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006855 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006856 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006857 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006858
Willy Tarreaud98cf932009-12-27 22:54:55 +01006859 /* the session handler will take care of timeouts and errors */
6860 return 0;
6861
Willy Tarreau40dba092010-03-04 18:14:51 +01006862 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006863 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006864 if (objt_server(s->target))
6865 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006866
6867 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006868 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006869 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006870 compressing = 0;
6871 }
6872
Willy Tarreauc623c172014-04-18 09:53:50 +02006873 /* we may have some pending data starting at res->buf->p */
6874 if (s->comp_algo == NULL) {
6875 b_adv(res->buf, msg->next);
6876 msg->next = 0;
6877 }
6878
Willy Tarreaud98cf932009-12-27 22:54:55 +01006879 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006880 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006881 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006882 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006883 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006884 if (objt_server(s->target))
6885 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006886
6887 if (!(s->flags & SN_ERR_MASK))
6888 s->flags |= SN_ERR_PRXCOND;
6889 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006890 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006891 return 0;
6892
6893 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006894 if (unlikely(compressing)) {
6895 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6896 compressing = 0;
6897 }
6898
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006899 txn->rsp.msg_state = HTTP_MSG_ERROR;
6900 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006901 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006902 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006903 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006904
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006905 s->fe->fe_counters.cli_aborts++;
6906 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006907 if (objt_server(s->target))
6908 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006909
6910 if (!(s->flags & SN_ERR_MASK))
6911 s->flags |= SN_ERR_CLICL;
6912 if (!(s->flags & SN_FINST_MASK))
6913 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006914 return 0;
6915}
6916
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006917/* Iterate the same filter through all request headers.
6918 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 * Since it can manage the switch to another backend, it updates the per-proxy
6920 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006922int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 char *cur_ptr, *cur_end, *cur_next;
6925 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006926 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006927 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006928 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006929
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006930 last_hdr = 0;
6931
Willy Tarreau9b28e032012-10-12 23:49:43 +02006932 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 old_idx = 0;
6934
6935 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006936 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006937 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006938 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006939 (exp->action == ACT_ALLOW ||
6940 exp->action == ACT_DENY ||
6941 exp->action == ACT_TARPIT))
6942 return 0;
6943
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006944 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006945 if (!cur_idx)
6946 break;
6947
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006948 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 cur_ptr = cur_next;
6950 cur_end = cur_ptr + cur_hdr->len;
6951 cur_next = cur_end + cur_hdr->cr + 1;
6952
6953 /* Now we have one header between cur_ptr and cur_end,
6954 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006955 */
6956
Willy Tarreau15a53a42015-01-21 13:39:42 +01006957 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 switch (exp->action) {
6959 case ACT_SETBE:
6960 /* It is not possible to jump a second time.
6961 * FIXME: should we return an HTTP/500 here so that
6962 * the admin knows there's a problem ?
6963 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006964 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006965 break;
6966
6967 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006968 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969 last_hdr = 1;
6970 break;
6971
6972 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006973 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006974 last_hdr = 1;
6975 break;
6976
6977 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006978 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 break;
6981
6982 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006983 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006985 break;
6986
6987 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006988 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6989 if (trash.len < 0)
6990 return -1;
6991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006992 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 /* FIXME: if the user adds a newline in the replacement, the
6994 * index will not be recalculated for now, and the new line
6995 * will not be counted as a new header.
6996 */
6997
6998 cur_end += delta;
6999 cur_next += delta;
7000 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007001 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007002 break;
7003
7004 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007005 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 cur_next += delta;
7007
Willy Tarreaufa355d42009-11-29 18:12:29 +01007008 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007009 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7010 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 cur_hdr->len = 0;
7012 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007013 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007014 break;
7015
7016 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017 }
7018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007019 /* keep the link from this header to next one in case of later
7020 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007021 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 old_idx = cur_idx;
7023 }
7024 return 0;
7025}
7026
7027
7028/* Apply the filter to the request line.
7029 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7030 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 * Since it can manage the switch to another backend, it updates the per-proxy
7032 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007034int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007035{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 char *cur_ptr, *cur_end;
7037 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007038 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007039 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007040
Willy Tarreau3d300592007-03-18 18:34:41 +01007041 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007042 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007043 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 (exp->action == ACT_ALLOW ||
7045 exp->action == ACT_DENY ||
7046 exp->action == ACT_TARPIT))
7047 return 0;
7048 else if (exp->action == ACT_REMOVE)
7049 return 0;
7050
7051 done = 0;
7052
Willy Tarreau9b28e032012-10-12 23:49:43 +02007053 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007054 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055
7056 /* Now we have the request line between cur_ptr and cur_end */
7057
Willy Tarreau15a53a42015-01-21 13:39:42 +01007058 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007059 switch (exp->action) {
7060 case ACT_SETBE:
7061 /* It is not possible to jump a second time.
7062 * FIXME: should we return an HTTP/500 here so that
7063 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007065 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 break;
7067
7068 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007069 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007070 done = 1;
7071 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007074 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 done = 1;
7076 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007078 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007079 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007080 done = 1;
7081 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007084 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007085 done = 1;
7086 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007089 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7090 if (trash.len < 0)
7091 return -1;
7092
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007093 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007094 /* FIXME: if the user adds a newline in the replacement, the
7095 * index will not be recalculated for now, and the new line
7096 * will not be counted as a new header.
7097 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007098
Willy Tarreaufa355d42009-11-29 18:12:29 +01007099 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007101 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 HTTP_MSG_RQMETH,
7103 cur_ptr, cur_end + 1,
7104 NULL, NULL);
7105 if (unlikely(!cur_end))
7106 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007108 /* we have a full request and we know that we have either a CR
7109 * or an LF at <ptr>.
7110 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007111 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7112 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007113 /* there is no point trying this regex on headers */
7114 return 1;
7115 }
7116 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 return done;
7118}
Willy Tarreau97de6242006-12-27 17:18:38 +01007119
Willy Tarreau58f10d72006-12-04 02:26:12 +01007120
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007123 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125 * unparsable request. Since it can manage the switch to another backend, it
7126 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007128int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007130 struct http_txn *txn = &s->txn;
7131 struct hdr_exp *exp;
7132
7133 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007136 /*
7137 * The interleaving of transformations and verdicts
7138 * makes it difficult to decide to continue or stop
7139 * the evaluation.
7140 */
7141
Willy Tarreau6c123b12010-01-28 20:22:06 +01007142 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7143 break;
7144
Willy Tarreau3d300592007-03-18 18:34:41 +01007145 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007146 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007147 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007149
7150 /* if this filter had a condition, evaluate it now and skip to
7151 * next filter if the condition does not match.
7152 */
7153 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007154 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007155 ret = acl_pass(ret);
7156 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7157 ret = !ret;
7158
7159 if (!ret)
7160 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007161 }
7162
7163 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007164 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 if (unlikely(ret < 0))
7166 return -1;
7167
7168 if (likely(ret == 0)) {
7169 /* The filter did not match the request, it can be
7170 * iterated through all headers.
7171 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007172 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7173 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177}
7178
7179
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007182 * Try to retrieve the server associated to the appsession.
7183 * If the server is found, it's assigned to the session.
7184 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007185void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7186 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007187 appsess *asession = NULL;
7188 char *sessid_temp = NULL;
7189
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007190 if (len > s->be->appsession_len) {
7191 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007192 }
7193
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007194 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007195 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007196 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007197 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007198 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007199 }
7200
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007201 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007202 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007203 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007204 return;
7205 }
7206
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007207 memcpy(txn->sessid, buf, len);
7208 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007209 }
7210
7211 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7212 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007213 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007214 return;
7215 }
7216
Cyril Bontéb21570a2009-11-29 20:04:48 +01007217 memcpy(sessid_temp, buf, len);
7218 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007219
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007220 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007221 /* free previously allocated memory */
7222 pool_free2(apools.sessid, sessid_temp);
7223
7224 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007225 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7226 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007227 asession->request_count++;
7228
7229 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007230 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007231
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007232 while (srv) {
7233 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007234 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007235 (s->be->options & PR_O_PERSIST) ||
7236 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007237 /* we found the server and it's usable */
7238 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007239 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007240 s->flags |= SN_DIRECT | SN_ASSIGNED;
7241 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007242
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007243 break;
7244 } else {
7245 txn->flags &= ~TX_CK_MASK;
7246 txn->flags |= TX_CK_DOWN;
7247 }
7248 }
7249 srv = srv->next;
7250 }
7251 }
7252 }
7253}
7254
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007255/* Find the end of a cookie value contained between <s> and <e>. It works the
7256 * same way as with headers above except that the semi-colon also ends a token.
7257 * See RFC2965 for more information. Note that it requires a valid header to
7258 * return a valid result.
7259 */
7260char *find_cookie_value_end(char *s, const char *e)
7261{
7262 int quoted, qdpair;
7263
7264 quoted = qdpair = 0;
7265 for (; s < e; s++) {
7266 if (qdpair) qdpair = 0;
7267 else if (quoted) {
7268 if (*s == '\\') qdpair = 1;
7269 else if (*s == '"') quoted = 0;
7270 }
7271 else if (*s == '"') quoted = 1;
7272 else if (*s == ',' || *s == ';') return s;
7273 }
7274 return s;
7275}
7276
7277/* Delete a value in a header between delimiters <from> and <next> in buffer
7278 * <buf>. The number of characters displaced is returned, and the pointer to
7279 * the first delimiter is updated if required. The function tries as much as
7280 * possible to respect the following principles :
7281 * - replace <from> delimiter by the <next> one unless <from> points to a
7282 * colon, in which case <next> is simply removed
7283 * - set exactly one space character after the new first delimiter, unless
7284 * there are not enough characters in the block being moved to do so.
7285 * - remove unneeded spaces before the previous delimiter and after the new
7286 * one.
7287 *
7288 * It is the caller's responsibility to ensure that :
7289 * - <from> points to a valid delimiter or the colon ;
7290 * - <next> points to a valid delimiter or the final CR/LF ;
7291 * - there are non-space chars before <from> ;
7292 * - there is a CR/LF at or after <next>.
7293 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007294int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295{
7296 char *prev = *from;
7297
7298 if (*prev == ':') {
7299 /* We're removing the first value, preserve the colon and add a
7300 * space if possible.
7301 */
7302 if (!http_is_crlf[(unsigned char)*next])
7303 next++;
7304 prev++;
7305 if (prev < next)
7306 *prev++ = ' ';
7307
7308 while (http_is_spht[(unsigned char)*next])
7309 next++;
7310 } else {
7311 /* Remove useless spaces before the old delimiter. */
7312 while (http_is_spht[(unsigned char)*(prev-1)])
7313 prev--;
7314 *from = prev;
7315
7316 /* copy the delimiter and if possible a space if we're
7317 * not at the end of the line.
7318 */
7319 if (!http_is_crlf[(unsigned char)*next]) {
7320 *prev++ = *next++;
7321 if (prev + 1 < next)
7322 *prev++ = ' ';
7323 while (http_is_spht[(unsigned char)*next])
7324 next++;
7325 }
7326 }
7327 return buffer_replace2(buf, prev, next, NULL, 0);
7328}
7329
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007330/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007331 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007332 * desirable to call it only when needed. This code is quite complex because
7333 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7334 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007336void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007338 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007339 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007341 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7342 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007344 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007345 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007346 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007347
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007348 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007350 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007351
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007352 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007353 hdr_beg = hdr_next;
7354 hdr_end = hdr_beg + cur_hdr->len;
7355 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007356
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007357 /* We have one full header between hdr_beg and hdr_end, and the
7358 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359 * "Cookie:" headers.
7360 */
7361
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007362 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007363 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364 old_idx = cur_idx;
7365 continue;
7366 }
7367
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007368 del_from = NULL; /* nothing to be deleted */
7369 preserve_hdr = 0; /* assume we may kill the whole header */
7370
Willy Tarreau58f10d72006-12-04 02:26:12 +01007371 /* Now look for cookies. Conforming to RFC2109, we have to support
7372 * attributes whose name begin with a '$', and associate them with
7373 * the right cookie, if we want to delete this cookie.
7374 * So there are 3 cases for each cookie read :
7375 * 1) it's a special attribute, beginning with a '$' : ignore it.
7376 * 2) it's a server id cookie that we *MAY* want to delete : save
7377 * some pointers on it (last semi-colon, beginning of cookie...)
7378 * 3) it's an application cookie : we *MAY* have to delete a previous
7379 * "special" cookie.
7380 * At the end of loop, if a "special" cookie remains, we may have to
7381 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007382 * *MUST* delete it.
7383 *
7384 * Note: RFC2965 is unclear about the processing of spaces around
7385 * the equal sign in the ATTR=VALUE form. A careful inspection of
7386 * the RFC explicitly allows spaces before it, and not within the
7387 * tokens (attrs or values). An inspection of RFC2109 allows that
7388 * too but section 10.1.3 lets one think that spaces may be allowed
7389 * after the equal sign too, resulting in some (rare) buggy
7390 * implementations trying to do that. So let's do what servers do.
7391 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7392 * allowed quoted strings in values, with any possible character
7393 * after a backslash, including control chars and delimitors, which
7394 * causes parsing to become ambiguous. Browsers also allow spaces
7395 * within values even without quotes.
7396 *
7397 * We have to keep multiple pointers in order to support cookie
7398 * removal at the beginning, middle or end of header without
7399 * corrupting the header. All of these headers are valid :
7400 *
7401 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7402 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7403 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7404 * | | | | | | | | |
7405 * | | | | | | | | hdr_end <--+
7406 * | | | | | | | +--> next
7407 * | | | | | | +----> val_end
7408 * | | | | | +-----------> val_beg
7409 * | | | | +--------------> equal
7410 * | | | +----------------> att_end
7411 * | | +---------------------> att_beg
7412 * | +--------------------------> prev
7413 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414 */
7415
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007416 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7417 /* Iterate through all cookies on this line */
7418
7419 /* find att_beg */
7420 att_beg = prev + 1;
7421 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7422 att_beg++;
7423
7424 /* find att_end : this is the first character after the last non
7425 * space before the equal. It may be equal to hdr_end.
7426 */
7427 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007428
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007429 while (equal < hdr_end) {
7430 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007431 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007432 if (http_is_spht[(unsigned char)*equal++])
7433 continue;
7434 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435 }
7436
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007437 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7438 * is between <att_beg> and <equal>, both may be identical.
7439 */
7440
7441 /* look for end of cookie if there is an equal sign */
7442 if (equal < hdr_end && *equal == '=') {
7443 /* look for the beginning of the value */
7444 val_beg = equal + 1;
7445 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7446 val_beg++;
7447
7448 /* find the end of the value, respecting quotes */
7449 next = find_cookie_value_end(val_beg, hdr_end);
7450
7451 /* make val_end point to the first white space or delimitor after the value */
7452 val_end = next;
7453 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7454 val_end--;
7455 } else {
7456 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007457 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007458
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007459 /* We have nothing to do with attributes beginning with '$'. However,
7460 * they will automatically be removed if a header before them is removed,
7461 * since they're supposed to be linked together.
7462 */
7463 if (*att_beg == '$')
7464 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 /* Ignore cookies with no equal sign */
7467 if (equal == next) {
7468 /* This is not our cookie, so we must preserve it. But if we already
7469 * scheduled another cookie for removal, we cannot remove the
7470 * complete header, but we can remove the previous block itself.
7471 */
7472 preserve_hdr = 1;
7473 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007474 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007475 val_end += delta;
7476 next += delta;
7477 hdr_end += delta;
7478 hdr_next += delta;
7479 cur_hdr->len += delta;
7480 http_msg_move_end(&txn->req, delta);
7481 prev = del_from;
7482 del_from = NULL;
7483 }
7484 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 /* if there are spaces around the equal sign, we need to
7488 * strip them otherwise we'll get trouble for cookie captures,
7489 * or even for rewrites. Since this happens extremely rarely,
7490 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7493 int stripped_before = 0;
7494 int stripped_after = 0;
7495
7496 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007497 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 equal += stripped_before;
7499 val_beg += stripped_before;
7500 }
7501
7502 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007503 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 val_beg += stripped_after;
7505 stripped_before += stripped_after;
7506 }
7507
7508 val_end += stripped_before;
7509 next += stripped_before;
7510 hdr_end += stripped_before;
7511 hdr_next += stripped_before;
7512 cur_hdr->len += stripped_before;
7513 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007514 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007516
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007517 /* First, let's see if we want to capture this cookie. We check
7518 * that we don't already have a client side cookie, because we
7519 * can only capture one. Also as an optimisation, we ignore
7520 * cookies shorter than the declared name.
7521 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007522 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7523 (val_end - att_beg >= s->fe->capture_namelen) &&
7524 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007525 int log_len = val_end - att_beg;
7526
7527 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7528 Alert("HTTP logging : out of memory.\n");
7529 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007530 if (log_len > s->fe->capture_len)
7531 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 memcpy(txn->cli_cookie, att_beg, log_len);
7533 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007534 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536
Willy Tarreaubca99692010-10-06 19:25:55 +02007537 /* Persistence cookies in passive, rewrite or insert mode have the
7538 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007540 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007542 * For cookies in prefix mode, the form is :
7543 *
7544 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007546 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7547 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7548 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007550
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7552 * have the server ID between val_beg and delim, and the original cookie between
7553 * delim+1 and val_end. Otherwise, delim==val_end :
7554 *
7555 * Cookie: NAME=SRV; # in all but prefix modes
7556 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7557 * | || || | |+-> next
7558 * | || || | +--> val_end
7559 * | || || +---------> delim
7560 * | || |+------------> val_beg
7561 * | || +-------------> att_end = equal
7562 * | |+-----------------> att_beg
7563 * | +------------------> prev
7564 * +-------------------------> hdr_beg
7565 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 for (delim = val_beg; delim < val_end; delim++)
7569 if (*delim == COOKIE_DELIM)
7570 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007571 } else {
7572 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007574 /* Now check if the cookie contains a date field, which would
7575 * appear after a vertical bar ('|') just after the server name
7576 * and before the delimiter.
7577 */
7578 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7579 if (vbar1) {
7580 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007581 * right is the last seen date. It is a base64 encoded
7582 * 30-bit value representing the UNIX date since the
7583 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007584 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007585 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007586 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007587 if (val_end - vbar1 >= 5) {
7588 val = b64tos30(vbar1);
7589 if (val > 0)
7590 txn->cookie_last_date = val << 2;
7591 }
7592 /* look for a second vertical bar */
7593 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7594 if (vbar1 && (val_end - vbar1 > 5)) {
7595 val = b64tos30(vbar1 + 1);
7596 if (val > 0)
7597 txn->cookie_first_date = val << 2;
7598 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007599 }
7600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601
Willy Tarreauf64d1412010-10-07 20:06:11 +02007602 /* if the cookie has an expiration date and the proxy wants to check
7603 * it, then we do that now. We first check if the cookie is too old,
7604 * then only if it has expired. We detect strict overflow because the
7605 * time resolution here is not great (4 seconds). Cookies with dates
7606 * in the future are ignored if their offset is beyond one day. This
7607 * allows an admin to fix timezone issues without expiring everyone
7608 * and at the same time avoids keeping unwanted side effects for too
7609 * long.
7610 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007611 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7612 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007613 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007614 txn->flags &= ~TX_CK_MASK;
7615 txn->flags |= TX_CK_OLD;
7616 delim = val_beg; // let's pretend we have not found the cookie
7617 txn->cookie_first_date = 0;
7618 txn->cookie_last_date = 0;
7619 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007620 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7621 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007622 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007623 txn->flags &= ~TX_CK_MASK;
7624 txn->flags |= TX_CK_EXPIRED;
7625 delim = val_beg; // let's pretend we have not found the cookie
7626 txn->cookie_first_date = 0;
7627 txn->cookie_last_date = 0;
7628 }
7629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 /* Here, we'll look for the first running server which supports the cookie.
7631 * This allows to share a same cookie between several servers, for example
7632 * to dedicate backup servers to specific servers only.
7633 * However, to prevent clients from sticking to cookie-less backup server
7634 * when they have incidentely learned an empty cookie, we simply ignore
7635 * empty cookies and mark them as invalid.
7636 * The same behaviour is applied when persistence must be ignored.
7637 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007638 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007640
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 while (srv) {
7642 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7643 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007644 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007645 (s->be->options & PR_O_PERSIST) ||
7646 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 /* we found the server and we can use it */
7648 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007649 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007650 s->flags |= SN_DIRECT | SN_ASSIGNED;
7651 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007652 break;
7653 } else {
7654 /* we found a server, but it's down,
7655 * mark it as such and go on in case
7656 * another one is available.
7657 */
7658 txn->flags &= ~TX_CK_MASK;
7659 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007660 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007662 srv = srv->next;
7663 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664
Willy Tarreauf64d1412010-10-07 20:06:11 +02007665 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007666 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007668 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007669 txn->flags |= TX_CK_UNUSED;
7670 else
7671 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 /* depending on the cookie mode, we may have to either :
7675 * - delete the complete cookie if we're in insert+indirect mode, so that
7676 * the server never sees it ;
7677 * - remove the server id from the cookie value, and tag the cookie as an
7678 * application cookie so that it does not get accidentely removed later,
7679 * if we're in cookie prefix mode
7680 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007681 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007683
Willy Tarreau9b28e032012-10-12 23:49:43 +02007684 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 val_end += delta;
7686 next += delta;
7687 hdr_end += delta;
7688 hdr_next += delta;
7689 cur_hdr->len += delta;
7690 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 del_from = NULL;
7693 preserve_hdr = 1; /* we want to keep this cookie */
7694 }
7695 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007696 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 del_from = prev;
7698 }
7699 } else {
7700 /* This is not our cookie, so we must preserve it. But if we already
7701 * scheduled another cookie for removal, we cannot remove the
7702 * complete header, but we can remove the previous block itself.
7703 */
7704 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007707 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007708 if (att_beg >= del_from)
7709 att_beg += delta;
7710 if (att_end >= del_from)
7711 att_end += delta;
7712 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 val_end += delta;
7714 next += delta;
7715 hdr_end += delta;
7716 hdr_next += delta;
7717 cur_hdr->len += delta;
7718 http_msg_move_end(&txn->req, delta);
7719 prev = del_from;
7720 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007725 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 int cmp_len, value_len;
7727 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007728
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007729 if (s->be->options2 & PR_O2_AS_PFX) {
7730 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7731 value_begin = att_beg + s->be->appsession_name_len;
7732 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007733 } else {
7734 cmp_len = att_end - att_beg;
7735 value_begin = val_beg;
7736 value_len = val_end - val_beg;
7737 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007738
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007740 if (cmp_len == s->be->appsession_name_len &&
7741 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7742 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744 }
7745
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 /* continue with next cookie on this header line */
7747 att_beg = next;
7748 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 /* There are no more cookies on this line.
7751 * We may still have one (or several) marked for deletion at the
7752 * end of the line. We must do this now in two ways :
7753 * - if some cookies must be preserved, we only delete from the
7754 * mark to the end of line ;
7755 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007760 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762 cur_hdr->len += delta;
7763 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007764 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765
7766 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007767 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7768 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007769 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007770 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007771 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007773 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774 }
7775
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779}
7780
7781
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782/* Iterate the same filter through all response headers contained in <rtr>.
7783 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7784 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007785int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 char *cur_ptr, *cur_end, *cur_next;
7788 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007789 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007791 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792
7793 last_hdr = 0;
7794
Willy Tarreau9b28e032012-10-12 23:49:43 +02007795 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 old_idx = 0;
7797
7798 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007799 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007801 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 (exp->action == ACT_ALLOW ||
7803 exp->action == ACT_DENY))
7804 return 0;
7805
7806 cur_idx = txn->hdr_idx.v[old_idx].next;
7807 if (!cur_idx)
7808 break;
7809
7810 cur_hdr = &txn->hdr_idx.v[cur_idx];
7811 cur_ptr = cur_next;
7812 cur_end = cur_ptr + cur_hdr->len;
7813 cur_next = cur_end + cur_hdr->cr + 1;
7814
7815 /* Now we have one header between cur_ptr and cur_end,
7816 * and the next header starts at cur_next.
7817 */
7818
Willy Tarreau15a53a42015-01-21 13:39:42 +01007819 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 switch (exp->action) {
7821 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007822 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823 last_hdr = 1;
7824 break;
7825
7826 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007827 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 last_hdr = 1;
7829 break;
7830
7831 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007832 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7833 if (trash.len < 0)
7834 return -1;
7835
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007836 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 /* FIXME: if the user adds a newline in the replacement, the
7838 * index will not be recalculated for now, and the new line
7839 * will not be counted as a new header.
7840 */
7841
7842 cur_end += delta;
7843 cur_next += delta;
7844 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007845 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 break;
7847
7848 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007849 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 cur_next += delta;
7851
Willy Tarreaufa355d42009-11-29 18:12:29 +01007852 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7854 txn->hdr_idx.used--;
7855 cur_hdr->len = 0;
7856 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007857 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 break;
7859
7860 }
7861 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862
7863 /* keep the link from this header to next one in case of later
7864 * removal of next header.
7865 */
7866 old_idx = cur_idx;
7867 }
7868 return 0;
7869}
7870
7871
7872/* Apply the filter to the status line in the response buffer <rtr>.
7873 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7874 * or -1 if a replacement resulted in an invalid status line.
7875 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007876int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007878 char *cur_ptr, *cur_end;
7879 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007880 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007881 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007882
7883
Willy Tarreau3d300592007-03-18 18:34:41 +01007884 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007886 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887 (exp->action == ACT_ALLOW ||
7888 exp->action == ACT_DENY))
7889 return 0;
7890 else if (exp->action == ACT_REMOVE)
7891 return 0;
7892
7893 done = 0;
7894
Willy Tarreau9b28e032012-10-12 23:49:43 +02007895 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007896 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007897
7898 /* Now we have the status line between cur_ptr and cur_end */
7899
Willy Tarreau15a53a42015-01-21 13:39:42 +01007900 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 switch (exp->action) {
7902 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007903 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 done = 1;
7905 break;
7906
7907 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007908 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007909 done = 1;
7910 break;
7911
7912 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007913 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7914 if (trash.len < 0)
7915 return -1;
7916
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007917 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918 /* FIXME: if the user adds a newline in the replacement, the
7919 * index will not be recalculated for now, and the new line
7920 * will not be counted as a new header.
7921 */
7922
Willy Tarreaufa355d42009-11-29 18:12:29 +01007923 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007925 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007926 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 cur_ptr, cur_end + 1,
7928 NULL, NULL);
7929 if (unlikely(!cur_end))
7930 return -1;
7931
7932 /* we have a full respnse and we know that we have either a CR
7933 * or an LF at <ptr>.
7934 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007935 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007936 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007937 /* there is no point trying this regex on headers */
7938 return 1;
7939 }
7940 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941 return done;
7942}
7943
7944
7945
7946/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007947 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7949 * unparsable response.
7950 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007951int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007953 struct http_txn *txn = &s->txn;
7954 struct hdr_exp *exp;
7955
7956 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 int ret;
7958
7959 /*
7960 * The interleaving of transformations and verdicts
7961 * makes it difficult to decide to continue or stop
7962 * the evaluation.
7963 */
7964
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007965 if (txn->flags & TX_SVDENY)
7966 break;
7967
Willy Tarreau3d300592007-03-18 18:34:41 +01007968 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007969 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7970 exp->action == ACT_PASS)) {
7971 exp = exp->next;
7972 continue;
7973 }
7974
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007975 /* if this filter had a condition, evaluate it now and skip to
7976 * next filter if the condition does not match.
7977 */
7978 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007979 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007980 ret = acl_pass(ret);
7981 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7982 ret = !ret;
7983 if (!ret)
7984 continue;
7985 }
7986
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007988 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 if (unlikely(ret < 0))
7990 return -1;
7991
7992 if (likely(ret == 0)) {
7993 /* The filter did not match the response, it can be
7994 * iterated through all headers.
7995 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007996 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7997 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 }
8000 return 0;
8001}
8002
8003
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008005 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008006 * desirable to call it only when needed. This function is also used when we
8007 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008009void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008011 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01008012 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008013 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008015 char *hdr_beg, *hdr_end, *hdr_next;
8016 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 /* Iterate through the headers.
8019 * we start with the start line.
8020 */
8021 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008022 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023
8024 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8025 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008026 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027
8028 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 hdr_beg = hdr_next;
8030 hdr_end = hdr_beg + cur_hdr->len;
8031 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032
Willy Tarreau24581ba2010-08-31 22:39:35 +02008033 /* We have one full header between hdr_beg and hdr_end, and the
8034 * next header starts at hdr_next. We're only interested in
8035 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 */
8037
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 is_cookie2 = 0;
8039 prev = hdr_beg + 10;
8040 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008041 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008042 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8043 if (!val) {
8044 old_idx = cur_idx;
8045 continue;
8046 }
8047 is_cookie2 = 1;
8048 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 }
8050
Willy Tarreau24581ba2010-08-31 22:39:35 +02008051 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8052 * <prev> points to the colon.
8053 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008054 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055
Willy Tarreau24581ba2010-08-31 22:39:35 +02008056 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8057 * check-cache is enabled) and we are not interested in checking
8058 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008059 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008060 if (s->be->cookie_name == NULL &&
8061 s->be->appsession_name == NULL &&
8062 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 return;
8064
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 /* OK so now we know we have to process this response cookie.
8066 * The format of the Set-Cookie header is slightly different
8067 * from the format of the Cookie header in that it does not
8068 * support the comma as a cookie delimiter (thus the header
8069 * cannot be folded) because the Expires attribute described in
8070 * the original Netscape's spec may contain an unquoted date
8071 * with a comma inside. We have to live with this because
8072 * many browsers don't support Max-Age and some browsers don't
8073 * support quoted strings. However the Set-Cookie2 header is
8074 * clean.
8075 *
8076 * We have to keep multiple pointers in order to support cookie
8077 * removal at the beginning, middle or end of header without
8078 * corrupting the header (in case of set-cookie2). A special
8079 * pointer, <scav> points to the beginning of the set-cookie-av
8080 * fields after the first semi-colon. The <next> pointer points
8081 * either to the end of line (set-cookie) or next unquoted comma
8082 * (set-cookie2). All of these headers are valid :
8083 *
8084 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8085 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8086 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8087 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8088 * | | | | | | | | | |
8089 * | | | | | | | | +-> next hdr_end <--+
8090 * | | | | | | | +------------> scav
8091 * | | | | | | +--------------> val_end
8092 * | | | | | +--------------------> val_beg
8093 * | | | | +----------------------> equal
8094 * | | | +------------------------> att_end
8095 * | | +----------------------------> att_beg
8096 * | +------------------------------> prev
8097 * +-----------------------------------------> hdr_beg
8098 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099
Willy Tarreau24581ba2010-08-31 22:39:35 +02008100 for (; prev < hdr_end; prev = next) {
8101 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102
Willy Tarreau24581ba2010-08-31 22:39:35 +02008103 /* find att_beg */
8104 att_beg = prev + 1;
8105 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8106 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107
Willy Tarreau24581ba2010-08-31 22:39:35 +02008108 /* find att_end : this is the first character after the last non
8109 * space before the equal. It may be equal to hdr_end.
8110 */
8111 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112
Willy Tarreau24581ba2010-08-31 22:39:35 +02008113 while (equal < hdr_end) {
8114 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8115 break;
8116 if (http_is_spht[(unsigned char)*equal++])
8117 continue;
8118 att_end = equal;
8119 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120
Willy Tarreau24581ba2010-08-31 22:39:35 +02008121 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8122 * is between <att_beg> and <equal>, both may be identical.
8123 */
8124
8125 /* look for end of cookie if there is an equal sign */
8126 if (equal < hdr_end && *equal == '=') {
8127 /* look for the beginning of the value */
8128 val_beg = equal + 1;
8129 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8130 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008131
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 /* find the end of the value, respecting quotes */
8133 next = find_cookie_value_end(val_beg, hdr_end);
8134
8135 /* make val_end point to the first white space or delimitor after the value */
8136 val_end = next;
8137 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8138 val_end--;
8139 } else {
8140 /* <equal> points to next comma, semi-colon or EOL */
8141 val_beg = val_end = next = equal;
8142 }
8143
8144 if (next < hdr_end) {
8145 /* Set-Cookie2 supports multiple cookies, and <next> points to
8146 * a colon or semi-colon before the end. So skip all attr-value
8147 * pairs and look for the next comma. For Set-Cookie, since
8148 * commas are permitted in values, skip to the end.
8149 */
8150 if (is_cookie2)
8151 next = find_hdr_value_end(next, hdr_end);
8152 else
8153 next = hdr_end;
8154 }
8155
8156 /* Now everything is as on the diagram above */
8157
8158 /* Ignore cookies with no equal sign */
8159 if (equal == val_end)
8160 continue;
8161
8162 /* If there are spaces around the equal sign, we need to
8163 * strip them otherwise we'll get trouble for cookie captures,
8164 * or even for rewrites. Since this happens extremely rarely,
8165 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008167 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8168 int stripped_before = 0;
8169 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008170
Willy Tarreau24581ba2010-08-31 22:39:35 +02008171 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008172 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008173 equal += stripped_before;
8174 val_beg += stripped_before;
8175 }
8176
8177 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008178 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008179 val_beg += stripped_after;
8180 stripped_before += stripped_after;
8181 }
8182
8183 val_end += stripped_before;
8184 next += stripped_before;
8185 hdr_end += stripped_before;
8186 hdr_next += stripped_before;
8187 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008188 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008189 }
8190
8191 /* First, let's see if we want to capture this cookie. We check
8192 * that we don't already have a server side cookie, because we
8193 * can only capture one. Also as an optimisation, we ignore
8194 * cookies shorter than the declared name.
8195 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008196 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008197 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008198 (val_end - att_beg >= s->fe->capture_namelen) &&
8199 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008201 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 Alert("HTTP logging : out of memory.\n");
8203 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008204 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 if (log_len > s->fe->capture_len)
8206 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008207 memcpy(txn->srv_cookie, att_beg, log_len);
8208 txn->srv_cookie[log_len] = 0;
8209 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 }
8211
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008212 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008214 if (!(s->flags & SN_IGNORE_PRST) &&
8215 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8216 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008217 /* assume passive cookie by default */
8218 txn->flags &= ~TX_SCK_MASK;
8219 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220
8221 /* If the cookie is in insert mode on a known server, we'll delete
8222 * this occurrence because we'll insert another one later.
8223 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 * a direct access.
8225 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008226 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008227 /* The "preserve" flag was set, we don't want to touch the
8228 * server's cookie.
8229 */
8230 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008231 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8232 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 /* this cookie must be deleted */
8234 if (*prev == ':' && next == hdr_end) {
8235 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008236 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8238 txn->hdr_idx.used--;
8239 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008240 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 hdr_next += delta;
8242 http_msg_move_end(&txn->rsp, delta);
8243 /* note: while both invalid now, <next> and <hdr_end>
8244 * are still equal, so the for() will stop as expected.
8245 */
8246 } else {
8247 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008248 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 next = prev;
8250 hdr_end += delta;
8251 hdr_next += delta;
8252 cur_hdr->len += delta;
8253 http_msg_move_end(&txn->rsp, delta);
8254 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008255 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008256 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008259 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008260 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 * with this server since we know it.
8262 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008263 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 next += delta;
8265 hdr_end += delta;
8266 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008268 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269
Willy Tarreauf1348312010-10-07 15:54:11 +02008270 txn->flags &= ~TX_SCK_MASK;
8271 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008273 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008275 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008276 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008277 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008278 next += delta;
8279 hdr_end += delta;
8280 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008282 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283
Willy Tarreau827aee92011-03-10 16:55:02 +01008284 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008285 txn->flags &= ~TX_SCK_MASK;
8286 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008287 }
8288 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008289 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008291 int cmp_len, value_len;
8292 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008293
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008294 if (s->be->options2 & PR_O2_AS_PFX) {
8295 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8296 value_begin = att_beg + s->be->appsession_name_len;
8297 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008298 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008299 cmp_len = att_end - att_beg;
8300 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008301 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008302 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008303
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008304 if ((cmp_len == s->be->appsession_name_len) &&
8305 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 /* free a possibly previously allocated memory */
8307 pool_free2(apools.sessid, txn->sessid);
8308
Cyril Bontéb21570a2009-11-29 20:04:48 +01008309 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008310 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008311 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008312 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008313 return;
8314 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008315 memcpy(txn->sessid, value_begin, value_len);
8316 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008318 }
8319 /* that's done for this cookie, check the next one on the same
8320 * line when next != hdr_end (only if is_cookie2).
8321 */
8322 }
8323 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008326
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008327 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008328 appsess *asession = NULL;
8329 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008330 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008331 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008332 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008333 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8334 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008335 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008336 return;
8337 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008338 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8339
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008340 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8341 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008342 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8343 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008344 return;
8345 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008346 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8347 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008348
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008349 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008350 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008351 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008352 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8353 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008354 return;
8355 }
8356 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008357 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008358
8359 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008360 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008361 }
8362
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008363 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008364 asession->request_count++;
8365 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366}
8367
8368
Willy Tarreaua15645d2007-03-18 16:22:39 +01008369/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008370 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008371 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008372void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008374 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375 char *p1, *p2;
8376
8377 char *cur_ptr, *cur_end, *cur_next;
8378 int cur_idx;
8379
Willy Tarreau5df51872007-11-25 16:20:08 +01008380 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008381 return;
8382
8383 /* Iterate through the headers.
8384 * we start with the start line.
8385 */
8386 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008387 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008388
8389 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8390 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008391 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392
8393 cur_hdr = &txn->hdr_idx.v[cur_idx];
8394 cur_ptr = cur_next;
8395 cur_end = cur_ptr + cur_hdr->len;
8396 cur_next = cur_end + cur_hdr->cr + 1;
8397
8398 /* We have one full header between cur_ptr and cur_end, and the
8399 * next header starts at cur_next. We're only interested in
8400 * "Cookie:" headers.
8401 */
8402
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008403 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8404 if (val) {
8405 if ((cur_end - (cur_ptr + val) >= 8) &&
8406 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8407 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8408 return;
8409 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008410 }
8411
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008412 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8413 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414 continue;
8415
8416 /* OK, right now we know we have a cache-control header at cur_ptr */
8417
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008418 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419
8420 if (p1 >= cur_end) /* no more info */
8421 continue;
8422
8423 /* p1 is at the beginning of the value */
8424 p2 = p1;
8425
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008426 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008427 p2++;
8428
8429 /* we have a complete value between p1 and p2 */
8430 if (p2 < cur_end && *p2 == '=') {
8431 /* we have something of the form no-cache="set-cookie" */
8432 if ((cur_end - p1 >= 21) &&
8433 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8434 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008435 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 continue;
8437 }
8438
8439 /* OK, so we know that either p2 points to the end of string or to a comma */
8440 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008441 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008442 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8443 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8444 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008445 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446 return;
8447 }
8448
8449 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008450 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 continue;
8452 }
8453 }
8454}
8455
8456
Willy Tarreau58f10d72006-12-04 02:26:12 +01008457/*
8458 * Try to retrieve a known appsession in the URI, then the associated server.
8459 * If the server is found, it's assigned to the session.
8460 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008461void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008462{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008463 char *end_params, *first_param, *cur_param, *next_param;
8464 char separator;
8465 int value_len;
8466
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008467 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008468
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008469 if (s->be->appsession_name == NULL ||
8470 (s->txn.meth != HTTP_METH_GET && s->txn.meth != HTTP_METH_POST && s->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008471 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008472 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008473
Cyril Bontéb21570a2009-11-29 20:04:48 +01008474 first_param = NULL;
8475 switch (mode) {
8476 case PR_O2_AS_M_PP:
8477 first_param = memchr(begin, ';', len);
8478 break;
8479 case PR_O2_AS_M_QS:
8480 first_param = memchr(begin, '?', len);
8481 break;
8482 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008483
Cyril Bontéb21570a2009-11-29 20:04:48 +01008484 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008485 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008487
Cyril Bontéb21570a2009-11-29 20:04:48 +01008488 switch (mode) {
8489 case PR_O2_AS_M_PP:
8490 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8491 end_params = (char *) begin + len;
8492 }
8493 separator = ';';
8494 break;
8495 case PR_O2_AS_M_QS:
8496 end_params = (char *) begin + len;
8497 separator = '&';
8498 break;
8499 default:
8500 /* unknown mode, shouldn't happen */
8501 return;
8502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008503
Cyril Bontéb21570a2009-11-29 20:04:48 +01008504 cur_param = next_param = end_params;
8505 while (cur_param > first_param) {
8506 cur_param--;
8507 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8508 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8510 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8511 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008512 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8514 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008515 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008516 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008517 }
8518 break;
8519 }
8520 next_param = cur_param;
8521 }
8522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008523#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008524 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008525 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008526#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008527}
8528
Willy Tarreaub2513902006-12-17 14:52:38 +01008529/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008530 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008531 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008532 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008533 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008534 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008535 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008536 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008537 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008538int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008539{
8540 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008541 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008542 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008543
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008544 if (!uri_auth)
8545 return 0;
8546
Cyril Bonté70be45d2010-10-12 00:14:35 +02008547 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008548 return 0;
8549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008550 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008551 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008552 return 0;
8553
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008554 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008555 return 0;
8556
Willy Tarreaub2513902006-12-17 14:52:38 +01008557 return 1;
8558}
8559
Willy Tarreau4076a152009-04-02 15:18:36 +02008560/*
8561 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008562 * By default it tries to report the error position as msg->err_pos. However if
8563 * this one is not set, it will then report msg->next, which is the last known
8564 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008565 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008566 */
8567void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008568 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008569 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008570{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008571 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008572 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008573
Willy Tarreau9b28e032012-10-12 23:49:43 +02008574 es->len = MIN(chn->buf->i, sizeof(es->buf));
8575 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008576 len1 = MIN(len1, es->len);
8577 len2 = es->len - len1; /* remaining data if buffer wraps */
8578
Willy Tarreau9b28e032012-10-12 23:49:43 +02008579 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008580 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008581 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008582
Willy Tarreau4076a152009-04-02 15:18:36 +02008583 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008584 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008585 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008586 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008587
Willy Tarreau4076a152009-04-02 15:18:36 +02008588 es->when = date; // user-visible date
8589 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008590 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008591 es->oe = other_end;
Willy Tarreau350f4872014-11-28 14:42:25 +01008592 if (objt_conn(s->si[0].end))
8593 es->src = __objt_conn(s->si[0].end)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008594 else
8595 memset(&es->src, 0, sizeof(es->src));
8596
Willy Tarreau078272e2010-12-12 12:46:33 +01008597 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008598 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008599 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008600 es->s_flags = s->flags;
8601 es->t_flags = s->txn.flags;
8602 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008603 es->b_out = chn->buf->o;
8604 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008605 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008606 es->m_clen = msg->chunk_len;
8607 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008608}
Willy Tarreaub2513902006-12-17 14:52:38 +01008609
Willy Tarreau294c4732011-12-16 21:35:50 +01008610/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8611 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8612 * performed over the whole headers. Otherwise it must contain a valid header
8613 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8614 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8615 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8616 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008617 * -1. The value fetch stops at commas, so this function is suited for use with
8618 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008619 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008620 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008621unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008622 struct hdr_idx *idx, int occ,
8623 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008624{
Willy Tarreau294c4732011-12-16 21:35:50 +01008625 struct hdr_ctx local_ctx;
8626 char *ptr_hist[MAX_HDR_HISTORY];
8627 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008628 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008629 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008630
Willy Tarreau294c4732011-12-16 21:35:50 +01008631 if (!ctx) {
8632 local_ctx.idx = 0;
8633 ctx = &local_ctx;
8634 }
8635
Willy Tarreaubce70882009-09-07 11:51:47 +02008636 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008637 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008638 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008639 occ--;
8640 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008641 *vptr = ctx->line + ctx->val;
8642 *vlen = ctx->vlen;
8643 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008644 }
8645 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008646 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008647 }
8648
8649 /* negative occurrence, we scan all the list then walk back */
8650 if (-occ > MAX_HDR_HISTORY)
8651 return 0;
8652
Willy Tarreau294c4732011-12-16 21:35:50 +01008653 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008654 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008655 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8656 len_hist[hist_ptr] = ctx->vlen;
8657 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008658 hist_ptr = 0;
8659 found++;
8660 }
8661 if (-occ > found)
8662 return 0;
8663 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008664 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8665 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8666 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008667 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008668 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008669 if (hist_ptr >= MAX_HDR_HISTORY)
8670 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008671 *vptr = ptr_hist[hist_ptr];
8672 *vlen = len_hist[hist_ptr];
8673 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008674}
8675
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008676/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8677 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8678 * performed over the whole headers. Otherwise it must contain a valid header
8679 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8680 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8681 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8682 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8683 * -1. This function differs from http_get_hdr() in that it only returns full
8684 * line header values and does not stop at commas.
8685 * The return value is 0 if nothing was found, or non-zero otherwise.
8686 */
8687unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8688 struct hdr_idx *idx, int occ,
8689 struct hdr_ctx *ctx, char **vptr, int *vlen)
8690{
8691 struct hdr_ctx local_ctx;
8692 char *ptr_hist[MAX_HDR_HISTORY];
8693 int len_hist[MAX_HDR_HISTORY];
8694 unsigned int hist_ptr;
8695 int found;
8696
8697 if (!ctx) {
8698 local_ctx.idx = 0;
8699 ctx = &local_ctx;
8700 }
8701
8702 if (occ >= 0) {
8703 /* search from the beginning */
8704 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8705 occ--;
8706 if (occ <= 0) {
8707 *vptr = ctx->line + ctx->val;
8708 *vlen = ctx->vlen;
8709 return 1;
8710 }
8711 }
8712 return 0;
8713 }
8714
8715 /* negative occurrence, we scan all the list then walk back */
8716 if (-occ > MAX_HDR_HISTORY)
8717 return 0;
8718
8719 found = hist_ptr = 0;
8720 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8721 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8722 len_hist[hist_ptr] = ctx->vlen;
8723 if (++hist_ptr >= MAX_HDR_HISTORY)
8724 hist_ptr = 0;
8725 found++;
8726 }
8727 if (-occ > found)
8728 return 0;
8729 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8730 * find occurrence -occ, so we have to check [hist_ptr+occ].
8731 */
8732 hist_ptr += occ;
8733 if (hist_ptr >= MAX_HDR_HISTORY)
8734 hist_ptr -= MAX_HDR_HISTORY;
8735 *vptr = ptr_hist[hist_ptr];
8736 *vlen = len_hist[hist_ptr];
8737 return 1;
8738}
8739
Willy Tarreaubaaee002006-06-26 02:48:02 +02008740/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008741 * Print a debug line with a header. Always stop at the first CR or LF char,
8742 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8743 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008744 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008745void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008746{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008747 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008748 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008749 dir,
Willy Tarreau350f4872014-11-28 14:42:25 +01008750 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
8751 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008752
8753 for (max = 0; start + max < end; max++)
8754 if (start[max] == '\r' || start[max] == '\n')
8755 break;
8756
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008757 UBOUND(max, trash.size - trash.len - 3);
8758 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8759 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008760 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008761}
8762
Willy Tarreau0937bc42009-12-22 15:03:09 +01008763/*
8764 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8765 * the required fields are properly allocated and that we only need to (re)init
8766 * them. This should be used before processing any new request.
8767 */
8768void http_init_txn(struct session *s)
8769{
8770 struct http_txn *txn = &s->txn;
8771 struct proxy *fe = s->fe;
8772
8773 txn->flags = 0;
8774 txn->status = -1;
8775
Willy Tarreauf64d1412010-10-07 20:06:11 +02008776 txn->cookie_first_date = 0;
8777 txn->cookie_last_date = 0;
8778
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008779 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008780 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008781 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008782 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008783 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008784 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008785 txn->req.chunk_len = 0LL;
8786 txn->req.body_len = 0LL;
8787 txn->rsp.chunk_len = 0LL;
8788 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008789 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8790 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008791 txn->req.chn = &s->req;
8792 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008793
8794 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008795
8796 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8797 if (fe->options2 & PR_O2_REQBUG_OK)
8798 txn->req.err_pos = -1; /* let buggy requests pass */
8799
Willy Tarreau46023632010-01-07 22:51:47 +01008800 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008801 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8802
Willy Tarreau46023632010-01-07 22:51:47 +01008803 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008804 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8805
8806 if (txn->hdr_idx.v)
8807 hdr_idx_init(&txn->hdr_idx);
8808}
8809
8810/* to be used at the end of a transaction */
8811void http_end_txn(struct session *s)
8812{
8813 struct http_txn *txn = &s->txn;
8814
Willy Tarreau75195602014-03-11 15:48:55 +01008815 /* release any possible compression context */
8816 if (s->flags & SN_COMP_READY)
8817 s->comp_algo->end(&s->comp_ctx);
8818 s->comp_algo = NULL;
8819 s->flags &= ~SN_COMP_READY;
8820
Willy Tarreau0937bc42009-12-22 15:03:09 +01008821 /* these ones will have been dynamically allocated */
8822 pool_free2(pool2_requri, txn->uri);
8823 pool_free2(pool2_capture, txn->cli_cookie);
8824 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008825 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008826 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008827
William Lallemanda73203e2012-03-12 12:48:57 +01008828 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008829 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008830 txn->uri = NULL;
8831 txn->srv_cookie = NULL;
8832 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008833
8834 if (txn->req.cap) {
8835 struct cap_hdr *h;
8836 for (h = s->fe->req_cap; h; h = h->next)
8837 pool_free2(h->pool, txn->req.cap[h->index]);
8838 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8839 }
8840
8841 if (txn->rsp.cap) {
8842 struct cap_hdr *h;
8843 for (h = s->fe->rsp_cap; h; h = h->next)
8844 pool_free2(h->pool, txn->rsp.cap[h->index]);
8845 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8846 }
8847
Willy Tarreau0937bc42009-12-22 15:03:09 +01008848}
8849
8850/* to be used at the end of a transaction to prepare a new one */
8851void http_reset_txn(struct session *s)
8852{
8853 http_end_txn(s);
8854 http_init_txn(s);
8855
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008856 /* reinitialise the current rule list pointer to NULL. We are sure that
8857 * any rulelist match the NULL pointer.
8858 */
8859 s->current_rule_list = NULL;
8860
Willy Tarreau0937bc42009-12-22 15:03:09 +01008861 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008863 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008864 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008865 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008866 /* re-init store persistence */
8867 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008868 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008869
Willy Tarreau0937bc42009-12-22 15:03:09 +01008870 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008872 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008873
Willy Tarreau739cfba2010-01-25 23:11:14 +01008874 /* We must trim any excess data from the response buffer, because we
8875 * may have blocked an invalid response from a server that we don't
8876 * want to accidentely forward once we disable the analysers, nor do
8877 * we want those data to come along with next response. A typical
8878 * example of such data would be from a buggy server responding to
8879 * a HEAD with some data, or sending more than the advertised
8880 * content-length.
8881 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008882 if (unlikely(s->res.buf->i))
8883 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008884
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008885 s->req.rto = s->fe->timeout.client;
8886 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008887
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008888 s->res.rto = TICK_ETERNITY;
8889 s->res.wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008890
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008891 s->req.rex = TICK_ETERNITY;
8892 s->req.wex = TICK_ETERNITY;
8893 s->req.analyse_exp = TICK_ETERNITY;
8894 s->res.rex = TICK_ETERNITY;
8895 s->res.wex = TICK_ETERNITY;
8896 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008898
Sasha Pachev218f0642014-06-16 12:05:59 -06008899void free_http_res_rules(struct list *r)
8900{
8901 struct http_res_rule *tr, *pr;
8902
8903 list_for_each_entry_safe(pr, tr, r, list) {
8904 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008905 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008906 free(pr);
8907 }
8908}
8909
8910void free_http_req_rules(struct list *r)
8911{
Willy Tarreauff011f22011-01-06 17:51:27 +01008912 struct http_req_rule *tr, *pr;
8913
8914 list_for_each_entry_safe(pr, tr, r, list) {
8915 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008916 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008917 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008918
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008919 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008920 free(pr);
8921 }
8922}
8923
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008924/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008925struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8926{
8927 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008928 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008929 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008930 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008931
8932 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8933 if (!rule) {
8934 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008935 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008936 }
8937
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008938 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008939 rule->action = HTTP_REQ_ACT_ALLOW;
8940 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008941 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008942 rule->action = HTTP_REQ_ACT_DENY;
8943 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008944 } else if (!strcmp(args[0], "tarpit")) {
8945 rule->action = HTTP_REQ_ACT_TARPIT;
8946 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008947 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008948 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008949 cur_arg = 1;
8950
8951 while(*args[cur_arg]) {
8952 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008953 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008954 cur_arg+=2;
8955 continue;
8956 } else
8957 break;
8958 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008959 } else if (!strcmp(args[0], "set-nice")) {
8960 rule->action = HTTP_REQ_ACT_SET_NICE;
8961 cur_arg = 1;
8962
8963 if (!*args[cur_arg] ||
8964 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8965 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8966 file, linenum, args[0]);
8967 goto out_err;
8968 }
8969 rule->arg.nice = atoi(args[cur_arg]);
8970 if (rule->arg.nice < -1024)
8971 rule->arg.nice = -1024;
8972 else if (rule->arg.nice > 1024)
8973 rule->arg.nice = 1024;
8974 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008975 } else if (!strcmp(args[0], "set-tos")) {
8976#ifdef IP_TOS
8977 char *err;
8978 rule->action = HTTP_REQ_ACT_SET_TOS;
8979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8983 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8984 file, linenum, args[0]);
8985 goto out_err;
8986 }
8987
8988 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8989 if (err && *err != '\0') {
8990 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8991 file, linenum, err, args[0]);
8992 goto out_err;
8993 }
8994 cur_arg++;
8995#else
8996 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8997 goto out_err;
8998#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008999 } else if (!strcmp(args[0], "set-mark")) {
9000#ifdef SO_MARK
9001 char *err;
9002 rule->action = HTTP_REQ_ACT_SET_MARK;
9003 cur_arg = 1;
9004
9005 if (!*args[cur_arg] ||
9006 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9007 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9008 file, linenum, args[0]);
9009 goto out_err;
9010 }
9011
9012 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9013 if (err && *err != '\0') {
9014 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9015 file, linenum, err, args[0]);
9016 goto out_err;
9017 }
9018 cur_arg++;
9019 global.last_checks |= LSTCHK_NETADM;
9020#else
9021 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9022 goto out_err;
9023#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009024 } else if (!strcmp(args[0], "set-log-level")) {
9025 rule->action = HTTP_REQ_ACT_SET_LOGL;
9026 cur_arg = 1;
9027
9028 if (!*args[cur_arg] ||
9029 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9030 bad_log_level:
9031 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9032 file, linenum, args[0]);
9033 goto out_err;
9034 }
9035 if (strcmp(args[cur_arg], "silent") == 0)
9036 rule->arg.loglevel = -1;
9037 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9038 goto bad_log_level;
9039 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009040 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9041 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9042 cur_arg = 1;
9043
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009044 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9045 (*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 +01009046 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9047 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009048 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009049 }
9050
9051 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9052 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9053 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009054
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009055 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009056 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009057 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9058 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009059 free(proxy->conf.lfs_file);
9060 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9061 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009062 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009063 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9064 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009065 cur_arg = 1;
9066
9067 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009068 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06009069 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9070 file, linenum, args[0]);
9071 goto out_err;
9072 }
9073
9074 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9075 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9076 LIST_INIT(&rule->arg.hdr_add.fmt);
9077
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009078 error = NULL;
9079 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9080 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9081 args[cur_arg + 1], error);
9082 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009083 goto out_err;
9084 }
9085
9086 proxy->conf.args.ctx = ARGC_HRQ;
9087 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9088 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9089 file, linenum);
9090
9091 free(proxy->conf.lfs_file);
9092 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9093 proxy->conf.lfs_line = proxy->conf.args.line;
9094 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009095 } else if (strcmp(args[0], "del-header") == 0) {
9096 rule->action = HTTP_REQ_ACT_DEL_HDR;
9097 cur_arg = 1;
9098
9099 if (!*args[cur_arg] ||
9100 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9101 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9102 file, linenum, args[0]);
9103 goto out_err;
9104 }
9105
9106 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9107 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9108
9109 proxy->conf.args.ctx = ARGC_HRQ;
9110 free(proxy->conf.lfs_file);
9111 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9112 proxy->conf.lfs_line = proxy->conf.args.line;
9113 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009114 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9115 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009116 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009117 struct sample_expr *expr;
9118 unsigned int where;
9119 char *err = NULL;
9120
9121 cur_arg = 1;
9122 proxy->conf.args.ctx = ARGC_TRK;
9123
9124 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9125 if (!expr) {
9126 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9127 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9128 free(err);
9129 goto out_err;
9130 }
9131
9132 where = 0;
9133 if (proxy->cap & PR_CAP_FE)
9134 where |= SMP_VAL_FE_HRQ_HDR;
9135 if (proxy->cap & PR_CAP_BE)
9136 where |= SMP_VAL_BE_HRQ_HDR;
9137
9138 if (!(expr->fetch->val & where)) {
9139 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9140 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9141 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9142 args[cur_arg-1], sample_src_names(expr->fetch->use));
9143 free(expr);
9144 goto out_err;
9145 }
9146
9147 if (strcmp(args[cur_arg], "table") == 0) {
9148 cur_arg++;
9149 if (!args[cur_arg]) {
9150 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9151 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9152 free(expr);
9153 goto out_err;
9154 }
9155 /* we copy the table name for now, it will be resolved later */
9156 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9157 cur_arg++;
9158 }
9159 rule->act_prm.trk_ctr.expr = expr;
9160 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009161 } else if (strcmp(args[0], "redirect") == 0) {
9162 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009163 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009164
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009165 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009166 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9167 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9168 goto out_err;
9169 }
9170
9171 /* this redirect rule might already contain a parsed condition which
9172 * we'll pass to the http-request rule.
9173 */
9174 rule->action = HTTP_REQ_ACT_REDIR;
9175 rule->arg.redir = redir;
9176 rule->cond = redir->cond;
9177 redir->cond = NULL;
9178 cur_arg = 2;
9179 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009180 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9181 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9182 rule->action = HTTP_REQ_ACT_ADD_ACL;
9183 /*
9184 * '+ 8' for 'add-acl('
9185 * '- 9' for 'add-acl(' + trailing ')'
9186 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009187 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009188
9189 cur_arg = 1;
9190
9191 if (!*args[cur_arg] ||
9192 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9193 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9194 file, linenum, args[0]);
9195 goto out_err;
9196 }
9197
9198 LIST_INIT(&rule->arg.map.key);
9199 proxy->conf.args.ctx = ARGC_HRQ;
9200 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9201 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9202 file, linenum);
9203 free(proxy->conf.lfs_file);
9204 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9205 proxy->conf.lfs_line = proxy->conf.args.line;
9206 cur_arg += 1;
9207 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9208 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9209 rule->action = HTTP_REQ_ACT_DEL_ACL;
9210 /*
9211 * '+ 8' for 'del-acl('
9212 * '- 9' for 'del-acl(' + trailing ')'
9213 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009214 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009215
9216 cur_arg = 1;
9217
9218 if (!*args[cur_arg] ||
9219 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9220 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9221 file, linenum, args[0]);
9222 goto out_err;
9223 }
9224
9225 LIST_INIT(&rule->arg.map.key);
9226 proxy->conf.args.ctx = ARGC_HRQ;
9227 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9228 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9229 file, linenum);
9230 free(proxy->conf.lfs_file);
9231 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9232 proxy->conf.lfs_line = proxy->conf.args.line;
9233 cur_arg += 1;
9234 } else if (strncmp(args[0], "del-map", 7) == 0) {
9235 /* http-request del-map(<reference (map name)>) <key pattern> */
9236 rule->action = HTTP_REQ_ACT_DEL_MAP;
9237 /*
9238 * '+ 8' for 'del-map('
9239 * '- 9' for 'del-map(' + trailing ')'
9240 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009241 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009242
9243 cur_arg = 1;
9244
9245 if (!*args[cur_arg] ||
9246 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9247 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9248 file, linenum, args[0]);
9249 goto out_err;
9250 }
9251
9252 LIST_INIT(&rule->arg.map.key);
9253 proxy->conf.args.ctx = ARGC_HRQ;
9254 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9255 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9256 file, linenum);
9257 free(proxy->conf.lfs_file);
9258 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9259 proxy->conf.lfs_line = proxy->conf.args.line;
9260 cur_arg += 1;
9261 } else if (strncmp(args[0], "set-map", 7) == 0) {
9262 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9263 rule->action = HTTP_REQ_ACT_SET_MAP;
9264 /*
9265 * '+ 8' for 'set-map('
9266 * '- 9' for 'set-map(' + trailing ')'
9267 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009268 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009269
9270 cur_arg = 1;
9271
9272 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9273 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9274 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9275 file, linenum, args[0]);
9276 goto out_err;
9277 }
9278
9279 LIST_INIT(&rule->arg.map.key);
9280 LIST_INIT(&rule->arg.map.value);
9281 proxy->conf.args.ctx = ARGC_HRQ;
9282
9283 /* key pattern */
9284 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9285 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9286 file, linenum);
9287
9288 /* value pattern */
9289 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9290 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9291 file, linenum);
9292 free(proxy->conf.lfs_file);
9293 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9294 proxy->conf.lfs_line = proxy->conf.args.line;
9295
9296 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009297 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9298 char *errmsg = NULL;
9299 cur_arg = 1;
9300 /* try in the module list */
9301 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9302 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9303 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9304 free(errmsg);
9305 goto out_err;
9306 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009307 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009308 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009309 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009310 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009311 }
9312
9313 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9314 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009315 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009316
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009317 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9318 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9319 file, linenum, args[0], errmsg);
9320 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009321 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009322 }
9323 rule->cond = cond;
9324 }
9325 else if (*args[cur_arg]) {
9326 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9327 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9328 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009329 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009330 }
9331
9332 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009333 out_err:
9334 free(rule);
9335 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009336}
9337
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009338/* parse an "http-respose" rule */
9339struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9340{
9341 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009342 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009343 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009344 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009345
9346 rule = calloc(1, sizeof(*rule));
9347 if (!rule) {
9348 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9349 goto out_err;
9350 }
9351
9352 if (!strcmp(args[0], "allow")) {
9353 rule->action = HTTP_RES_ACT_ALLOW;
9354 cur_arg = 1;
9355 } else if (!strcmp(args[0], "deny")) {
9356 rule->action = HTTP_RES_ACT_DENY;
9357 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009358 } else if (!strcmp(args[0], "set-nice")) {
9359 rule->action = HTTP_RES_ACT_SET_NICE;
9360 cur_arg = 1;
9361
9362 if (!*args[cur_arg] ||
9363 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9364 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9365 file, linenum, args[0]);
9366 goto out_err;
9367 }
9368 rule->arg.nice = atoi(args[cur_arg]);
9369 if (rule->arg.nice < -1024)
9370 rule->arg.nice = -1024;
9371 else if (rule->arg.nice > 1024)
9372 rule->arg.nice = 1024;
9373 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009374 } else if (!strcmp(args[0], "set-tos")) {
9375#ifdef IP_TOS
9376 char *err;
9377 rule->action = HTTP_RES_ACT_SET_TOS;
9378 cur_arg = 1;
9379
9380 if (!*args[cur_arg] ||
9381 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9382 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9383 file, linenum, args[0]);
9384 goto out_err;
9385 }
9386
9387 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9388 if (err && *err != '\0') {
9389 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9390 file, linenum, err, args[0]);
9391 goto out_err;
9392 }
9393 cur_arg++;
9394#else
9395 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9396 goto out_err;
9397#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009398 } else if (!strcmp(args[0], "set-mark")) {
9399#ifdef SO_MARK
9400 char *err;
9401 rule->action = HTTP_RES_ACT_SET_MARK;
9402 cur_arg = 1;
9403
9404 if (!*args[cur_arg] ||
9405 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9406 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9407 file, linenum, args[0]);
9408 goto out_err;
9409 }
9410
9411 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9412 if (err && *err != '\0') {
9413 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9414 file, linenum, err, args[0]);
9415 goto out_err;
9416 }
9417 cur_arg++;
9418 global.last_checks |= LSTCHK_NETADM;
9419#else
9420 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9421 goto out_err;
9422#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009423 } else if (!strcmp(args[0], "set-log-level")) {
9424 rule->action = HTTP_RES_ACT_SET_LOGL;
9425 cur_arg = 1;
9426
9427 if (!*args[cur_arg] ||
9428 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9429 bad_log_level:
9430 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9431 file, linenum, args[0]);
9432 goto out_err;
9433 }
9434 if (strcmp(args[cur_arg], "silent") == 0)
9435 rule->arg.loglevel = -1;
9436 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9437 goto bad_log_level;
9438 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009439 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9440 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9441 cur_arg = 1;
9442
9443 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9444 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9445 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9446 file, linenum, args[0]);
9447 goto out_err;
9448 }
9449
9450 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9451 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9452 LIST_INIT(&rule->arg.hdr_add.fmt);
9453
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009454 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009455 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009456 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9457 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009458 free(proxy->conf.lfs_file);
9459 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9460 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009461 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009462 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009463 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009464 cur_arg = 1;
9465
9466 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009467 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9468 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009469 file, linenum, args[0]);
9470 goto out_err;
9471 }
9472
9473 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9474 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9475 LIST_INIT(&rule->arg.hdr_add.fmt);
9476
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009477 error = NULL;
9478 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9479 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9480 args[cur_arg + 1], error);
9481 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009482 goto out_err;
9483 }
9484
9485 proxy->conf.args.ctx = ARGC_HRQ;
9486 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9487 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9488 file, linenum);
9489
9490 free(proxy->conf.lfs_file);
9491 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9492 proxy->conf.lfs_line = proxy->conf.args.line;
9493 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009494 } else if (strcmp(args[0], "del-header") == 0) {
9495 rule->action = HTTP_RES_ACT_DEL_HDR;
9496 cur_arg = 1;
9497
9498 if (!*args[cur_arg] ||
9499 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9500 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9501 file, linenum, args[0]);
9502 goto out_err;
9503 }
9504
9505 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9506 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9507
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009508 proxy->conf.args.ctx = ARGC_HRS;
9509 free(proxy->conf.lfs_file);
9510 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9511 proxy->conf.lfs_line = proxy->conf.args.line;
9512 cur_arg += 1;
9513 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9514 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9515 rule->action = HTTP_RES_ACT_ADD_ACL;
9516 /*
9517 * '+ 8' for 'add-acl('
9518 * '- 9' for 'add-acl(' + trailing ')'
9519 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009520 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009521
9522 cur_arg = 1;
9523
9524 if (!*args[cur_arg] ||
9525 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9526 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9527 file, linenum, args[0]);
9528 goto out_err;
9529 }
9530
9531 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009532 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009533 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9534 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9535 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009536 free(proxy->conf.lfs_file);
9537 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9538 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009539
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009540 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009541 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9542 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9543 rule->action = HTTP_RES_ACT_DEL_ACL;
9544 /*
9545 * '+ 8' for 'del-acl('
9546 * '- 9' for 'del-acl(' + trailing ')'
9547 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009548 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009549
9550 cur_arg = 1;
9551
9552 if (!*args[cur_arg] ||
9553 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9554 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9555 file, linenum, args[0]);
9556 goto out_err;
9557 }
9558
9559 LIST_INIT(&rule->arg.map.key);
9560 proxy->conf.args.ctx = ARGC_HRS;
9561 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9562 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9563 file, linenum);
9564 free(proxy->conf.lfs_file);
9565 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9566 proxy->conf.lfs_line = proxy->conf.args.line;
9567 cur_arg += 1;
9568 } else if (strncmp(args[0], "del-map", 7) == 0) {
9569 /* http-response del-map(<reference (map name)>) <key pattern> */
9570 rule->action = HTTP_RES_ACT_DEL_MAP;
9571 /*
9572 * '+ 8' for 'del-map('
9573 * '- 9' for 'del-map(' + trailing ')'
9574 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009575 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009576
9577 cur_arg = 1;
9578
9579 if (!*args[cur_arg] ||
9580 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9581 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9582 file, linenum, args[0]);
9583 goto out_err;
9584 }
9585
9586 LIST_INIT(&rule->arg.map.key);
9587 proxy->conf.args.ctx = ARGC_HRS;
9588 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9589 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9590 file, linenum);
9591 free(proxy->conf.lfs_file);
9592 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9593 proxy->conf.lfs_line = proxy->conf.args.line;
9594 cur_arg += 1;
9595 } else if (strncmp(args[0], "set-map", 7) == 0) {
9596 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9597 rule->action = HTTP_RES_ACT_SET_MAP;
9598 /*
9599 * '+ 8' for 'set-map('
9600 * '- 9' for 'set-map(' + trailing ')'
9601 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009602 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009603
9604 cur_arg = 1;
9605
9606 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9607 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9608 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9609 file, linenum, args[0]);
9610 goto out_err;
9611 }
9612
9613 LIST_INIT(&rule->arg.map.key);
9614 LIST_INIT(&rule->arg.map.value);
9615
9616 proxy->conf.args.ctx = ARGC_HRS;
9617
9618 /* key pattern */
9619 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9620 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9621 file, linenum);
9622
9623 /* value pattern */
9624 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9625 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9626 file, linenum);
9627
9628 free(proxy->conf.lfs_file);
9629 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9630 proxy->conf.lfs_line = proxy->conf.args.line;
9631
9632 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009633 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9634 char *errmsg = NULL;
9635 cur_arg = 1;
9636 /* try in the module list */
9637 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9638 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9639 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9640 free(errmsg);
9641 goto out_err;
9642 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009643 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009644 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009645 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9646 goto out_err;
9647 }
9648
9649 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9650 struct acl_cond *cond;
9651 char *errmsg = NULL;
9652
9653 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9654 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9655 file, linenum, args[0], errmsg);
9656 free(errmsg);
9657 goto out_err;
9658 }
9659 rule->cond = cond;
9660 }
9661 else if (*args[cur_arg]) {
9662 Alert("parsing [%s:%d]: 'http-response %s' expects"
9663 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9664 file, linenum, args[0], args[cur_arg]);
9665 goto out_err;
9666 }
9667
9668 return rule;
9669 out_err:
9670 free(rule);
9671 return NULL;
9672}
9673
Willy Tarreau4baae242012-12-27 12:00:31 +01009674/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009675 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9676 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009677 */
9678struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009679 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009680{
9681 struct redirect_rule *rule;
9682 int cur_arg;
9683 int type = REDIRECT_TYPE_NONE;
9684 int code = 302;
9685 const char *destination = NULL;
9686 const char *cookie = NULL;
9687 int cookie_set = 0;
9688 unsigned int flags = REDIRECT_FLAG_NONE;
9689 struct acl_cond *cond = NULL;
9690
9691 cur_arg = 0;
9692 while (*(args[cur_arg])) {
9693 if (strcmp(args[cur_arg], "location") == 0) {
9694 if (!*args[cur_arg + 1])
9695 goto missing_arg;
9696
9697 type = REDIRECT_TYPE_LOCATION;
9698 cur_arg++;
9699 destination = args[cur_arg];
9700 }
9701 else if (strcmp(args[cur_arg], "prefix") == 0) {
9702 if (!*args[cur_arg + 1])
9703 goto missing_arg;
9704
9705 type = REDIRECT_TYPE_PREFIX;
9706 cur_arg++;
9707 destination = args[cur_arg];
9708 }
9709 else if (strcmp(args[cur_arg], "scheme") == 0) {
9710 if (!*args[cur_arg + 1])
9711 goto missing_arg;
9712
9713 type = REDIRECT_TYPE_SCHEME;
9714 cur_arg++;
9715 destination = args[cur_arg];
9716 }
9717 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9718 if (!*args[cur_arg + 1])
9719 goto missing_arg;
9720
9721 cur_arg++;
9722 cookie = args[cur_arg];
9723 cookie_set = 1;
9724 }
9725 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9726 if (!*args[cur_arg + 1])
9727 goto missing_arg;
9728
9729 cur_arg++;
9730 cookie = args[cur_arg];
9731 cookie_set = 0;
9732 }
9733 else if (strcmp(args[cur_arg], "code") == 0) {
9734 if (!*args[cur_arg + 1])
9735 goto missing_arg;
9736
9737 cur_arg++;
9738 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009739 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009740 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009741 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009742 args[cur_arg - 1], args[cur_arg]);
9743 return NULL;
9744 }
9745 }
9746 else if (!strcmp(args[cur_arg],"drop-query")) {
9747 flags |= REDIRECT_FLAG_DROP_QS;
9748 }
9749 else if (!strcmp(args[cur_arg],"append-slash")) {
9750 flags |= REDIRECT_FLAG_APPEND_SLASH;
9751 }
9752 else if (strcmp(args[cur_arg], "if") == 0 ||
9753 strcmp(args[cur_arg], "unless") == 0) {
9754 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9755 if (!cond) {
9756 memprintf(errmsg, "error in condition: %s", *errmsg);
9757 return NULL;
9758 }
9759 break;
9760 }
9761 else {
9762 memprintf(errmsg,
9763 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9764 args[cur_arg]);
9765 return NULL;
9766 }
9767 cur_arg++;
9768 }
9769
9770 if (type == REDIRECT_TYPE_NONE) {
9771 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9772 return NULL;
9773 }
9774
9775 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9776 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009777 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009778
9779 if (!use_fmt) {
9780 /* old-style static redirect rule */
9781 rule->rdr_str = strdup(destination);
9782 rule->rdr_len = strlen(destination);
9783 }
9784 else {
9785 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009786
9787 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9788 * if prefix == "/", we don't want to add anything, otherwise it
9789 * makes it hard for the user to configure a self-redirection.
9790 */
Godbachd9722032014-12-18 15:44:58 +08009791 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009792 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009793 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009794 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9795 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009796 free(curproxy->conf.lfs_file);
9797 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9798 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009799 }
9800 }
9801
Willy Tarreau4baae242012-12-27 12:00:31 +01009802 if (cookie) {
9803 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9804 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9805 */
9806 rule->cookie_len = strlen(cookie);
9807 if (cookie_set) {
9808 rule->cookie_str = malloc(rule->cookie_len + 10);
9809 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9810 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9811 rule->cookie_len += 9;
9812 } else {
9813 rule->cookie_str = malloc(rule->cookie_len + 21);
9814 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9815 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9816 rule->cookie_len += 20;
9817 }
9818 }
9819 rule->type = type;
9820 rule->code = code;
9821 rule->flags = flags;
9822 LIST_INIT(&rule->list);
9823 return rule;
9824
9825 missing_arg:
9826 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9827 return NULL;
9828}
9829
Willy Tarreau8797c062007-05-07 00:55:35 +02009830/************************************************************************/
9831/* The code below is dedicated to ACL parsing and matching */
9832/************************************************************************/
9833
9834
Willy Tarreau14174bc2012-04-16 14:34:04 +02009835/* This function ensures that the prerequisites for an L7 fetch are ready,
9836 * which means that a request or response is ready. If some data is missing,
9837 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009838 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9839 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009840 *
9841 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009842 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9843 * decide whether or not an HTTP message is present ;
9844 * 0 if the requested data cannot be fetched or if it is certain that
9845 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009846 * 1 if an HTTP message is ready
9847 */
9848static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009849smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009850 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009851{
9852 struct http_txn *txn = l7;
9853 struct http_msg *msg = &txn->req;
9854
9855 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9856 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9857 */
9858
9859 if (unlikely(!s || !txn))
9860 return 0;
9861
9862 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009863 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009864
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009865 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009866 /* If the buffer does not leave enough free space at the end,
9867 * we must first realign it.
9868 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009869 if (s->req.buf->p > s->req.buf->data &&
9870 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9871 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009872
Willy Tarreau14174bc2012-04-16 14:34:04 +02009873 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009874 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009875 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009876
9877 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009878 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009879 http_msg_analyzer(msg, &txn->hdr_idx);
9880
9881 /* Still no valid request ? */
9882 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009883 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009884 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009885 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009886 }
9887 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009888 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009889 return 0;
9890 }
9891
9892 /* OK we just got a valid HTTP request. We have some minor
9893 * preparation to perform so that further checks can rely
9894 * on HTTP tests.
9895 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009896
9897 /* If the request was parsed but was too large, we must absolutely
9898 * return an error so that it is not processed. At the moment this
9899 * cannot happen, but if the parsers are to change in the future,
9900 * we want this check to be maintained.
9901 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009902 if (unlikely(s->req.buf->i + s->req.buf->p >
9903 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009904 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009905 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009906 return 1;
9907 }
9908
Willy Tarreau9b28e032012-10-12 23:49:43 +02009909 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009910 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9911 s->flags |= SN_REDIRECTABLE;
9912
Willy Tarreau506d0502013-07-06 13:29:24 +02009913 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9914 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009915 }
9916
Willy Tarreau506d0502013-07-06 13:29:24 +02009917 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009918 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009919 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009920
9921 /* otherwise everything's ready for the request */
9922 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009923 else {
9924 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009925 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9926 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009927 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009928 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009929 }
9930
9931 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009932 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009933 return 1;
9934}
Willy Tarreau8797c062007-05-07 00:55:35 +02009935
Willy Tarreau6c616e02014-06-25 16:56:41 +02009936/* Note: these functinos *do* modify the sample. Even in case of success, at
9937 * least the type and uint value are modified.
9938 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009939#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009940 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 +02009941
Willy Tarreau24e32d82012-04-23 23:55:44 +02009942#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009943 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 +02009944
Willy Tarreau8797c062007-05-07 00:55:35 +02009945
9946/* 1. Check on METHOD
9947 * We use the pre-parsed method if it is known, and store its number as an
9948 * integer. If it is unknown, we use the pointer and the length.
9949 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009950static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009951{
9952 int len, meth;
9953
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009954 len = strlen(text);
9955 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009956
9957 pattern->val.i = meth;
9958 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009959 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009960 pattern->len = len;
9961 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009962 else {
9963 pattern->ptr.str = NULL;
9964 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009965 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009966 return 1;
9967}
9968
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009969/* This function fetches the method of current HTTP request and stores
9970 * it in the global pattern struct as a chunk. There are two possibilities :
9971 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9972 * in <len> and <ptr> is NULL ;
9973 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9974 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009975 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009976 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009977static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009978smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01009979 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009980{
9981 int meth;
9982 struct http_txn *txn = l7;
9983
Willy Tarreau24e32d82012-04-23 23:55:44 +02009984 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009985
Willy Tarreau8797c062007-05-07 00:55:35 +02009986 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009987 smp->type = SMP_T_METH;
9988 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009989 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009990 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9991 /* ensure the indexes are not affected */
9992 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009993 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009994 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9995 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009996 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009997 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009998 return 1;
9999}
10000
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010001/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010002static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010003{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010004 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010005 struct pattern_list *lst;
10006 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010007
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010008 list_for_each_entry(lst, &expr->patterns, list) {
10009 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010010
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010011 /* well-known method */
10012 if (pattern->val.i != HTTP_METH_OTHER) {
10013 if (smp->data.meth.meth == pattern->val.i)
10014 return pattern;
10015 else
10016 continue;
10017 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010018
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010019 /* Other method, we must compare the strings */
10020 if (pattern->len != smp->data.meth.str.len)
10021 continue;
10022
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010023 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010024 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10025 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010026 return pattern;
10027 }
10028 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010029}
10030
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010031static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010032smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010033 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010034{
10035 struct http_txn *txn = l7;
10036 char *ptr;
10037 int len;
10038
Willy Tarreauc0239e02012-04-16 14:42:55 +020010039 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010040
Willy Tarreau8797c062007-05-07 00:55:35 +020010041 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010042 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010043
10044 while ((len-- > 0) && (*ptr++ != '/'));
10045 if (len <= 0)
10046 return 0;
10047
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010048 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010049 smp->data.str.str = ptr;
10050 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010051
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010052 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010053 return 1;
10054}
10055
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010056static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010057smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010058 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010059{
10060 struct http_txn *txn = l7;
10061 char *ptr;
10062 int len;
10063
Willy Tarreauc0239e02012-04-16 14:42:55 +020010064 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010065
Willy Tarreauf26b2522012-12-14 08:33:14 +010010066 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10067 return 0;
10068
Willy Tarreau8797c062007-05-07 00:55:35 +020010069 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010070 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010071
10072 while ((len-- > 0) && (*ptr++ != '/'));
10073 if (len <= 0)
10074 return 0;
10075
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010076 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010077 smp->data.str.str = ptr;
10078 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010079
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010080 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010081 return 1;
10082}
10083
10084/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010085static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010086smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010087 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010088{
10089 struct http_txn *txn = l7;
10090 char *ptr;
10091 int len;
10092
Willy Tarreauc0239e02012-04-16 14:42:55 +020010093 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010094
Willy Tarreauf26b2522012-12-14 08:33:14 +010010095 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10096 return 0;
10097
Willy Tarreau8797c062007-05-07 00:55:35 +020010098 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010099 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010100
Willy Tarreauf853c462012-04-23 18:53:56 +020010101 smp->type = SMP_T_UINT;
10102 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010103 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010104 return 1;
10105}
10106
10107/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010108static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010109smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010110 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010111{
10112 struct http_txn *txn = l7;
10113
Willy Tarreauc0239e02012-04-16 14:42:55 +020010114 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010115
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010116 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010117 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010118 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010119 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010120 return 1;
10121}
10122
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010123static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010124smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010125 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010126{
10127 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010128 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010129
Willy Tarreauc0239e02012-04-16 14:42:55 +020010130 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010131
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010132 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010133 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010134 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010135
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010136 smp->type = SMP_T_IPV4;
10137 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010138 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010139 return 1;
10140}
10141
10142static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010143smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010144 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010145{
10146 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010147 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010148
Willy Tarreauc0239e02012-04-16 14:42:55 +020010149 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010150
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010151 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010152 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10153 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010154
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010155 smp->type = SMP_T_UINT;
10156 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010157 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010158 return 1;
10159}
10160
Willy Tarreau185b5c42012-04-26 15:11:51 +020010161/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10162 * Accepts an optional argument of type string containing the header field name,
10163 * and an optional argument of type signed or unsigned integer to request an
10164 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010165 * headers are considered from the first one. It does not stop on commas and
10166 * returns full lines instead (useful for User-Agent or Date for example).
10167 */
10168static int
10169smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010170 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010171{
10172 struct http_txn *txn = l7;
10173 struct hdr_idx *idx = &txn->hdr_idx;
10174 struct hdr_ctx *ctx = smp->ctx.a[0];
10175 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10176 int occ = 0;
10177 const char *name_str = NULL;
10178 int name_len = 0;
10179
10180 if (!ctx) {
10181 /* first call */
10182 ctx = &static_hdr_ctx;
10183 ctx->idx = 0;
10184 smp->ctx.a[0] = ctx;
10185 }
10186
10187 if (args) {
10188 if (args[0].type != ARGT_STR)
10189 return 0;
10190 name_str = args[0].data.str.str;
10191 name_len = args[0].data.str.len;
10192
10193 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10194 occ = args[1].data.uint;
10195 }
10196
10197 CHECK_HTTP_MESSAGE_FIRST();
10198
10199 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10200 /* search for header from the beginning */
10201 ctx->idx = 0;
10202
10203 if (!occ && !(opt & SMP_OPT_ITERATE))
10204 /* no explicit occurrence and single fetch => last header by default */
10205 occ = -1;
10206
10207 if (!occ)
10208 /* prepare to report multiple occurrences for ACL fetches */
10209 smp->flags |= SMP_F_NOT_LAST;
10210
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010211 smp->type = SMP_T_STR;
10212 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010213 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10214 return 1;
10215
10216 smp->flags &= ~SMP_F_NOT_LAST;
10217 return 0;
10218}
10219
10220/* 6. Check on HTTP header count. The number of occurrences is returned.
10221 * Accepts exactly 1 argument of type string. It does not stop on commas and
10222 * returns full lines instead (useful for User-Agent or Date for example).
10223 */
10224static int
10225smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010226 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010227{
10228 struct http_txn *txn = l7;
10229 struct hdr_idx *idx = &txn->hdr_idx;
10230 struct hdr_ctx ctx;
10231 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10232 int cnt;
10233
10234 if (!args || args->type != ARGT_STR)
10235 return 0;
10236
10237 CHECK_HTTP_MESSAGE_FIRST();
10238
10239 ctx.idx = 0;
10240 cnt = 0;
10241 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10242 cnt++;
10243
10244 smp->type = SMP_T_UINT;
10245 smp->data.uint = cnt;
10246 smp->flags = SMP_F_VOL_HDR;
10247 return 1;
10248}
10249
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010250static int
10251smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010252 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010253{
10254 struct http_txn *txn = l7;
10255 struct hdr_idx *idx = &txn->hdr_idx;
10256 struct hdr_ctx ctx;
10257 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10258 struct chunk *temp;
10259 char del = ',';
10260
10261 if (args && args->type == ARGT_STR)
10262 del = *args[0].data.str.str;
10263
10264 CHECK_HTTP_MESSAGE_FIRST();
10265
10266 temp = get_trash_chunk();
10267
10268 ctx.idx = 0;
10269 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10270 if (temp->len)
10271 temp->str[temp->len++] = del;
10272 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10273 temp->len += ctx.del;
10274 }
10275
10276 smp->type = SMP_T_STR;
10277 smp->data.str.str = temp->str;
10278 smp->data.str.len = temp->len;
10279 smp->flags = SMP_F_VOL_HDR;
10280 return 1;
10281}
10282
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010283/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10284 * Accepts an optional argument of type string containing the header field name,
10285 * and an optional argument of type signed or unsigned integer to request an
10286 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010287 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010288 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010289static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010290smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010291 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010292{
10293 struct http_txn *txn = l7;
10294 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010295 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010296 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010297 int occ = 0;
10298 const char *name_str = NULL;
10299 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010300
Willy Tarreaua890d072013-04-02 12:01:06 +020010301 if (!ctx) {
10302 /* first call */
10303 ctx = &static_hdr_ctx;
10304 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010305 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010306 }
10307
Willy Tarreau185b5c42012-04-26 15:11:51 +020010308 if (args) {
10309 if (args[0].type != ARGT_STR)
10310 return 0;
10311 name_str = args[0].data.str.str;
10312 name_len = args[0].data.str.len;
10313
10314 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10315 occ = args[1].data.uint;
10316 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010317
Willy Tarreaue333ec92012-04-16 16:26:40 +020010318 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010319
Willy Tarreau185b5c42012-04-26 15:11:51 +020010320 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010321 /* search for header from the beginning */
10322 ctx->idx = 0;
10323
Willy Tarreau185b5c42012-04-26 15:11:51 +020010324 if (!occ && !(opt & SMP_OPT_ITERATE))
10325 /* no explicit occurrence and single fetch => last header by default */
10326 occ = -1;
10327
10328 if (!occ)
10329 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010330 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010331
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010332 smp->type = SMP_T_STR;
10333 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010334 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 +020010335 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010336
Willy Tarreau37406352012-04-23 16:16:37 +020010337 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010338 return 0;
10339}
10340
Willy Tarreauc11416f2007-06-17 16:58:38 +020010341/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010342 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010343 */
10344static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010345smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010346 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010347{
10348 struct http_txn *txn = l7;
10349 struct hdr_idx *idx = &txn->hdr_idx;
10350 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010351 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010352 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010353
Willy Tarreau24e32d82012-04-23 23:55:44 +020010354 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010355 return 0;
10356
Willy Tarreaue333ec92012-04-16 16:26:40 +020010357 CHECK_HTTP_MESSAGE_FIRST();
10358
Willy Tarreau33a7e692007-06-10 19:45:56 +020010359 ctx.idx = 0;
10360 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010361 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 +020010362 cnt++;
10363
Willy Tarreauf853c462012-04-23 18:53:56 +020010364 smp->type = SMP_T_UINT;
10365 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010366 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010367 return 1;
10368}
10369
Willy Tarreau185b5c42012-04-26 15:11:51 +020010370/* Fetch an HTTP header's integer value. The integer value is returned. It
10371 * takes a mandatory argument of type string and an optional one of type int
10372 * to designate a specific occurrence. It returns an unsigned integer, which
10373 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010374 */
10375static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010376smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010377 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010378{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010379 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010380
Willy Tarreauf853c462012-04-23 18:53:56 +020010381 if (ret > 0) {
10382 smp->type = SMP_T_UINT;
10383 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10384 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010385
Willy Tarreaud53e2422012-04-16 17:21:11 +020010386 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010387}
10388
Cyril Bonté69fa9922012-10-25 00:01:06 +020010389/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10390 * and an optional one of type int to designate a specific occurrence.
10391 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010392 */
10393static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010394smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010395 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010396{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010397 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010398
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010399 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010400 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10401 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010402 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010403 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010404 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010405 if (smp->data.str.len < temp->size - 1) {
10406 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10407 temp->str[smp->data.str.len] = '\0';
10408 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10409 smp->type = SMP_T_IPV6;
10410 break;
10411 }
10412 }
10413 }
10414
Willy Tarreaud53e2422012-04-16 17:21:11 +020010415 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010416 if (!(smp->flags & SMP_F_NOT_LAST))
10417 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010418 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010419 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010420}
10421
Willy Tarreau737b0c12007-06-10 21:28:46 +020010422/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10423 * the first '/' after the possible hostname, and ends before the possible '?'.
10424 */
10425static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010426smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010427 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010428{
10429 struct http_txn *txn = l7;
10430 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010431
Willy Tarreauc0239e02012-04-16 14:42:55 +020010432 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010433
Willy Tarreau9b28e032012-10-12 23:49:43 +020010434 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010435 ptr = http_get_path(txn);
10436 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010437 return 0;
10438
10439 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010440 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010441 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010442
10443 while (ptr < end && *ptr != '?')
10444 ptr++;
10445
Willy Tarreauf853c462012-04-23 18:53:56 +020010446 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010447 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010448 return 1;
10449}
10450
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010451/* This produces a concatenation of the first occurrence of the Host header
10452 * followed by the path component if it begins with a slash ('/'). This means
10453 * that '*' will not be added, resulting in exactly the first Host entry.
10454 * If no Host header is found, then the path is returned as-is. The returned
10455 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010456 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010457 */
10458static int
10459smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010460 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010461{
10462 struct http_txn *txn = l7;
10463 char *ptr, *end, *beg;
10464 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010465 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010466
10467 CHECK_HTTP_MESSAGE_FIRST();
10468
10469 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010470 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010471 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010472
10473 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010474 temp = get_trash_chunk();
10475 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010476 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010477 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010478 smp->data.str.len = ctx.vlen;
10479
10480 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010481 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010482 beg = http_get_path(txn);
10483 if (!beg)
10484 beg = end;
10485
10486 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10487
10488 if (beg < ptr && *beg == '/') {
10489 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10490 smp->data.str.len += ptr - beg;
10491 }
10492
10493 smp->flags = SMP_F_VOL_1ST;
10494 return 1;
10495}
10496
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010497/* This produces a 32-bit hash of the concatenation of the first occurrence of
10498 * the Host header followed by the path component if it begins with a slash ('/').
10499 * This means that '*' will not be added, resulting in exactly the first Host
10500 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010501 * is hashed using the path hash followed by a full avalanche hash and provides a
10502 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010503 * high-traffic sites without having to store whole paths.
10504 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010505int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010506smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010507 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010508{
10509 struct http_txn *txn = l7;
10510 struct hdr_ctx ctx;
10511 unsigned int hash = 0;
10512 char *ptr, *beg, *end;
10513 int len;
10514
10515 CHECK_HTTP_MESSAGE_FIRST();
10516
10517 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010518 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010519 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10520 ptr = ctx.line + ctx.val;
10521 len = ctx.vlen;
10522 while (len--)
10523 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10524 }
10525
10526 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010527 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010528 beg = http_get_path(txn);
10529 if (!beg)
10530 beg = end;
10531
10532 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10533
10534 if (beg < ptr && *beg == '/') {
10535 while (beg < ptr)
10536 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10537 }
10538 hash = full_hash(hash);
10539
10540 smp->type = SMP_T_UINT;
10541 smp->data.uint = hash;
10542 smp->flags = SMP_F_VOL_1ST;
10543 return 1;
10544}
10545
Willy Tarreau4a550602012-12-09 14:53:32 +010010546/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010547 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10548 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10549 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010550 * that in environments where IPv6 is insignificant, truncating the output to
10551 * 8 bytes would still work.
10552 */
10553static int
10554smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010555 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010556{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010557 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010558 struct connection *cli_conn = objt_conn(l4->si[0].end);
10559
10560 if (!cli_conn)
10561 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010562
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010563 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010564 return 0;
10565
Willy Tarreau47ca5452012-12-23 20:22:19 +010010566 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010567 *(unsigned int *)temp->str = htonl(smp->data.uint);
10568 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010569
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010570 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010571 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010572 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010573 temp->len += 4;
10574 break;
10575 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010576 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010577 temp->len += 16;
10578 break;
10579 default:
10580 return 0;
10581 }
10582
10583 smp->data.str = *temp;
10584 smp->type = SMP_T_BIN;
10585 return 1;
10586}
10587
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010588/* Extracts the query string, which comes after the question mark '?'. If no
10589 * question mark is found, nothing is returned. Otherwise it returns a sample
10590 * of type string carrying the whole query string.
10591 */
10592static int
10593smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010594 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010595{
10596 struct http_txn *txn = l7;
10597 char *ptr, *end;
10598
10599 CHECK_HTTP_MESSAGE_FIRST();
10600
10601 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10602 end = ptr + txn->req.sl.rq.u_l;
10603
10604 /* look up the '?' */
10605 do {
10606 if (ptr == end)
10607 return 0;
10608 } while (*ptr++ != '?');
10609
10610 smp->type = SMP_T_STR;
10611 smp->data.str.str = ptr;
10612 smp->data.str.len = end - ptr;
10613 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10614 return 1;
10615}
10616
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010617static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010618smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010619 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010620{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010621 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10622 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10623 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010624
Willy Tarreau24e32d82012-04-23 23:55:44 +020010625 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010626
Willy Tarreauf853c462012-04-23 18:53:56 +020010627 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010628 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010629 return 1;
10630}
10631
Willy Tarreau7f18e522010-10-22 20:04:13 +020010632/* return a valid test if the current request is the first one on the connection */
10633static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010634smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010635 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010636{
10637 if (!s)
10638 return 0;
10639
Willy Tarreauf853c462012-04-23 18:53:56 +020010640 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010641 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010642 return 1;
10643}
10644
Willy Tarreau34db1082012-04-19 17:16:54 +020010645/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010646static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010647smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010648 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010649{
10650
Willy Tarreau24e32d82012-04-23 23:55:44 +020010651 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010652 return 0;
10653
Willy Tarreauc0239e02012-04-16 14:42:55 +020010654 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010655
Willy Tarreauc0239e02012-04-16 14:42:55 +020010656 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010657 return 0;
10658
Willy Tarreauf853c462012-04-23 18:53:56 +020010659 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010660 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010661 return 1;
10662}
Willy Tarreau8797c062007-05-07 00:55:35 +020010663
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010664/* Accepts exactly 1 argument of type userlist */
10665static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010666smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010667 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010668{
10669
10670 if (!args || args->type != ARGT_USR)
10671 return 0;
10672
10673 CHECK_HTTP_MESSAGE_FIRST();
10674
10675 if (!get_http_auth(l4))
10676 return 0;
10677
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010678 /* if the user does not belong to the userlist or has a wrong password,
10679 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010680 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010681 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010682 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10683 return 0;
10684
10685 /* pat_match_auth() will need the user list */
10686 smp->ctx.a[0] = args->data.usr;
10687
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010688 smp->type = SMP_T_STR;
10689 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010690 smp->data.str.str = l4->txn.auth.user;
10691 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010692
10693 return 1;
10694}
10695
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010696/* Try to find the next occurrence of a cookie name in a cookie header value.
10697 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10698 * the cookie value is returned into *value and *value_l, and the function
10699 * returns a pointer to the next pointer to search from if the value was found.
10700 * Otherwise if the cookie was not found, NULL is returned and neither value
10701 * nor value_l are touched. The input <hdr> string should first point to the
10702 * header's value, and the <hdr_end> pointer must point to the first character
10703 * not part of the value. <list> must be non-zero if value may represent a list
10704 * of values (cookie headers). This makes it faster to abort parsing when no
10705 * list is expected.
10706 */
10707static char *
10708extract_cookie_value(char *hdr, const char *hdr_end,
10709 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010710 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010711{
10712 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10713 char *next;
10714
10715 /* we search at least a cookie name followed by an equal, and more
10716 * generally something like this :
10717 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10718 */
10719 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10720 /* Iterate through all cookies on this line */
10721
10722 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10723 att_beg++;
10724
10725 /* find att_end : this is the first character after the last non
10726 * space before the equal. It may be equal to hdr_end.
10727 */
10728 equal = att_end = att_beg;
10729
10730 while (equal < hdr_end) {
10731 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10732 break;
10733 if (http_is_spht[(unsigned char)*equal++])
10734 continue;
10735 att_end = equal;
10736 }
10737
10738 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10739 * is between <att_beg> and <equal>, both may be identical.
10740 */
10741
10742 /* look for end of cookie if there is an equal sign */
10743 if (equal < hdr_end && *equal == '=') {
10744 /* look for the beginning of the value */
10745 val_beg = equal + 1;
10746 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10747 val_beg++;
10748
10749 /* find the end of the value, respecting quotes */
10750 next = find_cookie_value_end(val_beg, hdr_end);
10751
10752 /* make val_end point to the first white space or delimitor after the value */
10753 val_end = next;
10754 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10755 val_end--;
10756 } else {
10757 val_beg = val_end = next = equal;
10758 }
10759
10760 /* We have nothing to do with attributes beginning with '$'. However,
10761 * they will automatically be removed if a header before them is removed,
10762 * since they're supposed to be linked together.
10763 */
10764 if (*att_beg == '$')
10765 continue;
10766
10767 /* Ignore cookies with no equal sign */
10768 if (equal == next)
10769 continue;
10770
10771 /* Now we have the cookie name between att_beg and att_end, and
10772 * its value between val_beg and val_end.
10773 */
10774
10775 if (att_end - att_beg == cookie_name_l &&
10776 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10777 /* let's return this value and indicate where to go on from */
10778 *value = val_beg;
10779 *value_l = val_end - val_beg;
10780 return next + 1;
10781 }
10782
10783 /* Set-Cookie headers only have the name in the first attr=value part */
10784 if (!list)
10785 break;
10786 }
10787
10788 return NULL;
10789}
10790
William Lallemanda43ba4e2014-01-28 18:14:25 +010010791/* Fetch a captured HTTP request header. The index is the position of
10792 * the "capture" option in the configuration file
10793 */
10794static int
10795smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010796 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010797{
10798 struct proxy *fe = l4->fe;
10799 struct http_txn *txn = l7;
10800 int idx;
10801
10802 if (!args || args->type != ARGT_UINT)
10803 return 0;
10804
10805 idx = args->data.uint;
10806
10807 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10808 return 0;
10809
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010810 smp->type = SMP_T_STR;
10811 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010812 smp->data.str.str = txn->req.cap[idx];
10813 smp->data.str.len = strlen(txn->req.cap[idx]);
10814
10815 return 1;
10816}
10817
10818/* Fetch a captured HTTP response header. The index is the position of
10819 * the "capture" option in the configuration file
10820 */
10821static int
10822smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010823 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010824{
10825 struct proxy *fe = l4->fe;
10826 struct http_txn *txn = l7;
10827 int idx;
10828
10829 if (!args || args->type != ARGT_UINT)
10830 return 0;
10831
10832 idx = args->data.uint;
10833
10834 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10835 return 0;
10836
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010837 smp->type = SMP_T_STR;
10838 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010839 smp->data.str.str = txn->rsp.cap[idx];
10840 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10841
10842 return 1;
10843}
10844
William Lallemand65ad6e12014-01-31 15:08:02 +010010845/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10846static int
10847smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010848 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010849{
10850 struct chunk *temp;
10851 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010852 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010853
10854 if (!txn->uri)
10855 return 0;
10856
William Lallemand96a77852014-02-05 00:30:02 +010010857 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010858
William Lallemand96a77852014-02-05 00:30:02 +010010859 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10860 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010861
William Lallemand96a77852014-02-05 00:30:02 +010010862 temp = get_trash_chunk();
10863 temp->str = txn->uri;
10864 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010865 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010866 smp->type = SMP_T_STR;
10867 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010868
10869 return 1;
10870
10871}
10872
10873/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10874static int
10875smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010876 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010877{
10878 struct chunk *temp;
10879 struct http_txn *txn = l7;
10880 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010881
10882 if (!txn->uri)
10883 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010884
William Lallemand65ad6e12014-01-31 15:08:02 +010010885 ptr = txn->uri;
10886
10887 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10888 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010889
William Lallemand65ad6e12014-01-31 15:08:02 +010010890 if (!*ptr)
10891 return 0;
10892
10893 ptr++; /* skip the space */
10894
10895 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010896 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010897 if (!ptr)
10898 return 0;
10899 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10900 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010901
10902 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010903 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010904 smp->type = SMP_T_STR;
10905 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010906
10907 return 1;
10908}
10909
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010910/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10911 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10912 */
10913static int
10914smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010915 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010916{
10917 struct http_txn *txn = l7;
10918
10919 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10920 return 0;
10921
10922 if (txn->req.flags & HTTP_MSGF_VER_11)
10923 smp->data.str.str = "HTTP/1.1";
10924 else
10925 smp->data.str.str = "HTTP/1.0";
10926
10927 smp->data.str.len = 8;
10928 smp->type = SMP_T_STR;
10929 smp->flags = SMP_F_CONST;
10930 return 1;
10931
10932}
10933
10934/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10935 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10936 */
10937static int
10938smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010939 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010940{
10941 struct http_txn *txn = l7;
10942
10943 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10944 return 0;
10945
10946 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10947 smp->data.str.str = "HTTP/1.1";
10948 else
10949 smp->data.str.str = "HTTP/1.0";
10950
10951 smp->data.str.len = 8;
10952 smp->type = SMP_T_STR;
10953 smp->flags = SMP_F_CONST;
10954 return 1;
10955
10956}
10957
William Lallemand65ad6e12014-01-31 15:08:02 +010010958
Willy Tarreaue333ec92012-04-16 16:26:40 +020010959/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010960 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010961 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010963 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010964 * Accepts exactly 1 argument of type string. If the input options indicate
10965 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010966 * The returned sample is of type CSTR. Can be used to parse cookies in other
10967 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010968 */
William Lallemand07c8b242014-05-02 17:11:07 +020010969int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010970 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010971{
10972 struct http_txn *txn = l7;
10973 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010974 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010975 const struct http_msg *msg;
10976 const char *hdr_name;
10977 int hdr_name_len;
10978 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010979 int occ = 0;
10980 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010981
Willy Tarreau24e32d82012-04-23 23:55:44 +020010982 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010983 return 0;
10984
Willy Tarreaua890d072013-04-02 12:01:06 +020010985 if (!ctx) {
10986 /* first call */
10987 ctx = &static_hdr_ctx;
10988 ctx->idx = 0;
10989 smp->ctx.a[2] = ctx;
10990 }
10991
Willy Tarreaue333ec92012-04-16 16:26:40 +020010992 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010993
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010994 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010995 msg = &txn->req;
10996 hdr_name = "Cookie";
10997 hdr_name_len = 6;
10998 } else {
10999 msg = &txn->rsp;
11000 hdr_name = "Set-Cookie";
11001 hdr_name_len = 10;
11002 }
11003
Willy Tarreau28376d62012-04-26 21:26:10 +020011004 if (!occ && !(opt & SMP_OPT_ITERATE))
11005 /* no explicit occurrence and single fetch => last cookie by default */
11006 occ = -1;
11007
11008 /* OK so basically here, either we want only one value and it's the
11009 * last one, or we want to iterate over all of them and we fetch the
11010 * next one.
11011 */
11012
Willy Tarreau9b28e032012-10-12 23:49:43 +020011013 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011014 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011015 /* search for the header from the beginning, we must first initialize
11016 * the search parameters.
11017 */
Willy Tarreau37406352012-04-23 16:16:37 +020011018 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011019 ctx->idx = 0;
11020 }
11021
Willy Tarreau28376d62012-04-26 21:26:10 +020011022 smp->flags |= SMP_F_VOL_HDR;
11023
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011024 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011025 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11026 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011027 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11028 goto out;
11029
Willy Tarreau24e32d82012-04-23 23:55:44 +020011030 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011031 continue;
11032
Willy Tarreau37406352012-04-23 16:16:37 +020011033 smp->ctx.a[0] = ctx->line + ctx->val;
11034 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011035 }
11036
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011037 smp->type = SMP_T_STR;
11038 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011039 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011040 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011041 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011042 &smp->data.str.str,
11043 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011044 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011045 found = 1;
11046 if (occ >= 0) {
11047 /* one value was returned into smp->data.str.{str,len} */
11048 smp->flags |= SMP_F_NOT_LAST;
11049 return 1;
11050 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011051 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011052 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011053 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011054 /* all cookie headers and values were scanned. If we're looking for the
11055 * last occurrence, we may return it now.
11056 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011057 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011058 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011059 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011060}
11061
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011062/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011063 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011064 * multiple cookies may be parsed on the same line. The returned sample is of
11065 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011066 */
11067static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010011068smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011069 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011070{
11071 struct http_txn *txn = l7;
11072 struct hdr_idx *idx = &txn->hdr_idx;
11073 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011074 const struct http_msg *msg;
11075 const char *hdr_name;
11076 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011077 int cnt;
11078 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011079 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011080
Willy Tarreau24e32d82012-04-23 23:55:44 +020011081 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011082 return 0;
11083
Willy Tarreaue333ec92012-04-16 16:26:40 +020011084 CHECK_HTTP_MESSAGE_FIRST();
11085
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011086 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011087 msg = &txn->req;
11088 hdr_name = "Cookie";
11089 hdr_name_len = 6;
11090 } else {
11091 msg = &txn->rsp;
11092 hdr_name = "Set-Cookie";
11093 hdr_name_len = 10;
11094 }
11095
Willy Tarreau9b28e032012-10-12 23:49:43 +020011096 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011097 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011098 ctx.idx = 0;
11099 cnt = 0;
11100
11101 while (1) {
11102 /* Note: val_beg == NULL every time we need to fetch a new header */
11103 if (!val_beg) {
11104 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11105 break;
11106
Willy Tarreau24e32d82012-04-23 23:55:44 +020011107 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108 continue;
11109
11110 val_beg = ctx.line + ctx.val;
11111 val_end = val_beg + ctx.vlen;
11112 }
11113
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011114 smp->type = SMP_T_STR;
11115 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011116 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011117 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011118 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011119 &smp->data.str.str,
11120 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011121 cnt++;
11122 }
11123 }
11124
Willy Tarreaub169eba2013-12-16 15:14:43 +010011125 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011126 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011127 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011128 return 1;
11129}
11130
Willy Tarreau51539362012-05-08 12:46:28 +020011131/* Fetch an cookie's integer value. The integer value is returned. It
11132 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11133 */
11134static int
11135smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011136 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011137{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011138 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011139
11140 if (ret > 0) {
11141 smp->type = SMP_T_UINT;
11142 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11143 }
11144
11145 return ret;
11146}
11147
Willy Tarreau8797c062007-05-07 00:55:35 +020011148/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011149/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011150/************************************************************************/
11151
David Cournapeau16023ee2010-12-23 20:55:41 +090011152/*
11153 * Given a path string and its length, find the position of beginning of the
11154 * query string. Returns NULL if no query string is found in the path.
11155 *
11156 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11157 *
11158 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11159 */
bedis4c75cca2012-10-05 08:38:24 +020011160static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011161{
11162 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011163
bedis4c75cca2012-10-05 08:38:24 +020011164 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011165 return p ? p + 1 : NULL;
11166}
11167
bedis4c75cca2012-10-05 08:38:24 +020011168static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011169{
bedis4c75cca2012-10-05 08:38:24 +020011170 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011171}
11172
11173/*
11174 * Given a url parameter, find the starting position of the first occurence,
11175 * or NULL if the parameter is not found.
11176 *
11177 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11178 * the function will return query_string+8.
11179 */
11180static char*
11181find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011182 char* url_param_name, size_t url_param_name_l,
11183 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011184{
11185 char *pos, *last;
11186
11187 pos = query_string;
11188 last = query_string + query_string_l - url_param_name_l - 1;
11189
11190 while (pos <= last) {
11191 if (pos[url_param_name_l] == '=') {
11192 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11193 return pos;
11194 pos += url_param_name_l + 1;
11195 }
bedis4c75cca2012-10-05 08:38:24 +020011196 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011197 pos++;
11198 pos++;
11199 }
11200 return NULL;
11201}
11202
11203/*
11204 * Given a url parameter name, returns its value and size into *value and
11205 * *value_l respectively, and returns non-zero. If the parameter is not found,
11206 * zero is returned and value/value_l are not touched.
11207 */
11208static int
11209find_url_param_value(char* path, size_t path_l,
11210 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011211 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011212{
11213 char *query_string, *qs_end;
11214 char *arg_start;
11215 char *value_start, *value_end;
11216
bedis4c75cca2012-10-05 08:38:24 +020011217 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011218 if (!query_string)
11219 return 0;
11220
11221 qs_end = path + path_l;
11222 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011223 url_param_name, url_param_name_l,
11224 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011225 if (!arg_start)
11226 return 0;
11227
11228 value_start = arg_start + url_param_name_l + 1;
11229 value_end = value_start;
11230
bedis4c75cca2012-10-05 08:38:24 +020011231 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011232 value_end++;
11233
11234 *value = value_start;
11235 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011236 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011237}
11238
11239static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011240smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011241 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011242{
bedis4c75cca2012-10-05 08:38:24 +020011243 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011244 struct http_txn *txn = l7;
11245 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011246
bedis4c75cca2012-10-05 08:38:24 +020011247 if (!args || args[0].type != ARGT_STR ||
11248 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011249 return 0;
11250
11251 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011252
bedis4c75cca2012-10-05 08:38:24 +020011253 if (args[1].type)
11254 delim = *args[1].data.str.str;
11255
Willy Tarreau9b28e032012-10-12 23:49:43 +020011256 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011257 args->data.str.str, args->data.str.len,
11258 &smp->data.str.str, &smp->data.str.len,
11259 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011260 return 0;
11261
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011262 smp->type = SMP_T_STR;
11263 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011264 return 1;
11265}
11266
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011267/* Return the signed integer value for the specified url parameter (see url_param
11268 * above).
11269 */
11270static int
11271smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011272 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011273{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011274 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011275
11276 if (ret > 0) {
11277 smp->type = SMP_T_UINT;
11278 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11279 }
11280
11281 return ret;
11282}
11283
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011284/* This produces a 32-bit hash of the concatenation of the first occurrence of
11285 * the Host header followed by the path component if it begins with a slash ('/').
11286 * This means that '*' will not be added, resulting in exactly the first Host
11287 * entry. If no Host header is found, then the path is used. The resulting value
11288 * is hashed using the url hash followed by a full avalanche hash and provides a
11289 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11290 * high-traffic sites without having to store whole paths.
11291 * this differs from the base32 functions in that it includes the url parameters
11292 * as well as the path
11293 */
11294static int
11295smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011296 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011297{
11298 struct http_txn *txn = l7;
11299 struct hdr_ctx ctx;
11300 unsigned int hash = 0;
11301 char *ptr, *beg, *end;
11302 int len;
11303
11304 CHECK_HTTP_MESSAGE_FIRST();
11305
11306 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011307 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011308 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11309 ptr = ctx.line + ctx.val;
11310 len = ctx.vlen;
11311 while (len--)
11312 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11313 }
11314
11315 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011316 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011317 beg = http_get_path(txn);
11318 if (!beg)
11319 beg = end;
11320
11321 for (ptr = beg; ptr < end ; ptr++);
11322
11323 if (beg < ptr && *beg == '/') {
11324 while (beg < ptr)
11325 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11326 }
11327 hash = full_hash(hash);
11328
11329 smp->type = SMP_T_UINT;
11330 smp->data.uint = hash;
11331 smp->flags = SMP_F_VOL_1ST;
11332 return 1;
11333}
11334
11335/* This concatenates the source address with the 32-bit hash of the Host and
11336 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11337 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11338 * on the source address length. The URL hash is stored before the address so
11339 * that in environments where IPv6 is insignificant, truncating the output to
11340 * 8 bytes would still work.
11341 */
11342static int
11343smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011344 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011345{
11346 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011347 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011348
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011349 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011350 return 0;
11351
11352 temp = get_trash_chunk();
11353 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11354 temp->len += sizeof(smp->data.uint);
11355
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011356 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011357 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011358 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011359 temp->len += 4;
11360 break;
11361 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011362 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011363 temp->len += 16;
11364 break;
11365 default:
11366 return 0;
11367 }
11368
11369 smp->data.str = *temp;
11370 smp->type = SMP_T_BIN;
11371 return 1;
11372}
11373
Willy Tarreau185b5c42012-04-26 15:11:51 +020011374/* This function is used to validate the arguments passed to any "hdr" fetch
11375 * keyword. These keywords support an optional positive or negative occurrence
11376 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11377 * is assumed that the types are already the correct ones. Returns 0 on error,
11378 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11379 * error message in case of error, that the caller is responsible for freeing.
11380 * The initial location must either be freeable or NULL.
11381 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011382int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011383{
11384 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011385 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011386 return 0;
11387 }
11388 return 1;
11389}
11390
Willy Tarreau276fae92013-07-25 14:36:01 +020011391/* takes an UINT value on input supposed to represent the time since EPOCH,
11392 * adds an optional offset found in args[0] and emits a string representing
11393 * the date in RFC-1123/5322 format.
11394 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011395static int sample_conv_http_date(struct session *session, const struct arg *args,
11396 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011397{
11398 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11399 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11400 struct chunk *temp;
11401 struct tm *tm;
11402 time_t curr_date = smp->data.uint;
11403
11404 /* add offset */
11405 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11406 curr_date += args[0].data.sint;
11407
11408 tm = gmtime(&curr_date);
11409
11410 temp = get_trash_chunk();
11411 temp->len = snprintf(temp->str, temp->size - temp->len,
11412 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11413 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11414 tm->tm_hour, tm->tm_min, tm->tm_sec);
11415
11416 smp->data.str = *temp;
11417 smp->type = SMP_T_STR;
11418 return 1;
11419}
11420
Thierry FOURNIERad903512014-04-11 17:51:01 +020011421/* Match language range with language tag. RFC2616 14.4:
11422 *
11423 * A language-range matches a language-tag if it exactly equals
11424 * the tag, or if it exactly equals a prefix of the tag such
11425 * that the first tag character following the prefix is "-".
11426 *
11427 * Return 1 if the strings match, else return 0.
11428 */
11429static inline int language_range_match(const char *range, int range_len,
11430 const char *tag, int tag_len)
11431{
11432 const char *end = range + range_len;
11433 const char *tend = tag + tag_len;
11434 while (range < end) {
11435 if (*range == '-' && tag == tend)
11436 return 1;
11437 if (*range != *tag || tag == tend)
11438 return 0;
11439 range++;
11440 tag++;
11441 }
11442 /* Return true only if the last char of the tag is matched. */
11443 return tag == tend;
11444}
11445
11446/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011447static int sample_conv_q_prefered(struct session *session, const struct arg *args,
11448 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011449{
11450 const char *al = smp->data.str.str;
11451 const char *end = al + smp->data.str.len;
11452 const char *token;
11453 int toklen;
11454 int qvalue;
11455 const char *str;
11456 const char *w;
11457 int best_q = 0;
11458
11459 /* Set the constant to the sample, because the output of the
11460 * function will be peek in the constant configuration string.
11461 */
11462 smp->flags |= SMP_F_CONST;
11463 smp->data.str.size = 0;
11464 smp->data.str.str = "";
11465 smp->data.str.len = 0;
11466
11467 /* Parse the accept language */
11468 while (1) {
11469
11470 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011471 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011472 al++;
11473 if (al >= end)
11474 break;
11475
11476 /* Start of the fisrt word. */
11477 token = al;
11478
11479 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011480 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011481 al++;
11482 if (al == token)
11483 goto expect_comma;
11484
11485 /* Length of the token. */
11486 toklen = al - token;
11487 qvalue = 1000;
11488
11489 /* Check if the token exists in the list. If the token not exists,
11490 * jump to the next token.
11491 */
11492 str = args[0].data.str.str;
11493 w = str;
11494 while (1) {
11495 if (*str == ';' || *str == '\0') {
11496 if (language_range_match(token, toklen, w, str-w))
11497 goto look_for_q;
11498 if (*str == '\0')
11499 goto expect_comma;
11500 w = str + 1;
11501 }
11502 str++;
11503 }
11504 goto expect_comma;
11505
11506look_for_q:
11507
11508 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011509 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011510 al++;
11511 if (al >= end)
11512 goto process_value;
11513
11514 /* If ',' is found, process the result */
11515 if (*al == ',')
11516 goto process_value;
11517
11518 /* If the character is different from ';', look
11519 * for the end of the header part in best effort.
11520 */
11521 if (*al != ';')
11522 goto expect_comma;
11523
11524 /* Assumes that the char is ';', now expect "q=". */
11525 al++;
11526
11527 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011528 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011529 al++;
11530 if (al >= end)
11531 goto process_value;
11532
11533 /* Expect 'q'. If no 'q', continue in best effort */
11534 if (*al != 'q')
11535 goto process_value;
11536 al++;
11537
11538 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011539 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011540 al++;
11541 if (al >= end)
11542 goto process_value;
11543
11544 /* Expect '='. If no '=', continue in best effort */
11545 if (*al != '=')
11546 goto process_value;
11547 al++;
11548
11549 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011550 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011551 al++;
11552 if (al >= end)
11553 goto process_value;
11554
11555 /* Parse the q value. */
11556 qvalue = parse_qvalue(al, &al);
11557
11558process_value:
11559
11560 /* If the new q value is the best q value, then store the associated
11561 * language in the response. If qvalue is the biggest value (1000),
11562 * break the process.
11563 */
11564 if (qvalue > best_q) {
11565 smp->data.str.str = (char *)w;
11566 smp->data.str.len = str - w;
11567 if (qvalue >= 1000)
11568 break;
11569 best_q = qvalue;
11570 }
11571
11572expect_comma:
11573
11574 /* Expect comma or end. If the end is detected, quit the loop. */
11575 while (al < end && *al != ',')
11576 al++;
11577 if (al >= end)
11578 break;
11579
11580 /* Comma is found, jump it and restart the analyzer. */
11581 al++;
11582 }
11583
11584 /* Set default value if required. */
11585 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11586 smp->data.str.str = args[1].data.str.str;
11587 smp->data.str.len = args[1].data.str.len;
11588 }
11589
11590 /* Return true only if a matching language was found. */
11591 return smp->data.str.len != 0;
11592}
11593
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011594/* This function executes one of the set-{method,path,query,uri} actions. It
11595 * builds a string in the trash from the specified format string, then modifies
11596 * the relevant part of the request line accordingly. Then it updates various
11597 * pointers to the next elements which were moved, and the total buffer length.
11598 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011599 * parse_set_req_line(). It always returns 1. If an error occurs the action
11600 * is canceled, but the rule processing continue.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011601 */
11602int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn)
11603{
11604 char *cur_ptr, *cur_end;
11605 int offset;
11606 int delta;
11607
11608 chunk_reset(&trash);
11609
11610 /* prepare a '?' just in case we have to create a query string */
11611 trash.str[trash.len++] = '?';
11612 offset = 1;
11613 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11614
11615 switch (*(int *)&rule->arg.act.p[2]) {
11616 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011617 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011618 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11619
11620 /* adjust req line offsets and lengths */
11621 delta = trash.len - offset - (cur_end - cur_ptr);
11622 txn->req.sl.rq.m_l += delta;
11623 txn->req.sl.rq.u += delta;
11624 txn->req.sl.rq.v += delta;
11625 break;
11626
11627 case 1: // path
11628 cur_ptr = http_get_path(txn);
11629 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011630 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011631
11632 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011633 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011634 cur_end++;
11635
11636 /* adjust req line offsets and lengths */
11637 delta = trash.len - offset - (cur_end - cur_ptr);
11638 txn->req.sl.rq.u_l += delta;
11639 txn->req.sl.rq.v += delta;
11640 break;
11641
11642 case 2: // query
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011643 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011644 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11645 while (cur_ptr < cur_end && *cur_ptr != '?')
11646 cur_ptr++;
11647
11648 /* skip the question mark or indicate that we must insert it
11649 * (but only if the format string is not empty then).
11650 */
11651 if (cur_ptr < cur_end)
11652 cur_ptr++;
11653 else if (trash.len > 1)
11654 offset = 0;
11655
11656 /* adjust req line offsets and lengths */
11657 delta = trash.len - offset - (cur_end - cur_ptr);
11658 txn->req.sl.rq.u_l += delta;
11659 txn->req.sl.rq.v += delta;
11660 break;
11661
11662 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011663 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011664 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11665
11666 /* adjust req line offsets and lengths */
11667 delta = trash.len - offset - (cur_end - cur_ptr);
11668 txn->req.sl.rq.u_l += delta;
11669 txn->req.sl.rq.v += delta;
11670 break;
11671
11672 default:
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011673 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011674 }
11675
11676 /* commit changes and adjust end of message */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011677 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011678 http_msg_move_end(&txn->req, delta);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011679 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011680}
11681
11682/* parse an http-request action among :
11683 * set-method
11684 * set-path
11685 * set-query
11686 * set-uri
11687 *
11688 * All of them accept a single argument of type string representing a log-format.
11689 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11690 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11691 * It returns 0 on success, < 0 on error.
11692 */
11693int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11694{
11695 int cur_arg = *orig_arg;
11696
11697 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11698
11699 switch (args[0][4]) {
11700 case 'm' :
11701 *(int *)&rule->arg.act.p[2] = 0;
11702 rule->action_ptr = http_action_set_req_line;
11703 break;
11704 case 'p' :
11705 *(int *)&rule->arg.act.p[2] = 1;
11706 rule->action_ptr = http_action_set_req_line;
11707 break;
11708 case 'q' :
11709 *(int *)&rule->arg.act.p[2] = 2;
11710 rule->action_ptr = http_action_set_req_line;
11711 break;
11712 case 'u' :
11713 *(int *)&rule->arg.act.p[2] = 3;
11714 rule->action_ptr = http_action_set_req_line;
11715 break;
11716 default:
11717 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11718 return -1;
11719 }
11720
11721 if (!*args[cur_arg] ||
11722 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11723 memprintf(err, "expects exactly 1 argument <format>");
11724 return -1;
11725 }
11726
11727 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11728 proxy->conf.args.ctx = ARGC_HRQ;
11729 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11730 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11731 proxy->conf.args.file, proxy->conf.args.line);
11732
11733 (*orig_arg)++;
11734 return 0;
11735}
11736
William Lallemand73025dd2014-04-24 14:38:37 +020011737/*
11738 * Return the struct http_req_action_kw associated to a keyword.
11739 */
11740struct http_req_action_kw *action_http_req_custom(const char *kw)
11741{
11742 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11743 struct http_req_action_kw_list *kw_list;
11744 int i;
11745
11746 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11747 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11748 if (!strcmp(kw, kw_list->kw[i].kw))
11749 return &kw_list->kw[i];
11750 }
11751 }
11752 }
11753 return NULL;
11754}
11755
11756/*
11757 * Return the struct http_res_action_kw associated to a keyword.
11758 */
11759struct http_res_action_kw *action_http_res_custom(const char *kw)
11760{
11761 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11762 struct http_res_action_kw_list *kw_list;
11763 int i;
11764
11765 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11766 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11767 if (!strcmp(kw, kw_list->kw[i].kw))
11768 return &kw_list->kw[i];
11769 }
11770 }
11771 }
11772 return NULL;
11773}
11774
Willy Tarreau4a568972010-05-12 08:08:50 +020011775/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011776/* All supported ACL keywords must be declared here. */
11777/************************************************************************/
11778
11779/* Note: must not be declared <const> as its list will be overwritten.
11780 * Please take care of keeping this list alphabetically sorted.
11781 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011782static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011783 { "base", "base", PAT_MATCH_STR },
11784 { "base_beg", "base", PAT_MATCH_BEG },
11785 { "base_dir", "base", PAT_MATCH_DIR },
11786 { "base_dom", "base", PAT_MATCH_DOM },
11787 { "base_end", "base", PAT_MATCH_END },
11788 { "base_len", "base", PAT_MATCH_LEN },
11789 { "base_reg", "base", PAT_MATCH_REG },
11790 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011791
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011792 { "cook", "req.cook", PAT_MATCH_STR },
11793 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11794 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11795 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11796 { "cook_end", "req.cook", PAT_MATCH_END },
11797 { "cook_len", "req.cook", PAT_MATCH_LEN },
11798 { "cook_reg", "req.cook", PAT_MATCH_REG },
11799 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011800
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011801 { "hdr", "req.hdr", PAT_MATCH_STR },
11802 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11803 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11804 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11805 { "hdr_end", "req.hdr", PAT_MATCH_END },
11806 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11807 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11808 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011809
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011810 /* these two declarations uses strings with list storage (in place
11811 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11812 * and delete functions are relative to the list management. The parse
11813 * and match method are related to the corresponding fetch methods. This
11814 * is very particular ACL declaration mode.
11815 */
11816 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11817 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011818
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011819 { "path", "path", PAT_MATCH_STR },
11820 { "path_beg", "path", PAT_MATCH_BEG },
11821 { "path_dir", "path", PAT_MATCH_DIR },
11822 { "path_dom", "path", PAT_MATCH_DOM },
11823 { "path_end", "path", PAT_MATCH_END },
11824 { "path_len", "path", PAT_MATCH_LEN },
11825 { "path_reg", "path", PAT_MATCH_REG },
11826 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011827
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011828 { "req_ver", "req.ver", PAT_MATCH_STR },
11829 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011830
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011831 { "scook", "res.cook", PAT_MATCH_STR },
11832 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11833 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11834 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11835 { "scook_end", "res.cook", PAT_MATCH_END },
11836 { "scook_len", "res.cook", PAT_MATCH_LEN },
11837 { "scook_reg", "res.cook", PAT_MATCH_REG },
11838 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011839
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011840 { "shdr", "res.hdr", PAT_MATCH_STR },
11841 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11842 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11843 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11844 { "shdr_end", "res.hdr", PAT_MATCH_END },
11845 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11846 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11847 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011848
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011849 { "url", "url", PAT_MATCH_STR },
11850 { "url_beg", "url", PAT_MATCH_BEG },
11851 { "url_dir", "url", PAT_MATCH_DIR },
11852 { "url_dom", "url", PAT_MATCH_DOM },
11853 { "url_end", "url", PAT_MATCH_END },
11854 { "url_len", "url", PAT_MATCH_LEN },
11855 { "url_reg", "url", PAT_MATCH_REG },
11856 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011857
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011858 { "urlp", "urlp", PAT_MATCH_STR },
11859 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11860 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11861 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11862 { "urlp_end", "urlp", PAT_MATCH_END },
11863 { "urlp_len", "urlp", PAT_MATCH_LEN },
11864 { "urlp_reg", "urlp", PAT_MATCH_REG },
11865 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011866
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011867 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011868}};
11869
11870/************************************************************************/
11871/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011872/************************************************************************/
11873/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011874static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011875 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011876 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11877 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11878
William Lallemanda43ba4e2014-01-28 18:14:25 +010011879 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011880 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011881
11882 /* retrieve these captures from the HTTP logs */
11883 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11884 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11885 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11886
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011887 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011888 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011889
Willy Tarreau409bcde2013-01-08 00:31:00 +010011890 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11891 * are only here to match the ACL's name, are request-only and are used
11892 * for ACL compatibility only.
11893 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011894 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11895 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011896 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11897 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11898
11899 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11900 * only here to match the ACL's name, are request-only and are used for
11901 * ACL compatibility only.
11902 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011903 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011904 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11905 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11906 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11907
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011908 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011909 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011910 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011911 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011912 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011913 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011914
11915 /* HTTP protocol on the request path */
11916 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011917 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011918
11919 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011920 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11921 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011922
11923 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011924 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11925 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011926
Willy Tarreau18ed2562013-01-14 15:56:36 +010011927 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011928 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011929 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11930 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11931
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011932 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011933 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011934 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011935 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11936 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010011937 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011938 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11939
11940 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011941 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011942 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11943 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11944
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011945 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011946 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011947 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011948 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11949 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010011950 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011951 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11952
Willy Tarreau409bcde2013-01-08 00:31:00 +010011953 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011954 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011955 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11956 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011957 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011958
11959 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011960 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011961 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11962 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11963 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11964
11965 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011966 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011967 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11968 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011969 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11970 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011971 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11972 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011973 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11974 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011975}};
11976
Willy Tarreau8797c062007-05-07 00:55:35 +020011977
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011978/************************************************************************/
11979/* All supported converter keywords must be declared here. */
11980/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020011981/* Note: must not be declared <const> as its list will be overwritten */
11982static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011983 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11984 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011985 { NULL, NULL, 0, 0, 0 },
11986}};
11987
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011988/************************************************************************/
11989/* All supported http-request action keywords must be declared here. */
11990/************************************************************************/
11991struct http_req_action_kw_list http_req_actions = {
11992 .scope = "http",
11993 .kw = {
11994 { "set-method", parse_set_req_line },
11995 { "set-path", parse_set_req_line },
11996 { "set-query", parse_set_req_line },
11997 { "set-uri", parse_set_req_line },
11998 }
11999};
12000
Willy Tarreau8797c062007-05-07 00:55:35 +020012001__attribute__((constructor))
12002static void __http_protocol_init(void)
12003{
12004 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012005 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012006 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012007 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012008}
12009
12010
Willy Tarreau58f10d72006-12-04 02:26:12 +010012011/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012012 * Local variables:
12013 * c-indent-level: 8
12014 * c-basic-offset: 8
12015 * End:
12016 */