blob: fae53fc92f87cfdfb282bf15f4ed2ce42d947bd9 [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 Tarreau8263d2b2012-08-28 00:06:31 +0200829 channel_auto_read(si->ob);
830 channel_abort(si->ob);
831 channel_auto_close(si->ob);
832 channel_erase(si->ob);
833 channel_auto_close(si->ib);
834 channel_auto_read(si->ib);
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 Tarreau9dab5fc2012-05-07 11:56:55 +0200837 bo_inject(si->ib, 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 Tarreau211cdec2014-04-17 20:18:08 +02001004 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 Tarreau9b28e032012-10-12 23:49:43 +02001007 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 Tarreau9b28e032012-10-12 23:49:43 +02001009 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 Tarreau9b28e032012-10-12 23:49:43 +02001463 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
William Lallemandd3002612012-11-26 14:34:47 +01002365 /* 200 only */
2366 if (txn->status != 200)
2367 goto fail;
2368
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 /* Content-Length is null */
2370 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2371 goto fail;
2372
2373 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002374 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2376 goto fail;
2377
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002378 /* no compression when Cache-Control: no-transform is present in the message */
2379 ctx.idx = 0;
2380 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2381 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2382 goto fail;
2383 }
2384
William Lallemand82fe75c2012-10-23 10:25:10 +02002385 comp_type = NULL;
2386
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002387 /* we don't want to compress multipart content-types, nor content-types that are
2388 * not listed in the "compression type" directive if any. If no content-type was
2389 * found but configuration requires one, we don't compress either. Backend has
2390 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002391 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002392 ctx.idx = 0;
2393 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2394 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2395 goto fail;
2396
2397 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2398 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002399 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002400 if (ctx.vlen >= comp_type->name_len &&
2401 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 /* this Content-Type should be compressed */
2403 break;
2404 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002405 /* this Content-Type should not be compressed */
2406 if (comp_type == NULL)
2407 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002409 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 else { /* no content-type header */
2411 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2412 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002413 }
2414
William Lallemandd85f9172012-11-09 17:05:39 +01002415 /* limit compression rate */
2416 if (global.comp_rate_lim > 0)
2417 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2418 goto fail;
2419
William Lallemand072a2bf2012-11-20 17:01:01 +01002420 /* limit cpu usage */
2421 if (idle_pct < compress_min_idle)
2422 goto fail;
2423
William Lallemand4c49fae2012-11-07 15:00:23 +01002424 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002425 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002426 goto fail;
2427
William Lallemandec3e3892012-11-12 17:02:18 +01002428 s->flags |= SN_COMP_READY;
2429
William Lallemand82fe75c2012-10-23 10:25:10 +02002430 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002431 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2434
2435 /* add Transfer-Encoding header */
2436 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2437 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2438
2439 /*
2440 * Add Content-Encoding header when it's not identity encoding.
2441 * RFC 2616 : Identity encoding: This content-coding is used only in the
2442 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2443 * header.
2444 */
2445 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002446 trash.len = 18;
2447 memcpy(trash.str, "Content-Encoding: ", trash.len);
2448 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2449 trash.len += s->comp_algo->name_len;
2450 trash.str[trash.len] = '\0';
2451 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002452 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 return 1;
2454
2455fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002456 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002457 return 0;
2458}
2459
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002460void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2461{
2462 int tmp = TX_CON_WANT_KAL;
2463
2464 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2465 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2467 tmp = TX_CON_WANT_TUN;
2468
2469 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2470 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2471 tmp = TX_CON_WANT_TUN;
2472 }
2473
2474 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2475 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2476 /* option httpclose + server_close => forceclose */
2477 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2479 tmp = TX_CON_WANT_CLO;
2480 else
2481 tmp = TX_CON_WANT_SCL;
2482 }
2483
2484 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2486 tmp = TX_CON_WANT_CLO;
2487
2488 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2490
2491 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2492 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2493 /* parse the Connection header and possibly clean it */
2494 int to_del = 0;
2495 if ((msg->flags & HTTP_MSGF_VER_11) ||
2496 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2497 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2498 to_del |= 2; /* remove "keep-alive" */
2499 if (!(msg->flags & HTTP_MSGF_VER_11))
2500 to_del |= 1; /* remove "close" */
2501 http_parse_connection_header(txn, msg, to_del);
2502 }
2503
2504 /* check if client or config asks for explicit close in KAL/SCL */
2505 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2506 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2507 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2508 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2509 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2510 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2511 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2512}
William Lallemand82fe75c2012-10-23 10:25:10 +02002513
Willy Tarreaud787e662009-07-07 10:14:51 +02002514/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2515 * processing can continue on next analysers, or zero if it either needs more
2516 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2517 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2518 * when it has nothing left to do, and may remove any analyser when it wants to
2519 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002520 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002521int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002522{
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 /*
2524 * We will parse the partial (or complete) lines.
2525 * We will check the request syntax, and also join multi-line
2526 * headers. An index of all the lines will be elaborated while
2527 * parsing.
2528 *
2529 * For the parsing, we use a 28 states FSM.
2530 *
2531 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002532 * req->buf->p = beginning of request
2533 * req->buf->p + msg->eoh = end of processed headers / start of current one
2534 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002535 * msg->eol = end of current header or line (LF or CRLF)
2536 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 *
2538 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002539 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2541 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002545 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 struct http_txn *txn = &s->txn;
2547 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002548 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002549
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002550 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 +01002551 now_ms, __FUNCTION__,
2552 s,
2553 req,
2554 req->rex, req->wex,
2555 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002557 req->analysers);
2558
Willy Tarreau52a0c602009-08-16 22:45:38 +02002559 /* we're speaking HTTP here, so let's speak HTTP to the client */
2560 s->srv_error = http_return_srv_error;
2561
Willy Tarreau83e3af02009-12-28 17:39:57 +01002562 /* There's a protected area at the end of the buffer for rewriting
2563 * purposes. We don't want to start to parse the request if the
2564 * protected area is affected, because we may have to move processed
2565 * data later, which is much more complicated.
2566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002567 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002568 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002569 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002570 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002571 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002572 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002573 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002574 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002575 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002576 return 0;
2577 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002578 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2579 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2580 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002581 }
2582
Willy Tarreau065e8332010-01-08 00:30:20 +01002583 /* Note that we have the same problem with the response ; we
2584 * may want to send a redirect, error or anything which requires
2585 * some spare space. So we'll ensure that we have at least
2586 * maxrewrite bytes available in the response buffer before
2587 * processing that one. This will only affect pipelined
2588 * keep-alive requests.
2589 */
2590 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreauba0902e2015-01-13 14:39:16 +01002591 unlikely(!channel_is_rewritable(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2593 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2594 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002597 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002600 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002601 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002602 return 0;
2603 }
2604 }
2605
Willy Tarreau9b28e032012-10-12 23:49:43 +02002606 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002607 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002608 }
2609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* 1: we might have to print this header in debug mode */
2611 if (unlikely((global.mode & MODE_DEBUG) &&
2612 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002613 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau9b28e032012-10-12 23:49:43 +02002616 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002617 /* this is a bit complex : in case of error on the request line,
2618 * we know that rq.l is still zero, so we display only the part
2619 * up to the end of the line (truncated by debug_hdr).
2620 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002621 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 sol += hdr_idx_first_pos(&txn->hdr_idx);
2625 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 while (cur_idx) {
2628 eol = sol + txn->hdr_idx.v[cur_idx].len;
2629 debug_hdr("clihdr", s, sol, eol);
2630 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2631 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 }
2634
Willy Tarreau58f10d72006-12-04 02:26:12 +01002635
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 /*
2637 * Now we quickly check if we have found a full valid request.
2638 * If not so, we check the FD and buffer states before leaving.
2639 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002640 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002641 * requests are checked first. When waiting for a second request
2642 * on a keep-alive session, if we encounter and error, close, t/o,
2643 * we note the error in the session flags but don't set any state.
2644 * Since the error will be noted there, it will not be counted by
2645 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002646 * Last, we may increase some tracked counters' http request errors on
2647 * the cases that are deliberately the client's fault. For instance,
2648 * a timeout or connection reset is not counted as an error. However
2649 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau655dce92009-11-08 13:10:58 +01002652 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002653 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002655 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002656 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_req_ctr(s);
2658 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 /* 1: Since we are in header mode, if there's no space
2664 * left for headers, we won't be able to free more
2665 * later, so the session will never terminate. We
2666 * must terminate it now.
2667 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /* FIXME: check if URI is set and return Status
2670 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002671 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002672 session_inc_http_req_ctr(s);
2673 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002674 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002675 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 goto return_bad_req;
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002682 if (!(s->flags & SN_ERR_MASK))
2683 s->flags |= SN_ERR_CLICL;
2684
Willy Tarreaufcffa692010-01-10 14:21:19 +01002685 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002686 goto failed_keep_alive;
2687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002690 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002691 session_inc_http_err_ctr(s);
2692 }
2693
Willy Tarreaudc979f22012-12-04 10:39:01 +01002694 txn->status = 400;
2695 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 msg->msg_state = HTTP_MSG_ERROR;
2697 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002700 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002701 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002702 if (s->listener->counters)
2703 s->listener->counters->failed_req++;
2704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 if (!(s->flags & SN_FINST_MASK))
2706 s->flags |= SN_FINST_R;
2707 return 0;
2708 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002709
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002711 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002712 if (!(s->flags & SN_ERR_MASK))
2713 s->flags |= SN_ERR_CLITO;
2714
Willy Tarreaufcffa692010-01-10 14:21:19 +01002715 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002716 goto failed_keep_alive;
2717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002719 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002720 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002721 session_inc_http_err_ctr(s);
2722 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002724 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 msg->msg_state = HTTP_MSG_ERROR;
2726 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002727
Willy Tarreauda7ff642010-06-23 11:44:09 +02002728 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002729 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002730 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731 if (s->listener->counters)
2732 s->listener->counters->failed_req++;
2733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 if (!(s->flags & SN_FINST_MASK))
2735 s->flags |= SN_FINST_R;
2736 return 0;
2737 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002740 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002741 if (!(s->flags & SN_ERR_MASK))
2742 s->flags |= SN_ERR_CLICL;
2743
Willy Tarreaufcffa692010-01-10 14:21:19 +01002744 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002745 goto failed_keep_alive;
2746
Willy Tarreau4076a152009-04-02 15:18:36 +02002747 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002748 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 msg->msg_state = HTTP_MSG_ERROR;
2752 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreauda7ff642010-06-23 11:44:09 +02002754 session_inc_http_err_ctr(s);
2755 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002756 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002757 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758 if (s->listener->counters)
2759 s->listener->counters->failed_req++;
2760
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 if (!(s->flags & SN_FINST_MASK))
2762 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002763 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 }
2765
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002766 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002767 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2768 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002769#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002770 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002771 /* We need more data, we have to re-enable quick-ack in case we
2772 * previously disabled it, otherwise we might cause the client
2773 * to delay next data.
2774 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002775 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002776 }
2777#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002778
Willy Tarreaufcffa692010-01-10 14:21:19 +01002779 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2780 /* If the client starts to talk, let's fall back to
2781 * request timeout processing.
2782 */
2783 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002784 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002785 }
2786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002788 if (!tick_isset(req->analyse_exp)) {
2789 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2790 (txn->flags & TX_WAIT_NEXT_RQ) &&
2791 tick_isset(s->be->timeout.httpka))
2792 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2793 else
2794 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002796
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 /* we're not ready yet */
2798 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002799
2800 failed_keep_alive:
2801 /* Here we process low-level errors for keep-alive requests. In
2802 * short, if the request is not the first one and it experiences
2803 * a timeout, read error or shutdown, we just silently close so
2804 * that the client can try again.
2805 */
2806 txn->status = 0;
2807 msg->msg_state = HTTP_MSG_RQBEFORE;
2808 req->analysers = 0;
2809 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002810 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002811 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002812 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002813 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002815
Willy Tarreaud787e662009-07-07 10:14:51 +02002816 /* OK now we have a complete HTTP request with indexed headers. Let's
2817 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002818 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002819 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002820 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002821 * byte after the last LF. msg->sov points to the first byte of data.
2822 * msg->eol cannot be trusted because it may have been left uninitialized
2823 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002825
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002827 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2828
Willy Tarreaub16a5742010-01-10 14:46:16 +01002829 if (txn->flags & TX_WAIT_NEXT_RQ) {
2830 /* kill the pending keep-alive timeout */
2831 txn->flags &= ~TX_WAIT_NEXT_RQ;
2832 req->analyse_exp = TICK_ETERNITY;
2833 }
2834
2835
Willy Tarreaud787e662009-07-07 10:14:51 +02002836 /* Maybe we found in invalid header name while we were configured not
2837 * to block on that, so we have to capture it now.
2838 */
2839 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002840 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002841
Willy Tarreau59234e92008-11-30 23:51:27 +01002842 /*
2843 * 1: identify the method
2844 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002845 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002846
2847 /* we can make use of server redirect on GET and HEAD */
2848 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2849 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /*
2852 * 2: check if the URI matches the monitor_uri.
2853 * We have to do this for every request which gets in, because
2854 * the monitor-uri is defined by the frontend.
2855 */
2856 if (unlikely((s->fe->monitor_uri_len != 0) &&
2857 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002858 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 s->fe->monitor_uri,
2860 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002863 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002865
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002867 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002871 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002872
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 ret = acl_pass(ret);
2874 if (cond->pol == ACL_COND_UNLESS)
2875 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 if (ret) {
2878 /* we fail this request, let's return 503 service unavail */
2879 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002880 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002881 if (!(s->flags & SN_ERR_MASK))
2882 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002884 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002885 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002886
Willy Tarreau59234e92008-11-30 23:51:27 +01002887 /* nothing to fail, let's reply normaly */
2888 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002889 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002890 if (!(s->flags & SN_ERR_MASK))
2891 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002892 goto return_prx_cond;
2893 }
2894
2895 /*
2896 * 3: Maybe we have to copy the original REQURI for the logs ?
2897 * Note: we cannot log anymore if the request has been
2898 * classified as invalid.
2899 */
2900 if (unlikely(s->logs.logwait & LW_REQ)) {
2901 /* we have a complete HTTP request that we must log */
2902 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2903 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002904
Willy Tarreau59234e92008-11-30 23:51:27 +01002905 if (urilen >= REQURI_LEN)
2906 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002907 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002909
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002910 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 s->do_log(s);
2912 } else {
2913 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002914 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 }
Willy Tarreau06619262006-12-17 08:37:22 +01002916
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002918 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002919 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002920
Willy Tarreau5b154472009-12-21 20:11:07 +01002921 /* ... and check if the request is HTTP/1.1 or above */
2922 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002923 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2924 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2925 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002926 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002927
2928 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002929 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 +01002930
Willy Tarreau88d349d2010-01-25 12:15:43 +01002931 /* if the frontend has "option http-use-proxy-header", we'll check if
2932 * we have what looks like a proxied connection instead of a connection,
2933 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2934 * Note that this is *not* RFC-compliant, however browsers and proxies
2935 * happen to do that despite being non-standard :-(
2936 * We consider that a request not beginning with either '/' or '*' is
2937 * a proxied connection, which covers both "scheme://location" and
2938 * CONNECT ip:port.
2939 */
2940 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002941 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002942 txn->flags |= TX_USE_PX_CONN;
2943
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002944 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002945 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002946
Willy Tarreau59234e92008-11-30 23:51:27 +01002947 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002948 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002951
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002952 /* 6: determine the transfer-length.
2953 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2954 * the presence of a message-body in a REQUEST and its transfer length
2955 * must be determined that way (in order of precedence) :
2956 * 1. The presence of a message-body in a request is signaled by the
2957 * inclusion of a Content-Length or Transfer-Encoding header field
2958 * in the request's header fields. When a request message contains
2959 * both a message-body of non-zero length and a method that does
2960 * not define any semantics for that request message-body, then an
2961 * origin server SHOULD either ignore the message-body or respond
2962 * with an appropriate error message (e.g., 413). A proxy or
2963 * gateway, when presented the same request, SHOULD either forward
2964 * the request inbound with the message- body or ignore the
2965 * message-body when determining a response.
2966 *
2967 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2968 * and the "chunked" transfer-coding (Section 6.2) is used, the
2969 * transfer-length is defined by the use of this transfer-coding.
2970 * If a Transfer-Encoding header field is present and the "chunked"
2971 * transfer-coding is not present, the transfer-length is defined
2972 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002974 * 3. If a Content-Length header field is present, its decimal value in
2975 * OCTETs represents both the entity-length and the transfer-length.
2976 * If a message is received with both a Transfer-Encoding header
2977 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002978 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002979 * 4. By the server closing the connection. (Closing the connection
2980 * cannot be used to indicate the end of a request body, since that
2981 * would leave no possibility for the server to send back a response.)
2982 *
2983 * Whenever a transfer-coding is applied to a message-body, the set of
2984 * transfer-codings MUST include "chunked", unless the message indicates
2985 * it is terminated by closing the connection. When the "chunked"
2986 * transfer-coding is used, it MUST be the last transfer-coding applied
2987 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002988 */
2989
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002990 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002991 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002992 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002993 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002994 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2997 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* bad transfer-encoding (chunked followed by something else) */
2999 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003000 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 break;
3002 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 }
3004
Willy Tarreau32b47f42009-10-18 20:55:02 +02003005 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003006 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003007 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 signed long long cl;
3009
Willy Tarreauad14f752011-09-02 20:33:27 +02003010 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreauad14f752011-09-02 20:33:27 +02003015 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003016 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003019
Willy Tarreauad14f752011-09-02 20:33:27 +02003020 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003021 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003024
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003025 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003028 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003030 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003031 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032 }
3033
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003034 /* bodyless requests have a known length */
3035 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003036 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003037
Willy Tarreau179085c2014-04-28 16:48:56 +02003038 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3039 * only change if both the request and the config reference something else.
3040 * Option httpclose by itself sets tunnel mode where headers are mangled.
3041 * However, if another mode is set, it will affect it (eg: server-close/
3042 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3043 * if FE and BE have the same settings (common). The method consists in
3044 * checking if options changed between the two calls (implying that either
3045 * one is non-null, or one of them is non-null and we are there for the first
3046 * time.
3047 */
3048 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003049 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3050 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003051
Willy Tarreaud787e662009-07-07 10:14:51 +02003052 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003053 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003054 req->analyse_exp = TICK_ETERNITY;
3055 return 1;
3056
3057 return_bad_req:
3058 /* We centralize bad requests processing here */
3059 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3060 /* we detected a parsing error. We want to archive this request
3061 * in the dedicated proxy area for later troubleshooting.
3062 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003063 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003064 }
3065
3066 txn->req.msg_state = HTTP_MSG_ERROR;
3067 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003068 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003069
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003070 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003071 if (s->listener->counters)
3072 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003073
3074 return_prx_cond:
3075 if (!(s->flags & SN_ERR_MASK))
3076 s->flags |= SN_ERR_PRXCOND;
3077 if (!(s->flags & SN_FINST_MASK))
3078 s->flags |= SN_FINST_R;
3079
3080 req->analysers = 0;
3081 req->analyse_exp = TICK_ETERNITY;
3082 return 0;
3083}
3084
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003085
Willy Tarreau347a35d2013-11-22 17:51:09 +01003086/* This function prepares an applet to handle the stats. It can deal with the
3087 * "100-continue" expectation, check that admin rules are met for POST requests,
3088 * and program a response message if something was unexpected. It cannot fail
3089 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003090 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003091 * s->target which is supposed to already point to the stats applet. The caller
3092 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 */
3094int http_handle_stats(struct session *s, struct channel *req)
3095{
3096 struct stats_admin_rule *stats_admin_rule;
3097 struct stream_interface *si = s->rep->prod;
3098 struct http_txn *txn = &s->txn;
3099 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003100 struct uri_auth *uri_auth = s->be->uri_auth;
3101 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003104 appctx = si_appctx(si);
3105 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3106 appctx->st1 = appctx->st2 = 0;
3107 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3108 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003109 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3110 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111
3112 uri = msg->chn->buf->p + msg->sl.rq.u;
3113 lookup = uri + uri_auth->uri_len;
3114
3115 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3116 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 break;
3119 }
3120 }
3121
3122 if (uri_auth->refresh) {
3123 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3124 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126 break;
3127 }
3128 }
3129 }
3130
3131 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3132 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003134 break;
3135 }
3136 }
3137
3138 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3139 if (memcmp(h, ";st=", 4) == 0) {
3140 int i;
3141 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003142 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003143 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3144 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 break;
3147 }
3148 }
3149 break;
3150 }
3151 }
3152
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003153 appctx->ctx.stats.scope_str = 0;
3154 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003155 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3156 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3157 int itx = 0;
3158 const char *h2;
3159 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3160 const char *err;
3161
3162 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3163 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003165 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3166 itx++;
3167 h++;
3168 }
3169
3170 if (itx > STAT_SCOPE_TXT_MAXLEN)
3171 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003173
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 memcpy(scope_txt, h2, itx);
3176 scope_txt[itx] = '\0';
3177 err = invalid_char(scope_txt);
3178 if (err) {
3179 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.scope_str = 0;
3181 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 }
3183 break;
3184 }
3185 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003186
3187 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189 int ret = 1;
3190
3191 if (stats_admin_rule->cond) {
3192 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3193 ret = acl_pass(ret);
3194 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3195 ret = !ret;
3196 }
3197
3198 if (ret) {
3199 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003201 break;
3202 }
3203 }
3204
3205 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003206 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003208 /* we'll need the request body, possibly after sending 100-continue */
3209 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003211 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003212 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3214 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003215 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003217 else {
3218 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003220 }
3221
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003222 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003223 return 1;
3224}
3225
Lukas Tribus67db8df2013-06-23 17:37:13 +02003226/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3227 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3228 */
3229static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3230{
3231#ifdef IP_TOS
3232 if (from.ss_family == AF_INET)
3233 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3234#endif
3235#ifdef IPV6_TCLASS
3236 if (from.ss_family == AF_INET6) {
3237 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3238 /* v4-mapped addresses need IP_TOS */
3239 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3240 else
3241 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3242 }
3243#endif
3244}
3245
Sasha Pachev218f0642014-06-16 12:05:59 -06003246/* Returns the number of characters written to destination,
3247 * -1 on internal error and -2 if no replacement took place.
3248 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003249static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003250 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003251{
Willy Tarreau15a53a42015-01-21 13:39:42 +01003252 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch, 0))
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 return -2;
3254
3255 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3256}
3257
3258/* Returns the number of characters written to destination,
3259 * -1 on internal error and -2 if no replacement took place.
3260 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003261static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003262 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003263{
3264 char* p = val;
3265 char* dst_end = dst + dst_size;
3266 char* dst_p = dst;
3267
3268 for (;;) {
3269 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003270
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003271 /* look for delim. */
3272 p_delim = p;
3273 while (p_delim < p + len && *p_delim != delim)
3274 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003275
Willy Tarreau15a53a42015-01-21 13:39:42 +01003276 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch, 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003277 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3278
3279 if (replace_n < 0)
3280 return -1;
3281
3282 dst_p += replace_n;
3283 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003284 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285
3286 if (dst_p + len >= dst_end)
3287 return -1;
3288
3289 memcpy(dst_p, p, len);
3290 dst_p += len;
3291 }
3292
3293 if (dst_p >= dst_end)
3294 return -1;
3295
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003296 /* end of the replacements. */
3297 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003298 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003299
3300 /* Next part. */
3301 *dst_p++ = delim;
3302 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003303 }
3304
3305 return dst_p - dst;
3306}
3307
3308static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003309 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003310 struct hdr_ctx* ctx, int action)
3311{
3312 ctx->idx = 0;
3313
3314 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3315 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3316 int delta;
Willy Tarreauaa435e72015-01-29 14:01:34 +01003317 char* val = (char*)ctx->line + ctx->val;
Sasha Pachev218f0642014-06-16 12:05:59 -06003318 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003319 char* reg_dst_buf;
3320 uint reg_dst_buf_size;
3321 int n_replaced;
3322
Sasha Pachev218f0642014-06-16 12:05:59 -06003323 trash.len = build_logline(s, trash.str, trash.size, fmt);
3324
3325 if (trash.len >= trash.size - 1)
3326 return -1;
3327
3328 reg_dst_buf = trash.str + trash.len + 1;
3329 reg_dst_buf_size = trash.size - trash.len - 1;
3330
3331 switch (action) {
3332 case HTTP_REQ_ACT_REPLACE_VAL:
3333 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003334 n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003335 break;
3336 case HTTP_REQ_ACT_REPLACE_HDR:
3337 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003338 n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003339 break;
3340 default: /* impossible */
3341 return -1;
3342 }
3343
Sasha Pachev218f0642014-06-16 12:05:59 -06003344 switch (n_replaced) {
3345 case -1: return -1;
3346 case -2: continue;
3347 }
3348
3349 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3350
3351 hdr->len += delta;
3352 http_msg_move_end(msg, delta);
3353 }
3354
3355 return 0;
3356}
3357
Willy Tarreau20b0de52012-12-24 15:45:22 +01003358/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 * transaction <txn>. Returns the verdict of the first rule that prevents
3360 * further processing of the request (auth, deny, ...), and defaults to
3361 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3362 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3363 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003364 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003365enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003366http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003367{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003368 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003369 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003370 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003371 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003372
Willy Tarreauff011f22011-01-06 17:51:27 +01003373 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003374 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003375 continue;
3376
Willy Tarreau96257ec2012-12-27 10:46:37 +01003377 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003378 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003379 int ret;
3380
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003381 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003382 ret = acl_pass(ret);
3383
Willy Tarreauff011f22011-01-06 17:51:27 +01003384 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003385 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003386
3387 if (!ret) /* condition not matched */
3388 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003389 }
3390
Willy Tarreau20b0de52012-12-24 15:45:22 +01003391
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 switch (rule->action) {
3393 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003394 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395
3396 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003397 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398
Willy Tarreauccbcc372012-12-27 12:37:57 +01003399 case HTTP_REQ_ACT_TARPIT:
3400 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003401 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003402
Willy Tarreau96257ec2012-12-27 10:46:37 +01003403 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003404 /* Auth might be performed on regular http-req rules as well as on stats */
3405 auth_realm = rule->arg.auth.realm;
3406 if (!auth_realm) {
3407 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3408 auth_realm = STATS_DEFAULT_REALM;
3409 else
3410 auth_realm = px->id;
3411 }
3412 /* send 401/407 depending on whether we use a proxy or not. We still
3413 * count one error, because normal browsing won't significantly
3414 * increase the counter but brute force attempts will.
3415 */
3416 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3417 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3418 stream_int_retnclose(&s->si[0], &trash);
3419 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003420 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003421
Willy Tarreau81499eb2012-12-27 12:19:02 +01003422 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003423 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3424 return HTTP_RULE_RES_BADREQ;
3425 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003426
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003427 case HTTP_REQ_ACT_SET_NICE:
3428 s->task->nice = rule->arg.nice;
3429 break;
3430
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003431 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003432 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003433 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003434 break;
3435
Willy Tarreau51347ed2013-06-11 19:34:13 +02003436 case HTTP_REQ_ACT_SET_MARK:
3437#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003438 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003439 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003440#endif
3441 break;
3442
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003443 case HTTP_REQ_ACT_SET_LOGL:
3444 s->logs.level = rule->arg.loglevel;
3445 break;
3446
Sasha Pachev218f0642014-06-16 12:05:59 -06003447 case HTTP_REQ_ACT_REPLACE_HDR:
3448 case HTTP_REQ_ACT_REPLACE_VAL:
3449 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3450 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003451 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003452 return HTTP_RULE_RES_BADREQ;
3453 break;
3454
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003455 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456 ctx.idx = 0;
3457 /* remove all occurrences of the header */
3458 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3459 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3460 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003461 }
Willy Tarreau85603282015-01-21 20:39:27 +01003462 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003463
Willy Tarreau85603282015-01-21 20:39:27 +01003464 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465 case HTTP_REQ_ACT_ADD_HDR:
3466 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3467 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3468 trash.len = rule->arg.hdr_add.name_len;
3469 trash.str[trash.len++] = ':';
3470 trash.str[trash.len++] = ' ';
3471 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 +01003472
3473 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3474 /* remove all occurrences of the header */
3475 ctx.idx = 0;
3476 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3477 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3478 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3479 }
3480 }
3481
Willy Tarreau96257ec2012-12-27 10:46:37 +01003482 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3483 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003484
3485 case HTTP_REQ_ACT_DEL_ACL:
3486 case HTTP_REQ_ACT_DEL_MAP: {
3487 struct pat_ref *ref;
3488 char *key;
3489 int len;
3490
3491 /* collect reference */
3492 ref = pat_ref_lookup(rule->arg.map.ref);
3493 if (!ref)
3494 continue;
3495
3496 /* collect key */
3497 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3498 key = trash.str;
3499 key[len] = '\0';
3500
3501 /* perform update */
3502 /* returned code: 1=ok, 0=ko */
3503 pat_ref_delete(ref, key);
3504
3505 break;
3506 }
3507
3508 case HTTP_REQ_ACT_ADD_ACL: {
3509 struct pat_ref *ref;
3510 char *key;
3511 struct chunk *trash_key;
3512 int len;
3513
3514 trash_key = get_trash_chunk();
3515
3516 /* collect reference */
3517 ref = pat_ref_lookup(rule->arg.map.ref);
3518 if (!ref)
3519 continue;
3520
3521 /* collect key */
3522 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3523 key = trash_key->str;
3524 key[len] = '\0';
3525
3526 /* perform update */
3527 /* add entry only if it does not already exist */
3528 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003529 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003530
3531 break;
3532 }
3533
3534 case HTTP_REQ_ACT_SET_MAP: {
3535 struct pat_ref *ref;
3536 char *key, *value;
3537 struct chunk *trash_key, *trash_value;
3538 int len;
3539
3540 trash_key = get_trash_chunk();
3541 trash_value = get_trash_chunk();
3542
3543 /* collect reference */
3544 ref = pat_ref_lookup(rule->arg.map.ref);
3545 if (!ref)
3546 continue;
3547
3548 /* collect key */
3549 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3550 key = trash_key->str;
3551 key[len] = '\0';
3552
3553 /* collect value */
3554 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3555 value = trash_value->str;
3556 value[len] = '\0';
3557
3558 /* perform update */
3559 if (pat_ref_find_elt(ref, key) != NULL)
3560 /* update entry if it exists */
3561 pat_ref_set(ref, key, value, NULL);
3562 else
3563 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003564 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003565
3566 break;
3567 }
William Lallemand73025dd2014-04-24 14:38:37 +02003568
3569 case HTTP_REQ_ACT_CUSTOM_CONT:
3570 rule->action_ptr(rule, px, s, txn);
3571 break;
3572
3573 case HTTP_REQ_ACT_CUSTOM_STOP:
3574 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003575 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003576
3577 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3578 /* Note: only the first valid tracking parameter of each
3579 * applies.
3580 */
3581
3582 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3583 struct stktable *t;
3584 struct stksess *ts;
3585 struct stktable_key *key;
3586 void *ptr;
3587
3588 t = rule->act_prm.trk_ctr.table.t;
3589 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3590
3591 if (key && (ts = stktable_get_entry(t, key))) {
3592 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3593
3594 /* let's count a new HTTP request as it's the first time we do it */
3595 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3596 if (ptr)
3597 stktable_data_cast(ptr, http_req_cnt)++;
3598
3599 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3600 if (ptr)
3601 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3602 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3603
3604 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3605 if (s->fe != s->be)
3606 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3607 }
3608 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003609 }
3610 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003611
3612 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003613 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003614}
3615
Willy Tarreau71241ab2012-12-27 11:30:54 +01003616
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003617/* Executes the http-response rules <rules> for session <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003618 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3619 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3620 * evaluation of next rule list. If *STOP is returned, the process must stop
3621 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3622 * rule. If *YIELD is returned, the czller must call again the function with
3623 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003624 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003625static enum rule_result
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003626http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3627{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003628 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003629 struct http_res_rule *rule;
3630 struct hdr_ctx ctx;
3631
3632 list_for_each_entry(rule, rules, list) {
3633 if (rule->action >= HTTP_RES_ACT_MAX)
3634 continue;
3635
3636 /* check optional condition */
3637 if (rule->cond) {
3638 int ret;
3639
3640 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3641 ret = acl_pass(ret);
3642
3643 if (rule->cond->pol == ACL_COND_UNLESS)
3644 ret = !ret;
3645
3646 if (!ret) /* condition not matched */
3647 continue;
3648 }
3649
3650
3651 switch (rule->action) {
3652 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003653 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003654
3655 case HTTP_RES_ACT_DENY:
3656 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003657 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003658
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003659 case HTTP_RES_ACT_SET_NICE:
3660 s->task->nice = rule->arg.nice;
3661 break;
3662
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003663 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003664 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003665 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003666 break;
3667
Willy Tarreau51347ed2013-06-11 19:34:13 +02003668 case HTTP_RES_ACT_SET_MARK:
3669#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003670 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003671 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003672#endif
3673 break;
3674
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003675 case HTTP_RES_ACT_SET_LOGL:
3676 s->logs.level = rule->arg.loglevel;
3677 break;
3678
Sasha Pachev218f0642014-06-16 12:05:59 -06003679 case HTTP_RES_ACT_REPLACE_HDR:
3680 case HTTP_RES_ACT_REPLACE_VAL:
3681 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3682 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003683 &rule->arg.hdr_add.re, &ctx, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003684 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003685 break;
3686
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003687 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003688 ctx.idx = 0;
3689 /* remove all occurrences of the header */
3690 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3691 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3692 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3693 }
Willy Tarreau85603282015-01-21 20:39:27 +01003694 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003695
Willy Tarreau85603282015-01-21 20:39:27 +01003696 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003697 case HTTP_RES_ACT_ADD_HDR:
3698 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3699 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3700 trash.len = rule->arg.hdr_add.name_len;
3701 trash.str[trash.len++] = ':';
3702 trash.str[trash.len++] = ' ';
3703 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 +01003704
3705 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3706 /* remove all occurrences of the header */
3707 ctx.idx = 0;
3708 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3709 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3710 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3711 }
3712 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003713 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3714 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003715
3716 case HTTP_RES_ACT_DEL_ACL:
3717 case HTTP_RES_ACT_DEL_MAP: {
3718 struct pat_ref *ref;
3719 char *key;
3720 int len;
3721
3722 /* collect reference */
3723 ref = pat_ref_lookup(rule->arg.map.ref);
3724 if (!ref)
3725 continue;
3726
3727 /* collect key */
3728 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3729 key = trash.str;
3730 key[len] = '\0';
3731
3732 /* perform update */
3733 /* returned code: 1=ok, 0=ko */
3734 pat_ref_delete(ref, key);
3735
3736 break;
3737 }
3738
3739 case HTTP_RES_ACT_ADD_ACL: {
3740 struct pat_ref *ref;
3741 char *key;
3742 struct chunk *trash_key;
3743 int len;
3744
3745 trash_key = get_trash_chunk();
3746
3747 /* collect reference */
3748 ref = pat_ref_lookup(rule->arg.map.ref);
3749 if (!ref)
3750 continue;
3751
3752 /* collect key */
3753 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3754 key = trash_key->str;
3755 key[len] = '\0';
3756
3757 /* perform update */
3758 /* check if the entry already exists */
3759 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003760 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003761
3762 break;
3763 }
3764
3765 case HTTP_RES_ACT_SET_MAP: {
3766 struct pat_ref *ref;
3767 char *key, *value;
3768 struct chunk *trash_key, *trash_value;
3769 int len;
3770
3771 trash_key = get_trash_chunk();
3772 trash_value = get_trash_chunk();
3773
3774 /* collect reference */
3775 ref = pat_ref_lookup(rule->arg.map.ref);
3776 if (!ref)
3777 continue;
3778
3779 /* collect key */
3780 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3781 key = trash_key->str;
3782 key[len] = '\0';
3783
3784 /* collect value */
3785 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3786 value = trash_value->str;
3787 value[len] = '\0';
3788
3789 /* perform update */
3790 if (pat_ref_find_elt(ref, key) != NULL)
3791 /* update entry if it exists */
3792 pat_ref_set(ref, key, value, NULL);
3793 else
3794 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003795 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003796
3797 break;
3798 }
William Lallemand73025dd2014-04-24 14:38:37 +02003799
3800 case HTTP_RES_ACT_CUSTOM_CONT:
3801 rule->action_ptr(rule, px, s, txn);
3802 break;
3803
3804 case HTTP_RES_ACT_CUSTOM_STOP:
3805 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003806 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003807 }
3808 }
3809
3810 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003811 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812}
3813
3814
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815/* Perform an HTTP redirect based on the information in <rule>. The function
3816 * returns non-zero on success, or zero in case of a, irrecoverable error such
3817 * as too large a request to build a valid response.
3818 */
3819static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3820{
3821 struct http_msg *msg = &txn->req;
3822 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003823 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003824
3825 /* build redirect message */
3826 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003827 case 308:
3828 msg_fmt = HTTP_308;
3829 break;
3830 case 307:
3831 msg_fmt = HTTP_307;
3832 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003833 case 303:
3834 msg_fmt = HTTP_303;
3835 break;
3836 case 301:
3837 msg_fmt = HTTP_301;
3838 break;
3839 case 302:
3840 default:
3841 msg_fmt = HTTP_302;
3842 break;
3843 }
3844
3845 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3846 return 0;
3847
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003848 location = trash.str + trash.len;
3849
Willy Tarreau71241ab2012-12-27 11:30:54 +01003850 switch(rule->type) {
3851 case REDIRECT_TYPE_SCHEME: {
3852 const char *path;
3853 const char *host;
3854 struct hdr_ctx ctx;
3855 int pathlen;
3856 int hostlen;
3857
3858 host = "";
3859 hostlen = 0;
3860 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003861 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003862 host = ctx.line + ctx.val;
3863 hostlen = ctx.vlen;
3864 }
3865
3866 path = http_get_path(txn);
3867 /* build message using path */
3868 if (path) {
3869 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3870 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3871 int qs = 0;
3872 while (qs < pathlen) {
3873 if (path[qs] == '?') {
3874 pathlen = qs;
3875 break;
3876 }
3877 qs++;
3878 }
3879 }
3880 } else {
3881 path = "/";
3882 pathlen = 1;
3883 }
3884
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003885 if (rule->rdr_str) { /* this is an old "redirect" rule */
3886 /* check if we can add scheme + "://" + host + path */
3887 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3888 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003889
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003890 /* add scheme */
3891 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3892 trash.len += rule->rdr_len;
3893 }
3894 else {
3895 /* add scheme with executing log format */
3896 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003897
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003898 /* check if we can add scheme + "://" + host + path */
3899 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3900 return 0;
3901 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003902 /* add "://" */
3903 memcpy(trash.str + trash.len, "://", 3);
3904 trash.len += 3;
3905
3906 /* add host */
3907 memcpy(trash.str + trash.len, host, hostlen);
3908 trash.len += hostlen;
3909
3910 /* add path */
3911 memcpy(trash.str + trash.len, path, pathlen);
3912 trash.len += pathlen;
3913
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003914 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003915 if (trash.len && trash.str[trash.len - 1] != '/' &&
3916 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3917 if (trash.len > trash.size - 5)
3918 return 0;
3919 trash.str[trash.len] = '/';
3920 trash.len++;
3921 }
3922
3923 break;
3924 }
3925 case REDIRECT_TYPE_PREFIX: {
3926 const char *path;
3927 int pathlen;
3928
3929 path = http_get_path(txn);
3930 /* build message using path */
3931 if (path) {
3932 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3933 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3934 int qs = 0;
3935 while (qs < pathlen) {
3936 if (path[qs] == '?') {
3937 pathlen = qs;
3938 break;
3939 }
3940 qs++;
3941 }
3942 }
3943 } else {
3944 path = "/";
3945 pathlen = 1;
3946 }
3947
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003948 if (rule->rdr_str) { /* this is an old "redirect" rule */
3949 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3950 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003951
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003952 /* add prefix. Note that if prefix == "/", we don't want to
3953 * add anything, otherwise it makes it hard for the user to
3954 * configure a self-redirection.
3955 */
3956 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3957 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3958 trash.len += rule->rdr_len;
3959 }
3960 }
3961 else {
3962 /* add prefix with executing log format */
3963 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3964
3965 /* Check length */
3966 if (trash.len + pathlen > trash.size - 4)
3967 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003968 }
3969
3970 /* add path */
3971 memcpy(trash.str + trash.len, path, pathlen);
3972 trash.len += pathlen;
3973
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003974 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003975 if (trash.len && trash.str[trash.len - 1] != '/' &&
3976 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3977 if (trash.len > trash.size - 5)
3978 return 0;
3979 trash.str[trash.len] = '/';
3980 trash.len++;
3981 }
3982
3983 break;
3984 }
3985 case REDIRECT_TYPE_LOCATION:
3986 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003987 if (rule->rdr_str) { /* this is an old "redirect" rule */
3988 if (trash.len + rule->rdr_len > trash.size - 4)
3989 return 0;
3990
3991 /* add location */
3992 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3993 trash.len += rule->rdr_len;
3994 }
3995 else {
3996 /* add location with executing log format */
3997 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003998
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003999 /* Check left length */
4000 if (trash.len > trash.size - 4)
4001 return 0;
4002 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004003 break;
4004 }
4005
4006 if (rule->cookie_len) {
4007 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4008 trash.len += 14;
4009 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4010 trash.len += rule->cookie_len;
4011 memcpy(trash.str + trash.len, "\r\n", 2);
4012 trash.len += 2;
4013 }
4014
4015 /* add end of headers and the keep-alive/close status.
4016 * We may choose to set keep-alive if the Location begins
4017 * with a slash, because the client will come back to the
4018 * same server.
4019 */
4020 txn->status = rule->code;
4021 /* let's log the request time */
4022 s->logs.tv_request = now;
4023
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004024 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4026 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4027 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4028 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4029 /* keep-alive possible */
4030 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4031 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4032 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4033 trash.len += 30;
4034 } else {
4035 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4036 trash.len += 24;
4037 }
4038 }
4039 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4040 trash.len += 4;
4041 bo_inject(txn->rsp.chn, trash.str, trash.len);
4042 /* "eat" the request */
4043 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004044 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004045 msg->sov = 0;
4046 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
4047 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
4048 txn->req.msg_state = HTTP_MSG_CLOSED;
4049 txn->rsp.msg_state = HTTP_MSG_DONE;
4050 } else {
4051 /* keep-alive not possible */
4052 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4053 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4054 trash.len += 29;
4055 } else {
4056 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4057 trash.len += 23;
4058 }
4059 stream_int_retnclose(txn->req.chn->prod, &trash);
4060 txn->req.chn->analysers = 0;
4061 }
4062
4063 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004064 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065 if (!(s->flags & SN_FINST_MASK))
4066 s->flags |= SN_FINST_R;
4067
4068 return 1;
4069}
4070
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004071/* This stream analyser runs all HTTP request processing which is common to
4072 * frontends and backends, which means blocking ACLs, filters, connection-close,
4073 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004074 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004075 * either needs more data or wants to immediately abort the request (eg: deny,
4076 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004077 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004078int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004079{
Willy Tarreaud787e662009-07-07 10:14:51 +02004080 struct http_txn *txn = &s->txn;
4081 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004082 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004083 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004084 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004085
Willy Tarreau655dce92009-11-08 13:10:58 +01004086 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004087 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004088 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004089 return 0;
4090 }
4091
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004092 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 +02004093 now_ms, __FUNCTION__,
4094 s,
4095 req,
4096 req->rex, req->wex,
4097 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004098 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004099 req->analysers);
4100
Willy Tarreau65410832014-04-28 21:25:43 +02004101 /* just in case we have some per-backend tracking */
4102 session_inc_be_http_req_ctr(s);
4103
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004104 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004105 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4106 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004107
Willy Tarreau0b748332014-04-29 00:13:29 +02004108 switch (verdict) {
4109 case HTTP_RULE_RES_CONT:
4110 case HTTP_RULE_RES_STOP: /* nothing to do */
4111 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004112
Willy Tarreau0b748332014-04-29 00:13:29 +02004113 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4114 if (txn->flags & TX_CLTARPIT)
4115 goto tarpit;
4116 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004117
Willy Tarreau0b748332014-04-29 00:13:29 +02004118 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4119 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004120
Willy Tarreau0b748332014-04-29 00:13:29 +02004121 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004122 goto done;
4123
Willy Tarreau0b748332014-04-29 00:13:29 +02004124 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4125 goto return_bad_req;
4126 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004127 }
4128
Willy Tarreau52542592014-04-28 18:33:26 +02004129 /* OK at this stage, we know that the request was accepted according to
4130 * the http-request rules, we can check for the stats. Note that the
4131 * URI is detected *before* the req* rules in order not to be affected
4132 * by a possible reqrep, while they are processed *after* so that a
4133 * reqdeny can still block them. This clearly needs to change in 1.6!
4134 */
4135 if (stats_check_uri(s->rep->prod, txn, px)) {
4136 s->target = &http_stats_applet.obj_type;
4137 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4138 txn->status = 500;
4139 s->logs.tv_request = now;
4140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004141
Willy Tarreau52542592014-04-28 18:33:26 +02004142 if (!(s->flags & SN_ERR_MASK))
4143 s->flags |= SN_ERR_RESOURCE;
4144 goto return_prx_cond;
4145 }
4146
4147 /* parse the whole stats request and extract the relevant information */
4148 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004149 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4150 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004151
Willy Tarreau0b748332014-04-29 00:13:29 +02004152 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4153 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004154
Willy Tarreau0b748332014-04-29 00:13:29 +02004155 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4156 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004157 }
4158
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004159 /* evaluate the req* rules except reqadd */
4160 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004161 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004162 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004163
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004164 if (txn->flags & TX_CLDENY)
4165 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004166
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004167 if (txn->flags & TX_CLTARPIT)
4168 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004169 }
Willy Tarreau06619262006-12-17 08:37:22 +01004170
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004171 /* add request headers from the rule sets in the same order */
4172 list_for_each_entry(wl, &px->req_add, list) {
4173 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004174 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004175 ret = acl_pass(ret);
4176 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4177 ret = !ret;
4178 if (!ret)
4179 continue;
4180 }
4181
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004182 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004183 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004184 }
4185
Willy Tarreau52542592014-04-28 18:33:26 +02004186
4187 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004188 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004189 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004190 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4191 s->fe->fe_counters.intercepted_req++;
4192
4193 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4194 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4195 if (!(s->flags & SN_FINST_MASK))
4196 s->flags |= SN_FINST_R;
4197
Willy Tarreau70730dd2014-04-24 18:06:27 +02004198 /* we may want to compress the stats page */
4199 if (s->fe->comp || s->be->comp)
4200 select_compression_request_header(s, req->buf);
4201
4202 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004203 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004204 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004205 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004207 /* check whether we have some ACLs set to redirect this request */
4208 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004209 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004210 int ret;
4211
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004212 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004213 ret = acl_pass(ret);
4214 if (rule->cond->pol == ACL_COND_UNLESS)
4215 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004216 if (!ret)
4217 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004218 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004219 if (!http_apply_redirect_rule(rule, s, txn))
4220 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004221 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004222 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004223
Willy Tarreau2be39392010-01-03 17:24:51 +01004224 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4225 * If this happens, then the data will not come immediately, so we must
4226 * send all what we have without waiting. Note that due to the small gain
4227 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004228 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004229 * itself once used.
4230 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004231 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004232
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004233 done: /* done with this analyser, continue with next ones that the calling
4234 * points will have set, if any.
4235 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004236 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004237 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4238 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004239 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004240
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004241 tarpit:
4242 /* When a connection is tarpitted, we use the tarpit timeout,
4243 * which may be the same as the connect timeout if unspecified.
4244 * If unset, then set it to zero because we really want it to
4245 * eventually expire. We build the tarpit as an analyser.
4246 */
4247 channel_erase(s->req);
4248
4249 /* wipe the request out so that we can drop the connection early
4250 * if the client closes first.
4251 */
4252 channel_dont_connect(req);
4253 req->analysers = 0; /* remove switching rules etc... */
4254 req->analysers |= AN_REQ_HTTP_TARPIT;
4255 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4256 if (!req->analyse_exp)
4257 req->analyse_exp = tick_add(now_ms, 0);
4258 session_inc_http_err_ctr(s);
4259 s->fe->fe_counters.denied_req++;
4260 if (s->fe != s->be)
4261 s->be->be_counters.denied_req++;
4262 if (s->listener->counters)
4263 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004264 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004265
4266 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004267 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004268 txn->status = 403;
4269 s->logs.tv_request = now;
4270 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4271 session_inc_http_err_ctr(s);
4272 s->fe->fe_counters.denied_req++;
4273 if (s->fe != s->be)
4274 s->be->be_counters.denied_req++;
4275 if (s->listener->counters)
4276 s->listener->counters->denied_req++;
4277 goto return_prx_cond;
4278
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004279 return_bad_req:
4280 /* We centralize bad requests processing here */
4281 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4282 /* we detected a parsing error. We want to archive this request
4283 * in the dedicated proxy area for later troubleshooting.
4284 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004285 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004286 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004288 txn->req.msg_state = HTTP_MSG_ERROR;
4289 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004290 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004291
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004292 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004293 if (s->listener->counters)
4294 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004296 return_prx_cond:
4297 if (!(s->flags & SN_ERR_MASK))
4298 s->flags |= SN_ERR_PRXCOND;
4299 if (!(s->flags & SN_FINST_MASK))
4300 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004301
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004302 req->analysers = 0;
4303 req->analyse_exp = TICK_ETERNITY;
4304 return 0;
4305}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307/* This function performs all the processing enabled for the current request.
4308 * It returns 1 if the processing can continue on next analysers, or zero if it
4309 * needs more data, encounters an error, or wants to immediately abort the
4310 * request. It relies on buffers flags, and updates s->req->analysers.
4311 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004312int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313{
4314 struct http_txn *txn = &s->txn;
4315 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004316 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004317
Willy Tarreau655dce92009-11-08 13:10:58 +01004318 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004319 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004320 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004321 return 0;
4322 }
4323
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004324 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 +02004325 now_ms, __FUNCTION__,
4326 s,
4327 req,
4328 req->rex, req->wex,
4329 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004330 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004331 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004332
William Lallemand82fe75c2012-10-23 10:25:10 +02004333 if (s->fe->comp || s->be->comp)
4334 select_compression_request_header(s, req->buf);
4335
Willy Tarreau59234e92008-11-30 23:51:27 +01004336 /*
4337 * Right now, we know that we have processed the entire headers
4338 * and that unwanted requests have been filtered out. We can do
4339 * whatever we want with the remaining request. Also, now we
4340 * may have separate values for ->fe, ->be.
4341 */
Willy Tarreau06619262006-12-17 08:37:22 +01004342
Willy Tarreau59234e92008-11-30 23:51:27 +01004343 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004344 * If HTTP PROXY is set we simply get remote server address parsing
4345 * incoming request. Note that this requires that a connection is
4346 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004347 */
4348 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004349 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004350 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004351
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004352 /* Note that for now we don't reuse existing proxy connections */
4353 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004354 txn->req.msg_state = HTTP_MSG_ERROR;
4355 txn->status = 500;
4356 req->analysers = 0;
4357 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4358
4359 if (!(s->flags & SN_ERR_MASK))
4360 s->flags |= SN_ERR_RESOURCE;
4361 if (!(s->flags & SN_FINST_MASK))
4362 s->flags |= SN_FINST_R;
4363
4364 return 0;
4365 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004366
4367 path = http_get_path(txn);
4368 url2sa(req->buf->p + msg->sl.rq.u,
4369 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004370 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004371 /* if the path was found, we have to remove everything between
4372 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4373 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4374 * u_l characters by a single "/".
4375 */
4376 if (path) {
4377 char *cur_ptr = req->buf->p;
4378 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4379 int delta;
4380
4381 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4382 http_msg_move_end(&txn->req, delta);
4383 cur_end += delta;
4384 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4385 goto return_bad_req;
4386 }
4387 else {
4388 char *cur_ptr = req->buf->p;
4389 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4390 int delta;
4391
4392 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4393 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4394 http_msg_move_end(&txn->req, delta);
4395 cur_end += delta;
4396 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4397 goto return_bad_req;
4398 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004400
Willy Tarreau59234e92008-11-30 23:51:27 +01004401 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004402 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 * Note that doing so might move headers in the request, but
4404 * the fields will stay coherent and the URI will not move.
4405 * This should only be performed in the backend.
4406 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004407 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004408 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4409 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004410
Willy Tarreau59234e92008-11-30 23:51:27 +01004411 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004412 * 8: the appsession cookie was looked up very early in 1.2,
4413 * so let's do the same now.
4414 */
4415
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004416 /* It needs to look into the URI unless persistence must be ignored */
4417 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004418 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 +01004419 }
4420
William Lallemanda73203e2012-03-12 12:48:57 +01004421 /* add unique-id if "header-unique-id" is specified */
4422
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004423 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4424 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4425 goto return_bad_req;
4426 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004427 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004428 }
William Lallemanda73203e2012-03-12 12:48:57 +01004429
4430 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004431 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4432 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004433 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004434 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004435 goto return_bad_req;
4436 }
4437
Cyril Bontéb21570a2009-11-29 20:04:48 +01004438 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004439 * 9: add X-Forwarded-For if either the frontend or the backend
4440 * asks for it.
4441 */
4442 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004443 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004444 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004445 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4446 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004447 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004448 /* The header is set to be added only if none is present
4449 * and we found it, so don't do anything.
4450 */
4451 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004452 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004453 /* Add an X-Forwarded-For header unless the source IP is
4454 * in the 'except' network range.
4455 */
4456 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004457 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004458 != s->fe->except_net.s_addr) &&
4459 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004460 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004461 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004462 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004463 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004464 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004465
4466 /* Note: we rely on the backend to get the header name to be used for
4467 * x-forwarded-for, because the header is really meant for the backends.
4468 * However, if the backend did not specify any option, we have to rely
4469 * on the frontend's header name.
4470 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004471 if (s->be->fwdfor_hdr_len) {
4472 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004473 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004474 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004475 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004476 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004477 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004478 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 +01004479
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004480 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004481 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004482 }
4483 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004484 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004485 /* FIXME: for the sake of completeness, we should also support
4486 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004487 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004488 int len;
4489 char pn[INET6_ADDRSTRLEN];
4490 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004491 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004492 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004493
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 /* Note: we rely on the backend to get the header name to be used for
4495 * x-forwarded-for, because the header is really meant for the backends.
4496 * However, if the backend did not specify any option, we have to rely
4497 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004498 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004499 if (s->be->fwdfor_hdr_len) {
4500 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004501 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004502 } else {
4503 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004504 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004505 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004506 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004507
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004508 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004509 goto return_bad_req;
4510 }
4511 }
4512
4513 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004514 * 10: add X-Original-To if either the frontend or the backend
4515 * asks for it.
4516 */
4517 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4518
4519 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004520 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004521 /* Add an X-Original-To header unless the destination IP is
4522 * in the 'except' network range.
4523 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004524 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004525
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004527 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004528 (((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 +02004529 != s->fe->except_to.s_addr) &&
4530 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004531 (((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 +02004532 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004533 int len;
4534 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004535 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004536
4537 /* Note: we rely on the backend to get the header name to be used for
4538 * x-original-to, because the header is really meant for the backends.
4539 * However, if the backend did not specify any option, we have to rely
4540 * on the frontend's header name.
4541 */
4542 if (s->be->orgto_hdr_len) {
4543 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004544 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004545 } else {
4546 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004547 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004548 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004549 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 +02004550
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004551 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004552 goto return_bad_req;
4553 }
4554 }
4555 }
4556
Willy Tarreau50fc7772012-11-11 22:19:57 +01004557 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4558 * If an "Upgrade" token is found, the header is left untouched in order not to have
4559 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4560 * "Upgrade" is present in the Connection header.
4561 */
4562 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4563 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004564 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4565 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004566 unsigned int want_flags = 0;
4567
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004568 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004569 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004570 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4571 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004572 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004573 want_flags |= TX_CON_CLO_SET;
4574 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004575 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004576 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4577 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004578 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004579 want_flags |= TX_CON_KAL_SET;
4580 }
4581
4582 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004583 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004584 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004585
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004586
Willy Tarreau522d6c02009-12-06 18:49:18 +01004587 /* If we have no server assigned yet and we're balancing on url_param
4588 * with a POST request, we may be interested in checking the body for
4589 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004590 */
4591 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4592 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004593 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004594 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004595 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004596 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004597
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004598 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004599 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004600#ifdef TCP_QUICKACK
4601 /* We expect some data from the client. Unless we know for sure
4602 * we already have a full request, we have to re-enable quick-ack
4603 * in case we previously disabled it, otherwise we might cause
4604 * the client to delay further data.
4605 */
4606 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004607 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004608 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004609 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004610 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004611#endif
4612 }
Willy Tarreau03945942009-12-22 16:50:27 +01004613
Willy Tarreau59234e92008-11-30 23:51:27 +01004614 /*************************************************************
4615 * OK, that's finished for the headers. We have done what we *
4616 * could. Let's switch to the DATA state. *
4617 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004618 req->analyse_exp = TICK_ETERNITY;
4619 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004620
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004621 /* if the server closes the connection, we want to immediately react
4622 * and close the socket to save packets and syscalls.
4623 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004624 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4625 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004626
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 /* OK let's go on with the BODY now */
4629 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004630
Willy Tarreau59234e92008-11-30 23:51:27 +01004631 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004632 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004633 /* we detected a parsing error. We want to archive this request
4634 * in the dedicated proxy area for later troubleshooting.
4635 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004636 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004637 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004638
Willy Tarreau59234e92008-11-30 23:51:27 +01004639 txn->req.msg_state = HTTP_MSG_ERROR;
4640 txn->status = 400;
4641 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004642 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004643
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004644 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004645 if (s->listener->counters)
4646 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004647
Willy Tarreau59234e92008-11-30 23:51:27 +01004648 if (!(s->flags & SN_ERR_MASK))
4649 s->flags |= SN_ERR_PRXCOND;
4650 if (!(s->flags & SN_FINST_MASK))
4651 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004652 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004653}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004654
Willy Tarreau60b85b02008-11-30 23:28:40 +01004655/* This function is an analyser which processes the HTTP tarpit. It always
4656 * returns zero, at the beginning because it prevents any other processing
4657 * from occurring, and at the end because it terminates the request.
4658 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004659int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004660{
4661 struct http_txn *txn = &s->txn;
4662
4663 /* This connection is being tarpitted. The CLIENT side has
4664 * already set the connect expiration date to the right
4665 * timeout. We just have to check that the client is still
4666 * there and that the timeout has not expired.
4667 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004668 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004669 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004670 !tick_is_expired(req->analyse_exp, now_ms))
4671 return 0;
4672
4673 /* We will set the queue timer to the time spent, just for
4674 * logging purposes. We fake a 500 server error, so that the
4675 * attacker will not suspect his connection has been tarpitted.
4676 * It will not cause trouble to the logs because we can exclude
4677 * the tarpitted connections by filtering on the 'PT' status flags.
4678 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004679 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4680
4681 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004682 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004683 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004684
4685 req->analysers = 0;
4686 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004687
Willy Tarreau60b85b02008-11-30 23:28:40 +01004688 if (!(s->flags & SN_ERR_MASK))
4689 s->flags |= SN_ERR_PRXCOND;
4690 if (!(s->flags & SN_FINST_MASK))
4691 s->flags |= SN_FINST_T;
4692 return 0;
4693}
4694
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004695/* This function is an analyser which waits for the HTTP request body. It waits
4696 * for either the buffer to be full, or the full advertised contents to have
4697 * reached the buffer. It must only be called after the standard HTTP request
4698 * processing has occurred, because it expects the request to be parsed and will
4699 * look for the Expect header. It may send a 100-Continue interim response. It
4700 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4701 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4702 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004703 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004704int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004705{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004706 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004707 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004708
4709 /* We have to parse the HTTP request body to find any required data.
4710 * "balance url_param check_post" should have been the only way to get
4711 * into this. We were brought here after HTTP header analysis, so all
4712 * related structures are ready.
4713 */
4714
Willy Tarreau890988f2014-04-10 11:59:33 +02004715 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4716 /* This is the first call */
4717 if (msg->msg_state < HTTP_MSG_BODY)
4718 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719
Willy Tarreau890988f2014-04-10 11:59:33 +02004720 if (msg->msg_state < HTTP_MSG_100_SENT) {
4721 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4722 * send an HTTP/1.1 100 Continue intermediate response.
4723 */
4724 if (msg->flags & HTTP_MSGF_VER_11) {
4725 struct hdr_ctx ctx;
4726 ctx.idx = 0;
4727 /* Expect is allowed in 1.1, look for it */
4728 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4729 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4730 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4731 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004732 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004733 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004734 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004735
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004736 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004737 * req->buf->p still points to the beginning of the message. We
4738 * must save the body in msg->next because it survives buffer
4739 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004741 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004742
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004743 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004744 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4745 else
4746 msg->msg_state = HTTP_MSG_DATA;
4747 }
4748
Willy Tarreau890988f2014-04-10 11:59:33 +02004749 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4750 /* We're in content-length mode, we just have to wait for enough data. */
4751 if (req->buf->i - msg->sov < msg->body_len)
4752 goto missing_data;
4753
4754 /* OK we have everything we need now */
4755 goto http_end;
4756 }
4757
4758 /* OK here we're parsing a chunked-encoded message */
4759
Willy Tarreau522d6c02009-12-06 18:49:18 +01004760 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004761 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004762 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004764 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004765 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004766
Willy Tarreau115acb92009-12-26 13:56:06 +01004767 if (!ret)
4768 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004769 else if (ret < 0) {
4770 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004772 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004773 }
4774
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004776 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004777 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004778 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004779 if (msg->msg_state == HTTP_MSG_TRAILERS)
4780 goto http_end;
4781
Willy Tarreau226071e2014-04-10 11:55:45 +02004782 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004783 goto http_end;
4784
4785 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004786 /* we get here if we need to wait for more data. If the buffer is full,
4787 * we have the maximum we can expect.
4788 */
4789 if (buffer_full(req->buf, global.tune.maxrewrite))
4790 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004791
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004792 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004793 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004794 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004795
4796 if (!(s->flags & SN_ERR_MASK))
4797 s->flags |= SN_ERR_CLITO;
4798 if (!(s->flags & SN_FINST_MASK))
4799 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004800 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004801 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802
4803 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004804 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004805 /* Not enough data. We'll re-use the http-request
4806 * timeout here. Ideally, we should set the timeout
4807 * relative to the accept() date. We just set the
4808 * request timeout once at the beginning of the
4809 * request.
4810 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004812 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004813 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004814 return 0;
4815 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004816
4817 http_end:
4818 /* The situation will not evolve, so let's give up on the analysis. */
4819 s->logs.tv_request = now; /* update the request timer to reflect full request */
4820 req->analysers &= ~an_bit;
4821 req->analyse_exp = TICK_ETERNITY;
4822 return 1;
4823
4824 return_bad_req: /* let's centralize all bad requests */
4825 txn->req.msg_state = HTTP_MSG_ERROR;
4826 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004827 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004828
Willy Tarreau79ebac62010-06-07 13:47:49 +02004829 if (!(s->flags & SN_ERR_MASK))
4830 s->flags |= SN_ERR_PRXCOND;
4831 if (!(s->flags & SN_FINST_MASK))
4832 s->flags |= SN_FINST_R;
4833
Willy Tarreau522d6c02009-12-06 18:49:18 +01004834 return_err_msg:
4835 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004836 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004837 if (s->listener->counters)
4838 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004840}
4841
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004842/* send a server's name with an outgoing request over an established connection.
4843 * Note: this function is designed to be called once the request has been scheduled
4844 * for being forwarded. This is the reason why it rewinds the buffer before
4845 * proceeding.
4846 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004847int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004848
4849 struct hdr_ctx ctx;
4850
Mark Lamourinec2247f02012-01-04 13:02:01 -05004851 char *hdr_name = be->server_id_hdr_name;
4852 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004853 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004854 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004855 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004856
William Lallemandd9e90662012-01-30 17:27:17 +01004857 ctx.idx = 0;
4858
Willy Tarreau211cdec2014-04-17 20:18:08 +02004859 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004860 if (old_o) {
4861 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004862 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004863 txn->req.next += old_o;
4864 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004865 }
4866
Willy Tarreau9b28e032012-10-12 23:49:43 +02004867 old_i = chn->buf->i;
4868 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 -05004869 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004870 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004871 }
4872
4873 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004874 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004875 memcpy(hdr_val, hdr_name, hdr_name_len);
4876 hdr_val += hdr_name_len;
4877 *hdr_val++ = ':';
4878 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004879 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4880 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004881
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004882 if (old_o) {
4883 /* If this was a forwarded request, we must readjust the amount of
4884 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004885 * variations. Note that the current state is >= HTTP_MSG_BODY,
4886 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004887 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004888 old_o += chn->buf->i - old_i;
4889 b_adv(chn->buf, old_o);
4890 txn->req.next -= old_o;
4891 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004892 }
4893
Mark Lamourinec2247f02012-01-04 13:02:01 -05004894 return 0;
4895}
4896
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897/* Terminate current transaction and prepare a new one. This is very tricky
4898 * right now but it works.
4899 */
4900void http_end_txn_clean_session(struct session *s)
4901{
Willy Tarreau068621e2013-12-23 15:11:25 +01004902 int prev_status = s->txn.status;
4903
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904 /* FIXME: We need a more portable way of releasing a backend's and a
4905 * server's connections. We need a safer way to reinitialize buffer
4906 * flags. We also need a more accurate method for computing per-request
4907 * data.
4908 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909
Willy Tarreau4213a112013-12-15 10:25:42 +01004910 /* unless we're doing keep-alive, we want to quickly close the connection
4911 * to the server.
4912 */
4913 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4914 !si_conn_ready(s->req->cons)) {
4915 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4916 si_shutr(s->req->cons);
4917 si_shutw(s->req->cons);
4918 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004920 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004922 if (unlikely(s->srv_conn))
4923 sess_change_server(s, NULL);
4924 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925
4926 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4927 session_process_counters(s);
4928
4929 if (s->txn.status) {
4930 int n;
4931
4932 n = s->txn.status / 100;
4933 if (n < 1 || n > 5)
4934 n = 0;
4935
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004936 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004937 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004938 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004939 s->fe->fe_counters.p.http.comp_rsp++;
4940 }
Willy Tarreau24657792010-02-26 10:30:28 +01004941 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004942 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004943 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004944 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004945 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004946 s->be->be_counters.p.http.comp_rsp++;
4947 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004948 }
4949
4950 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004951 s->logs.bytes_in -= s->req->buf->i;
4952 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953
4954 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004955 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956 !(s->flags & SN_MONITOR) &&
4957 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4958 s->do_log(s);
4959 }
4960
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004961 /* stop tracking content-based counters */
4962 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004963 session_update_time_stats(s);
4964
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 s->logs.accept_date = date; /* user-visible date for logging */
4966 s->logs.tv_accept = now; /* corrected date for internal use */
4967 tv_zero(&s->logs.tv_request);
4968 s->logs.t_queue = -1;
4969 s->logs.t_connect = -1;
4970 s->logs.t_data = -1;
4971 s->logs.t_close = 0;
4972 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4973 s->logs.srv_queue_size = 0; /* we will get this number soon */
4974
Willy Tarreau9b28e032012-10-12 23:49:43 +02004975 s->logs.bytes_in = s->req->total = s->req->buf->i;
4976 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004977
4978 if (s->pend_pos)
4979 pendconn_free(s->pend_pos);
4980
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004981 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982 if (s->flags & SN_CURR_SESS) {
4983 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004984 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004986 if (may_dequeue_tasks(objt_server(s->target), s->be))
4987 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988 }
4989
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004990 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991
Willy Tarreau4213a112013-12-15 10:25:42 +01004992 /* only release our endpoint if we don't intend to reuse the
4993 * connection.
4994 */
4995 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4996 !si_conn_ready(s->req->cons)) {
4997 si_release_endpoint(s->req->cons);
4998 }
4999
Willy Tarreau610ecce2010-01-04 21:15:02 +01005000 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02005002 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005003 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01005004 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005005 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);
5006 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005007 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 +01005008 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02005009 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005010
Willy Tarreau610ecce2010-01-04 21:15:02 +01005011 s->txn.meth = 0;
5012 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005013 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005014
5015 if (prev_status == 401 || prev_status == 407) {
5016 /* In HTTP keep-alive mode, if we receive a 401, we still have
5017 * a chance of being able to send the visitor again to the same
5018 * server over the same connection. This is required by some
5019 * broken protocols such as NTLM, and anyway whenever there is
5020 * an opportunity for sending the challenge to the proper place,
5021 * it's better to do it (at least it helps with debugging).
5022 */
5023 s->txn.flags |= TX_PREFER_LAST;
5024 }
5025
Willy Tarreauee55dc02010-06-01 10:56:34 +02005026 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 s->req->cons->flags |= SI_FL_INDEP_STR;
5028
Willy Tarreau96e31212011-05-30 18:10:30 +02005029 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005030 s->req->flags |= CF_NEVER_WAIT;
5031 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005032 }
5033
Willy Tarreau610ecce2010-01-04 21:15:02 +01005034 /* if the request buffer is not empty, it means we're
5035 * about to process another request, so send pending
5036 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005037 * Just don't do this if the buffer is close to be full,
5038 * because the request will wait for it to flush a little
5039 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005041 if (s->req->buf->i) {
5042 if (s->rep->buf->o &&
5043 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
5044 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005045 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005046 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005047
5048 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005049 channel_auto_read(s->req);
5050 channel_auto_close(s->req);
5051 channel_auto_read(s->rep);
5052 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053
Willy Tarreau27375622013-12-17 00:00:28 +01005054 /* we're in keep-alive with an idle connection, monitor it */
5055 si_idle_conn(s->req->cons);
5056
Willy Tarreau342b11c2010-11-24 16:22:09 +01005057 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059}
5060
5061
5062/* This function updates the request state machine according to the response
5063 * state machine and buffer flags. It returns 1 if it changes anything (flag
5064 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5065 * it is only used to find when a request/response couple is complete. Both
5066 * this function and its equivalent should loop until both return zero. It
5067 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5068 */
5069int http_sync_req_state(struct session *s)
5070{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005071 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005073 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 unsigned int old_state = txn->req.msg_state;
5075
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5077 return 0;
5078
5079 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005080 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005081 * We can shut the read side unless we want to abort_on_close,
5082 * or we have a POST request. The issue with POST requests is
5083 * that some browsers still send a CRLF after the request, and
5084 * this CRLF must be read so that it does not remain in the kernel
5085 * buffers, otherwise a close could cause an RST on some systems
5086 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005087 * Note that if we're using keep-alive on the client side, we'd
5088 * rather poll now and keep the polling enabled for the whole
5089 * session's life than enabling/disabling it between each
5090 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005091 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005092 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5093 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5094 !(s->be->options & PR_O_ABRT_CLOSE) &&
5095 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005096 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097
Willy Tarreau40f151a2012-12-20 12:10:09 +01005098 /* if the server closes the connection, we want to immediately react
5099 * and close the socket to save packets and syscalls.
5100 */
5101 chn->cons->flags |= SI_FL_NOHALF;
5102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5104 goto wait_other_side;
5105
5106 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5107 /* The server has not finished to respond, so we
5108 * don't want to move in order not to upset it.
5109 */
5110 goto wait_other_side;
5111 }
5112
5113 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5114 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005115 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005117 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 goto wait_other_side;
5119 }
5120
5121 /* When we get here, it means that both the request and the
5122 * response have finished receiving. Depending on the connection
5123 * mode, we'll have to wait for the last bytes to leave in either
5124 * direction, and sometimes for a close to be effective.
5125 */
5126
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005127 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5128 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005129 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5130 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005131 }
5132 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5133 /* Option forceclose is set, or either side wants to close,
5134 * let's enforce it now that we're not expecting any new
5135 * data to come. The caller knows the session is complete
5136 * once both states are CLOSED.
5137 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005138 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5139 channel_shutr_now(chn);
5140 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005142 }
5143 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005144 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5145 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005146 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005147 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5148 channel_auto_read(chn);
5149 txn->req.msg_state = HTTP_MSG_TUNNEL;
5150 chn->flags |= CF_NEVER_WAIT;
5151 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152 }
5153
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005154 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005156 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005157
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005158 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 txn->req.msg_state = HTTP_MSG_CLOSING;
5160 goto http_msg_closing;
5161 }
5162 else {
5163 txn->req.msg_state = HTTP_MSG_CLOSED;
5164 goto http_msg_closed;
5165 }
5166 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005167 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 }
5169
5170 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5171 http_msg_closing:
5172 /* nothing else to forward, just waiting for the output buffer
5173 * to be empty and for the shutw_now to take effect.
5174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 txn->req.msg_state = HTTP_MSG_CLOSED;
5177 goto http_msg_closed;
5178 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005179 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 txn->req.msg_state = HTTP_MSG_ERROR;
5181 goto wait_other_side;
5182 }
5183 }
5184
5185 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5186 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005187 /* see above in MSG_DONE why we only do this in these states */
5188 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5189 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5190 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005191 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 goto wait_other_side;
5193 }
5194
5195 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005196 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197}
5198
5199
5200/* This function updates the response state machine according to the request
5201 * state machine and buffer flags. It returns 1 if it changes anything (flag
5202 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5203 * it is only used to find when a request/response couple is complete. Both
5204 * this function and its equivalent should loop until both return zero. It
5205 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5206 */
5207int http_sync_res_state(struct session *s)
5208{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005209 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005211 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 unsigned int old_state = txn->rsp.msg_state;
5213
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5215 return 0;
5216
5217 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5218 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005219 * still monitor the server connection for a possible close
5220 * while the request is being uploaded, so we don't disable
5221 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005223 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224
5225 if (txn->req.msg_state == HTTP_MSG_ERROR)
5226 goto wait_other_side;
5227
5228 if (txn->req.msg_state < HTTP_MSG_DONE) {
5229 /* The client seems to still be sending data, probably
5230 * because we got an error response during an upload.
5231 * We have the choice of either breaking the connection
5232 * or letting it pass through. Let's do the later.
5233 */
5234 goto wait_other_side;
5235 }
5236
5237 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5238 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005239 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005241 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 goto wait_other_side;
5243 }
5244
5245 /* When we get here, it means that both the request and the
5246 * response have finished receiving. Depending on the connection
5247 * mode, we'll have to wait for the last bytes to leave in either
5248 * direction, and sometimes for a close to be effective.
5249 */
5250
5251 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5252 /* Server-close mode : shut read and wait for the request
5253 * side to close its output buffer. The caller will detect
5254 * when we're in DONE and the other is in CLOSED and will
5255 * catch that for the final cleanup.
5256 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005257 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5258 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005260 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5261 /* Option forceclose is set, or either side wants to close,
5262 * let's enforce it now that we're not expecting any new
5263 * data to come. The caller knows the session is complete
5264 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005265 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005266 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5267 channel_shutr_now(chn);
5268 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005269 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 }
5271 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005272 /* The last possible modes are keep-alive and tunnel. Tunnel will
5273 * need to forward remaining data. Keep-alive will need to monitor
5274 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005275 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005277 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005278 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5279 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280 }
5281
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005282 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005284 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5286 goto http_msg_closing;
5287 }
5288 else {
5289 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5290 goto http_msg_closed;
5291 }
5292 }
5293 goto wait_other_side;
5294 }
5295
5296 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5297 http_msg_closing:
5298 /* nothing else to forward, just waiting for the output buffer
5299 * to be empty and for the shutw_now to take effect.
5300 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005301 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5303 goto http_msg_closed;
5304 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005305 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005306 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005307 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005308 if (objt_server(s->target))
5309 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 goto wait_other_side;
5311 }
5312 }
5313
5314 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5315 http_msg_closed:
5316 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005317 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 channel_auto_close(chn);
5319 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 goto wait_other_side;
5321 }
5322
5323 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005324 /* We force the response to leave immediately if we're waiting for the
5325 * other side, since there is no pending shutdown to push it out.
5326 */
5327 if (!channel_is_empty(chn))
5328 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005329 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330}
5331
5332
5333/* Resync the request and response state machines. Return 1 if either state
5334 * changes.
5335 */
5336int http_resync_states(struct session *s)
5337{
5338 struct http_txn *txn = &s->txn;
5339 int old_req_state = txn->req.msg_state;
5340 int old_res_state = txn->rsp.msg_state;
5341
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 http_sync_req_state(s);
5343 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 if (!http_sync_res_state(s))
5345 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 if (!http_sync_req_state(s))
5347 break;
5348 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005349
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 /* OK, both state machines agree on a compatible state.
5351 * There are a few cases we're interested in :
5352 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5353 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5354 * directions, so let's simply disable both analysers.
5355 * - HTTP_MSG_CLOSED on the response only means we must abort the
5356 * request.
5357 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5358 * with server-close mode means we've completed one request and we
5359 * must re-initialize the server connection.
5360 */
5361
5362 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5363 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5364 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5365 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5366 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005367 channel_auto_close(s->req);
5368 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005370 channel_auto_close(s->rep);
5371 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005373 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5374 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005375 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005376 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005377 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005378 channel_auto_close(s->rep);
5379 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005380 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005381 channel_abort(s->req);
5382 channel_auto_close(s->req);
5383 channel_auto_read(s->req);
Willy Tarreau319f7452015-01-14 20:32:59 +01005384 channel_truncate(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005386 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5387 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005389 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5390 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5391 /* server-close/keep-alive: terminate this transaction,
5392 * possibly killing the server connection and reinitialize
5393 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005394 */
5395 http_end_txn_clean_session(s);
5396 }
5397
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 return txn->req.msg_state != old_req_state ||
5399 txn->rsp.msg_state != old_res_state;
5400}
5401
Willy Tarreaud98cf932009-12-27 22:54:55 +01005402/* This function is an analyser which forwards request body (including chunk
5403 * sizes if any). It is called as soon as we must forward, even if we forward
5404 * zero byte. The only situation where it must not be called is when we're in
5405 * tunnel mode and we want to forward till the close. It's used both to forward
5406 * remaining data and to resync after end of body. It expects the msg_state to
5407 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5408 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005409 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005410 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005412int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413{
5414 struct http_txn *txn = &s->txn;
5415 struct http_msg *msg = &s->txn.req;
5416
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005417 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5418 return 0;
5419
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005420 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005421 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005422 /* Output closed while we were sending data. We must abort and
5423 * wake the other side up.
5424 */
5425 msg->msg_state = HTTP_MSG_ERROR;
5426 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005427 return 1;
5428 }
5429
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430 /* Note that we don't have to send 100-continue back because we don't
5431 * need the data to complete our job, and it's up to the server to
5432 * decide whether to return 100, 417 or anything else in return of
5433 * an "Expect: 100-continue" header.
5434 */
5435
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005436 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005437 /* we have msg->sov which points to the first byte of message
5438 * body, and req->buf.p still points to the beginning of the
5439 * message. We forward the headers now, as we don't need them
5440 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005442 b_adv(req->buf, msg->sov);
5443 msg->next -= msg->sov;
5444 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005445
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005446 /* The previous analysers guarantee that the state is somewhere
5447 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5448 * msg->next are always correct.
5449 */
5450 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5451 if (msg->flags & HTTP_MSGF_TE_CHNK)
5452 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5453 else
5454 msg->msg_state = HTTP_MSG_DATA;
5455 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 }
5457
Willy Tarreau7ba23542014-04-17 21:50:00 +02005458 /* Some post-connect processing might want us to refrain from starting to
5459 * forward data. Currently, the only reason for this is "balance url_param"
5460 * whichs need to parse/process the request after we've enabled forwarding.
5461 */
5462 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5463 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5464 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005465 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005466 goto missing_data;
5467 }
5468 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5469 }
5470
Willy Tarreau80a92c02014-03-12 10:41:13 +01005471 /* in most states, we should abort in case of early close */
5472 channel_auto_close(req);
5473
Willy Tarreauefdf0942014-04-24 20:08:57 +02005474 if (req->to_forward) {
5475 /* We can't process the buffer's contents yet */
5476 req->flags |= CF_WAKE_WRITE;
5477 goto missing_data;
5478 }
5479
Willy Tarreaud98cf932009-12-27 22:54:55 +01005480 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005481 if (msg->msg_state == HTTP_MSG_DATA) {
5482 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005483 /* we may have some pending data starting at req->buf->p */
5484 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005485 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005486 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005487 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005488 msg->next += msg->chunk_len;
5489 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005490
5491 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005492 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005493 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005494 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005495 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005496 }
5497 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005498 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005499 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 * TRAILERS state.
5501 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005502 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503
Willy Tarreau54d23df2012-10-25 19:04:45 +02005504 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005506 else if (ret < 0) {
5507 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005508 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005509 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005511 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005514 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005516 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517
5518 if (ret == 0)
5519 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005520 else if (ret < 0) {
5521 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005522 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005523 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005525 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 /* we're in MSG_CHUNK_SIZE now */
5527 }
5528 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005529 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530
5531 if (ret == 0)
5532 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005533 else if (ret < 0) {
5534 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005535 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005536 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005538 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539 /* we're in HTTP_MSG_DONE now */
5540 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005542 int old_state = msg->msg_state;
5543
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005545
5546 /* we may have some pending data starting at req->buf->p
5547 * such as last chunk of data or trailers.
5548 */
5549 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005550 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005551 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005552 msg->next = 0;
5553
Willy Tarreau4fe41902010-06-07 22:27:41 +02005554 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005555 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5556 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005557 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 if (http_resync_states(s)) {
5559 /* some state changes occurred, maybe the analyser
5560 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005561 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005562 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005563 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005564 /* request errors are most likely due to
5565 * the server aborting the transfer.
5566 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005568 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005569 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005570 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005572 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005574 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005575
5576 /* If "option abortonclose" is set on the backend, we
5577 * want to monitor the client's connection and forward
5578 * any shutdown notification to the server, which will
5579 * decide whether to close or to go on processing the
5580 * request.
5581 */
5582 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005583 channel_auto_read(req);
5584 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005585 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005586 else if (s->txn.meth == HTTP_METH_POST) {
5587 /* POST requests may require to read extra CRLF
5588 * sent by broken browsers and which could cause
5589 * an RST to be sent upon close on some systems
5590 * (eg: Linux).
5591 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005592 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005593 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005594
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 }
5597 }
5598
Willy Tarreaud98cf932009-12-27 22:54:55 +01005599 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005600 /* we may have some pending data starting at req->buf->p */
5601 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005602 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005603 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5604
Willy Tarreaubed410e2014-04-22 08:19:34 +02005605 msg->next = 0;
5606 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5607
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005608 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005609 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005610 if (!(s->flags & SN_ERR_MASK))
5611 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005612 if (!(s->flags & SN_FINST_MASK)) {
5613 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5614 s->flags |= SN_FINST_H;
5615 else
5616 s->flags |= SN_FINST_D;
5617 }
5618
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005619 s->fe->fe_counters.cli_aborts++;
5620 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005621 if (objt_server(s->target))
5622 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623
5624 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005625 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005626
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005627 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005628 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005630
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005631 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005632 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005633 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005634 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005635 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005636
Willy Tarreau5c620922011-05-11 19:56:11 +02005637 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005638 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005639 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005640 * modes are already handled by the stream sock layer. We must not do
5641 * this in content-length mode because it could present the MSG_MORE
5642 * flag with the last block of forwarded data, which would cause an
5643 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005644 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005645 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005646 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005647
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005648 return 0;
5649
Willy Tarreaud98cf932009-12-27 22:54:55 +01005650 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005651 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005652 if (s->listener->counters)
5653 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005654
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005655 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005656 /* we may have some pending data starting at req->buf->p */
5657 b_adv(req->buf, msg->next);
5658 msg->next = 0;
5659
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005660 txn->req.msg_state = HTTP_MSG_ERROR;
5661 if (txn->status) {
5662 /* Note: we don't send any error if some data were already sent */
5663 stream_int_retnclose(req->prod, NULL);
5664 } else {
5665 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005666 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005667 }
5668 req->analysers = 0;
5669 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670
5671 if (!(s->flags & SN_ERR_MASK))
5672 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005673 if (!(s->flags & SN_FINST_MASK)) {
5674 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5675 s->flags |= SN_FINST_H;
5676 else
5677 s->flags |= SN_FINST_D;
5678 }
5679 return 0;
5680
5681 aborted_xfer:
5682 txn->req.msg_state = HTTP_MSG_ERROR;
5683 if (txn->status) {
5684 /* Note: we don't send any error if some data were already sent */
5685 stream_int_retnclose(req->prod, NULL);
5686 } else {
5687 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005688 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005689 }
5690 req->analysers = 0;
5691 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5692
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005693 s->fe->fe_counters.srv_aborts++;
5694 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005695 if (objt_server(s->target))
5696 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697
5698 if (!(s->flags & SN_ERR_MASK))
5699 s->flags |= SN_ERR_SRVCL;
5700 if (!(s->flags & SN_FINST_MASK)) {
5701 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5702 s->flags |= SN_FINST_H;
5703 else
5704 s->flags |= SN_FINST_D;
5705 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005706 return 0;
5707}
5708
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5710 * processing can continue on next analysers, or zero if it either needs more
5711 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5712 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5713 * when it has nothing left to do, and may remove any analyser when it wants to
5714 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005715 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005716int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005717{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718 struct http_txn *txn = &s->txn;
5719 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005720 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005721 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005723 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005724
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005725 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 +02005726 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005727 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005728 rep,
5729 rep->rex, rep->wex,
5730 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005731 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005732 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005733
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005734 /*
5735 * Now parse the partial (or complete) lines.
5736 * We will check the response syntax, and also join multi-line
5737 * headers. An index of all the lines will be elaborated while
5738 * parsing.
5739 *
5740 * For the parsing, we use a 28 states FSM.
5741 *
5742 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005743 * rep->buf->p = beginning of response
5744 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5745 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005746 * msg->eol = end of current header or line (LF or CRLF)
5747 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005748 */
5749
Willy Tarreau628c40c2014-04-24 19:11:26 +02005750 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005751 /* There's a protected area at the end of the buffer for rewriting
5752 * purposes. We don't want to start to parse the request if the
5753 * protected area is affected, because we may have to move processed
5754 * data later, which is much more complicated.
5755 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005756 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005757 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005758 /* some data has still not left the buffer, wake us once that's done */
5759 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5760 goto abort_response;
5761 channel_dont_close(rep);
5762 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005763 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005764 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005765 }
5766
Willy Tarreau379357a2013-06-08 12:55:46 +02005767 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5768 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5769 buffer_slow_realign(rep->buf);
5770
Willy Tarreau9b28e032012-10-12 23:49:43 +02005771 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005772 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005773 }
5774
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005775 /* 1: we might have to print this header in debug mode */
5776 if (unlikely((global.mode & MODE_DEBUG) &&
5777 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005778 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780
Willy Tarreau9b28e032012-10-12 23:49:43 +02005781 sol = rep->buf->p;
5782 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 sol += hdr_idx_first_pos(&txn->hdr_idx);
5786 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005787
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005788 while (cur_idx) {
5789 eol = sol + txn->hdr_idx.v[cur_idx].len;
5790 debug_hdr("srvhdr", s, sol, eol);
5791 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5792 cur_idx = txn->hdr_idx.v[cur_idx].next;
5793 }
5794 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005795
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005796 /*
5797 * Now we quickly check if we have found a full valid response.
5798 * If not so, we check the FD and buffer states before leaving.
5799 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005800 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 * responses are checked first.
5802 *
5803 * Depending on whether the client is still there or not, we
5804 * may send an error response back or not. Note that normally
5805 * we should only check for HTTP status there, and check I/O
5806 * errors somewhere else.
5807 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005808
Willy Tarreau655dce92009-11-08 13:10:58 +01005809 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005810 /* Invalid response */
5811 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5812 /* we detected a parsing error. We want to archive this response
5813 * in the dedicated proxy area for later troubleshooting.
5814 */
5815 hdr_response_bad:
5816 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005817 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005818
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005819 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005820 if (objt_server(s->target)) {
5821 objt_server(s->target)->counters.failed_resp++;
5822 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005823 }
Willy Tarreau64648412010-03-05 10:41:54 +01005824 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005825 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005826 rep->analysers = 0;
5827 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005828 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005829 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005830 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005831
5832 if (!(s->flags & SN_ERR_MASK))
5833 s->flags |= SN_ERR_PRXCOND;
5834 if (!(s->flags & SN_FINST_MASK))
5835 s->flags |= SN_FINST_H;
5836
5837 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005838 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005839
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005840 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005841 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005842 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005843 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005844 goto hdr_response_bad;
5845 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005846
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005847 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005848 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005849 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005850 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005851 else if (txn->flags & TX_NOT_FIRST)
5852 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005853
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005854 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005855 if (objt_server(s->target)) {
5856 objt_server(s->target)->counters.failed_resp++;
5857 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005858 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005859
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005860 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861 rep->analysers = 0;
5862 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005863 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005864 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005865 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 if (!(s->flags & SN_ERR_MASK))
5868 s->flags |= SN_ERR_SRVCL;
5869 if (!(s->flags & SN_FINST_MASK))
5870 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005871 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005873
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005874 /* read timeout : return a 504 to the client. */
5875 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005876 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005877 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005878 else if (txn->flags & TX_NOT_FIRST)
5879 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005880
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005881 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005882 if (objt_server(s->target)) {
5883 objt_server(s->target)->counters.failed_resp++;
5884 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005885 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005886
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005887 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 rep->analysers = 0;
5889 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005890 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005891 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005892 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005893
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005894 if (!(s->flags & SN_ERR_MASK))
5895 s->flags |= SN_ERR_SRVTO;
5896 if (!(s->flags & SN_FINST_MASK))
5897 s->flags |= SN_FINST_H;
5898 return 0;
5899 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005900
Willy Tarreauf003d372012-11-26 13:35:37 +01005901 /* client abort with an abortonclose */
5902 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5903 s->fe->fe_counters.cli_aborts++;
5904 s->be->be_counters.cli_aborts++;
5905 if (objt_server(s->target))
5906 objt_server(s->target)->counters.cli_aborts++;
5907
5908 rep->analysers = 0;
5909 channel_auto_close(rep);
5910
5911 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005912 channel_truncate(rep);
Willy Tarreauf003d372012-11-26 13:35:37 +01005913 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5914
5915 if (!(s->flags & SN_ERR_MASK))
5916 s->flags |= SN_ERR_CLICL;
5917 if (!(s->flags & SN_FINST_MASK))
5918 s->flags |= SN_FINST_H;
5919
5920 /* process_session() will take care of the error */
5921 return 0;
5922 }
5923
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005924 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005925 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005926 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005927 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005928 else if (txn->flags & TX_NOT_FIRST)
5929 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005930
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005931 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005932 if (objt_server(s->target)) {
5933 objt_server(s->target)->counters.failed_resp++;
5934 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005935 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005936
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005937 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 rep->analysers = 0;
5939 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005940 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005941 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005942 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005943
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005944 if (!(s->flags & SN_ERR_MASK))
5945 s->flags |= SN_ERR_SRVCL;
5946 if (!(s->flags & SN_FINST_MASK))
5947 s->flags |= SN_FINST_H;
5948 return 0;
5949 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005952 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005954 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005955 else if (txn->flags & TX_NOT_FIRST)
5956 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005957
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005958 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005960 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005961
5962 if (!(s->flags & SN_ERR_MASK))
5963 s->flags |= SN_ERR_CLICL;
5964 if (!(s->flags & SN_FINST_MASK))
5965 s->flags |= SN_FINST_H;
5966
5967 /* process_session() will take care of the error */
5968 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005969 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005970
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005971 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005972 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 return 0;
5974 }
5975
5976 /* More interesting part now : we know that we have a complete
5977 * response which at least looks like HTTP. We have an indicator
5978 * of each header's length, so we can parse them quickly.
5979 */
5980
5981 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005982 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984 /*
5985 * 1: get the status code
5986 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005987 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005988 if (n < 1 || n > 5)
5989 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005990 /* when the client triggers a 4xx from the server, it's most often due
5991 * to a missing object or permission. These events should be tracked
5992 * because if they happen often, it may indicate a brute force or a
5993 * vulnerability scan.
5994 */
5995 if (n == 4)
5996 session_inc_http_err_ctr(s);
5997
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005998 if (objt_server(s->target))
5999 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000
Willy Tarreau5b154472009-12-21 20:11:07 +01006001 /* check if the response is HTTP/1.1 or above */
6002 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006003 ((rep->buf->p[5] > '1') ||
6004 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006005 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006006
6007 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006008 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 +01006009
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006010 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006011 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006012
Willy Tarreau9b28e032012-10-12 23:49:43 +02006013 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006014
Willy Tarreau39650402010-03-15 19:44:39 +01006015 /* Adjust server's health based on status code. Note: status codes 501
6016 * and 505 are triggered on demand by client request, so we must not
6017 * count them as server failures.
6018 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006019 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006020 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006021 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006022 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006023 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006024 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 /*
6027 * 2: check for cacheability.
6028 */
6029
6030 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006031 case 100:
6032 /*
6033 * We may be facing a 100-continue response, in which case this
6034 * is not the right response, and we're waiting for the next one.
6035 * Let's allow this response to go to the client and wait for the
6036 * next one.
6037 */
6038 hdr_idx_init(&txn->hdr_idx);
6039 msg->next -= channel_forward(rep, msg->next);
6040 msg->msg_state = HTTP_MSG_RPBEFORE;
6041 txn->status = 0;
6042 s->logs.t_data = -1; /* was not a response yet */
6043 goto next_one;
6044
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006045 case 200:
6046 case 203:
6047 case 206:
6048 case 300:
6049 case 301:
6050 case 410:
6051 /* RFC2616 @13.4:
6052 * "A response received with a status code of
6053 * 200, 203, 206, 300, 301 or 410 MAY be stored
6054 * by a cache (...) unless a cache-control
6055 * directive prohibits caching."
6056 *
6057 * RFC2616 @9.5: POST method :
6058 * "Responses to this method are not cacheable,
6059 * unless the response includes appropriate
6060 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006061 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006062 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006063 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6065 break;
6066 default:
6067 break;
6068 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006069
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070 /*
6071 * 3: we may need to capture headers
6072 */
6073 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006074 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006075 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076 txn->rsp.cap, s->fe->rsp_cap);
6077
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006078 /* 4: determine the transfer-length.
6079 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6080 * the presence of a message-body in a RESPONSE and its transfer length
6081 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006082 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006083 * All responses to the HEAD request method MUST NOT include a
6084 * message-body, even though the presence of entity-header fields
6085 * might lead one to believe they do. All 1xx (informational), 204
6086 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6087 * message-body. All other responses do include a message-body,
6088 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006089 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006090 * 1. Any response which "MUST NOT" include a message-body (such as the
6091 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6092 * always terminated by the first empty line after the header fields,
6093 * regardless of the entity-header fields present in the message.
6094 *
6095 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6096 * the "chunked" transfer-coding (Section 6.2) is used, the
6097 * transfer-length is defined by the use of this transfer-coding.
6098 * If a Transfer-Encoding header field is present and the "chunked"
6099 * transfer-coding is not present, the transfer-length is defined by
6100 * the sender closing the connection.
6101 *
6102 * 3. If a Content-Length header field is present, its decimal value in
6103 * OCTETs represents both the entity-length and the transfer-length.
6104 * If a message is received with both a Transfer-Encoding header
6105 * field and a Content-Length header field, the latter MUST be ignored.
6106 *
6107 * 4. If the message uses the media type "multipart/byteranges", and
6108 * the transfer-length is not otherwise specified, then this self-
6109 * delimiting media type defines the transfer-length. This media
6110 * type MUST NOT be used unless the sender knows that the recipient
6111 * can parse it; the presence in a request of a Range header with
6112 * multiple byte-range specifiers from a 1.1 client implies that the
6113 * client can parse multipart/byteranges responses.
6114 *
6115 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006116 */
6117
6118 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006119 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006120 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 * FIXME: should we parse anyway and return an error on chunked encoding ?
6122 */
6123 if (txn->meth == HTTP_METH_HEAD ||
6124 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006125 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006126 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006127 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006128 goto skip_content_length;
6129 }
6130
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006131 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006132 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006133 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006134 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006136 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6137 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006138 /* bad transfer-encoding (chunked followed by something else) */
6139 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006140 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006141 break;
6142 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006143 }
6144
6145 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6146 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006147 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006148 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006149 signed long long cl;
6150
Willy Tarreauad14f752011-09-02 20:33:27 +02006151 if (!ctx.vlen) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02006154 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155
Willy Tarreauad14f752011-09-02 20:33:27 +02006156 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006157 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006159 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160
Willy Tarreauad14f752011-09-02 20:33:27 +02006161 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006162 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006163 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006164 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006165
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006166 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006167 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006168 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006169 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006170
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006171 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006172 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006173 }
6174
William Lallemand82fe75c2012-10-23 10:25:10 +02006175 if (s->fe->comp || s->be->comp)
6176 select_compression_response_header(s, rep->buf);
6177
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006178skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006179 /* Now we have to check if we need to modify the Connection header.
6180 * This is more difficult on the response than it is on the request,
6181 * because we can have two different HTTP versions and we don't know
6182 * how the client will interprete a response. For instance, let's say
6183 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6184 * HTTP/1.1 response without any header. Maybe it will bound itself to
6185 * HTTP/1.0 because it only knows about it, and will consider the lack
6186 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6187 * the lack of header as a keep-alive. Thus we will use two flags
6188 * indicating how a request MAY be understood by the client. In case
6189 * of multiple possibilities, we'll fix the header to be explicit. If
6190 * ambiguous cases such as both close and keepalive are seen, then we
6191 * will fall back to explicit close. Note that we won't take risks with
6192 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006193 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006194 */
6195
Willy Tarreaudc008c52010-02-01 16:20:08 +01006196 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6197 txn->status == 101)) {
6198 /* Either we've established an explicit tunnel, or we're
6199 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006200 * to understand the next protocols. We have to switch to tunnel
6201 * mode, so that we transfer the request and responses then let
6202 * this protocol pass unmodified. When we later implement specific
6203 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006204 * header which contains information about that protocol for
6205 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006206 */
6207 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6208 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006209 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6210 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006211 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6212 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006213 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006214
Willy Tarreau70dffda2014-01-30 03:07:23 +01006215 /* this situation happens when combining pretend-keepalive with httpclose. */
6216 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006217 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6218 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006219 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6220
Willy Tarreau60466522010-01-18 19:08:45 +01006221 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006222 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006223 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6224 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006225
Willy Tarreau60466522010-01-18 19:08:45 +01006226 /* now adjust header transformations depending on current state */
6227 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6228 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6229 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006230 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006231 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006232 }
Willy Tarreau60466522010-01-18 19:08:45 +01006233 else { /* SCL / KAL */
6234 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006235 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006236 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006237 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006238
Willy Tarreau60466522010-01-18 19:08:45 +01006239 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006240 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006241
Willy Tarreau60466522010-01-18 19:08:45 +01006242 /* Some keep-alive responses are converted to Server-close if
6243 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006244 */
Willy Tarreau60466522010-01-18 19:08:45 +01006245 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6246 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006247 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006248 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006249 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006250 }
6251
Willy Tarreau7959a552013-09-23 16:44:27 +02006252 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006253 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006254
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006255 /* end of job, return OK */
6256 rep->analysers &= ~an_bit;
6257 rep->analyse_exp = TICK_ETERNITY;
6258 channel_auto_close(rep);
6259 return 1;
6260
6261 abort_keep_alive:
6262 /* A keep-alive request to the server failed on a network error.
6263 * The client is required to retry. We need to close without returning
6264 * any other information so that the client retries.
6265 */
6266 txn->status = 0;
6267 rep->analysers = 0;
6268 s->req->analysers = 0;
6269 channel_auto_close(rep);
6270 s->logs.logwait = 0;
6271 s->logs.level = 0;
6272 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006273 channel_truncate(rep);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006274 stream_int_retnclose(rep->cons, NULL);
6275 return 0;
6276}
6277
6278/* This function performs all the processing enabled for the current response.
6279 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6280 * and updates s->rep->analysers. It might make sense to explode it into several
6281 * other functions. It works like process_request (see indications above).
6282 */
6283int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6284{
6285 struct http_txn *txn = &s->txn;
6286 struct http_msg *msg = &txn->rsp;
6287 struct proxy *cur_proxy;
6288 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006289 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006290
6291 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6292 now_ms, __FUNCTION__,
6293 s,
6294 rep,
6295 rep->rex, rep->wex,
6296 rep->flags,
6297 rep->buf->i,
6298 rep->analysers);
6299
6300 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6301 return 0;
6302
6303 rep->analysers &= ~an_bit;
6304 rep->analyse_exp = TICK_ETERNITY;
6305
Willy Tarreau70730dd2014-04-24 18:06:27 +02006306 /* The stats applet needs to adjust the Connection header but we don't
6307 * apply any filter there.
6308 */
6309 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6310 goto skip_filters;
6311
Willy Tarreau58975672014-04-24 21:13:57 +02006312 /*
6313 * We will have to evaluate the filters.
6314 * As opposed to version 1.2, now they will be evaluated in the
6315 * filters order and not in the header order. This means that
6316 * each filter has to be validated among all headers.
6317 *
6318 * Filters are tried with ->be first, then with ->fe if it is
6319 * different from ->be.
6320 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006321
Willy Tarreau58975672014-04-24 21:13:57 +02006322 cur_proxy = s->be;
6323 while (1) {
6324 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006325
Willy Tarreau58975672014-04-24 21:13:57 +02006326 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006327 if (ret == HTTP_RULE_RES_CONT)
6328 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006329
Willy Tarreau58975672014-04-24 21:13:57 +02006330 /* try headers filters */
6331 if (rule_set->rsp_exp != NULL) {
6332 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6333 return_bad_resp:
6334 if (objt_server(s->target)) {
6335 objt_server(s->target)->counters.failed_resp++;
6336 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006337 }
Willy Tarreau58975672014-04-24 21:13:57 +02006338 s->be->be_counters.failed_resp++;
6339 return_srv_prx_502:
6340 rep->analysers = 0;
6341 txn->status = 502;
6342 s->logs.t_data = -1; /* was not a valid response */
6343 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006344 channel_truncate(rep);
Willy Tarreau58975672014-04-24 21:13:57 +02006345 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6346 if (!(s->flags & SN_ERR_MASK))
6347 s->flags |= SN_ERR_PRXCOND;
6348 if (!(s->flags & SN_FINST_MASK))
6349 s->flags |= SN_FINST_H;
6350 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006351 }
Willy Tarreau58975672014-04-24 21:13:57 +02006352 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006353
Willy Tarreau58975672014-04-24 21:13:57 +02006354 /* has the response been denied ? */
6355 if (txn->flags & TX_SVDENY) {
6356 if (objt_server(s->target))
6357 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006358
Willy Tarreau58975672014-04-24 21:13:57 +02006359 s->be->be_counters.denied_resp++;
6360 s->fe->fe_counters.denied_resp++;
6361 if (s->listener->counters)
6362 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006363
Willy Tarreau58975672014-04-24 21:13:57 +02006364 goto return_srv_prx_502;
6365 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006366
Willy Tarreau58975672014-04-24 21:13:57 +02006367 /* add response headers from the rule sets in the same order */
6368 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006369 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006370 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006371 if (wl->cond) {
6372 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6373 ret = acl_pass(ret);
6374 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6375 ret = !ret;
6376 if (!ret)
6377 continue;
6378 }
6379 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6380 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006381 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006382
Willy Tarreau58975672014-04-24 21:13:57 +02006383 /* check whether we're already working on the frontend */
6384 if (cur_proxy == s->fe)
6385 break;
6386 cur_proxy = s->fe;
6387 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006388
Willy Tarreau58975672014-04-24 21:13:57 +02006389 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006390 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006391 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006392
Willy Tarreau58975672014-04-24 21:13:57 +02006393 /*
6394 * Now check for a server cookie.
6395 */
6396 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6397 (s->be->options & PR_O_CHK_CACHE))
6398 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006399
Willy Tarreau58975672014-04-24 21:13:57 +02006400 /*
6401 * Check for cache-control or pragma headers if required.
6402 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006403 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 +02006404 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006405
Willy Tarreau58975672014-04-24 21:13:57 +02006406 /*
6407 * Add server cookie in the response if needed
6408 */
6409 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6410 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6411 (!(s->flags & SN_DIRECT) ||
6412 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6413 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6414 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6415 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6416 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6417 !(s->flags & SN_IGNORE_PRST)) {
6418 /* the server is known, it's not the one the client requested, or the
6419 * cookie's last seen date needs to be refreshed. We have to
6420 * insert a set-cookie here, except if we want to insert only on POST
6421 * requests and this one isn't. Note that servers which don't have cookies
6422 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 */
Willy Tarreau58975672014-04-24 21:13:57 +02006424 if (!objt_server(s->target)->cookie) {
6425 chunk_printf(&trash,
6426 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6427 s->be->cookie_name);
6428 }
6429 else {
6430 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006431
Willy Tarreau58975672014-04-24 21:13:57 +02006432 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6433 /* emit last_date, which is mandatory */
6434 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6435 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6436 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006437
Willy Tarreau58975672014-04-24 21:13:57 +02006438 if (s->be->cookie_maxlife) {
6439 /* emit first_date, which is either the original one or
6440 * the current date.
6441 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006442 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006443 s30tob64(txn->cookie_first_date ?
6444 txn->cookie_first_date >> 2 :
6445 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006446 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006447 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006448 }
Willy Tarreau58975672014-04-24 21:13:57 +02006449 chunk_appendf(&trash, "; path=/");
6450 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006451
Willy Tarreau58975672014-04-24 21:13:57 +02006452 if (s->be->cookie_domain)
6453 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006454
Willy Tarreau58975672014-04-24 21:13:57 +02006455 if (s->be->ck_opts & PR_CK_HTTPONLY)
6456 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006457
Willy Tarreau58975672014-04-24 21:13:57 +02006458 if (s->be->ck_opts & PR_CK_SECURE)
6459 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006460
Willy Tarreau58975672014-04-24 21:13:57 +02006461 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6462 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006463
Willy Tarreau58975672014-04-24 21:13:57 +02006464 txn->flags &= ~TX_SCK_MASK;
6465 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6466 /* the server did not change, only the date was updated */
6467 txn->flags |= TX_SCK_UPDATED;
6468 else
6469 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006470
Willy Tarreau58975672014-04-24 21:13:57 +02006471 /* Here, we will tell an eventual cache on the client side that we don't
6472 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6473 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6474 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006475 */
Willy Tarreau58975672014-04-24 21:13:57 +02006476 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006477
Willy Tarreau58975672014-04-24 21:13:57 +02006478 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006479
Willy Tarreau58975672014-04-24 21:13:57 +02006480 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6481 "Cache-control: private", 22) < 0))
6482 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006483 }
Willy Tarreau58975672014-04-24 21:13:57 +02006484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485
Willy Tarreau58975672014-04-24 21:13:57 +02006486 /*
6487 * Check if result will be cacheable with a cookie.
6488 * We'll block the response if security checks have caught
6489 * nasty things such as a cacheable cookie.
6490 */
6491 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6492 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6493 (s->be->options & PR_O_CHK_CACHE)) {
6494 /* we're in presence of a cacheable response containing
6495 * a set-cookie header. We'll block it as requested by
6496 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006497 */
Willy Tarreau58975672014-04-24 21:13:57 +02006498 if (objt_server(s->target))
6499 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006500
Willy Tarreau58975672014-04-24 21:13:57 +02006501 s->be->be_counters.denied_resp++;
6502 s->fe->fe_counters.denied_resp++;
6503 if (s->listener->counters)
6504 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505
Willy Tarreau58975672014-04-24 21:13:57 +02006506 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6507 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6508 send_log(s->be, LOG_ALERT,
6509 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6510 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6511 goto return_srv_prx_502;
6512 }
Willy Tarreau03945942009-12-22 16:50:27 +01006513
Willy Tarreau70730dd2014-04-24 18:06:27 +02006514 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006515 /*
6516 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6517 * If an "Upgrade" token is found, the header is left untouched in order
6518 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006519 * if anything but "Upgrade" is present in the Connection header. We don't
6520 * want to touch any 101 response either since it's switching to another
6521 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006522 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006523 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006524 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6525 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6526 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6527 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006528
Willy Tarreau58975672014-04-24 21:13:57 +02006529 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6531 /* we want a keep-alive response here. Keep-alive header
6532 * required if either side is not 1.1.
6533 */
6534 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6535 want_flags |= TX_CON_KAL_SET;
6536 }
6537 else {
6538 /* we want a close response here. Close header required if
6539 * the server is 1.1, regardless of the client.
6540 */
6541 if (msg->flags & HTTP_MSGF_VER_11)
6542 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006543 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544
Willy Tarreau58975672014-04-24 21:13:57 +02006545 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6546 http_change_connection_header(txn, msg, want_flags);
6547 }
6548
6549 skip_header_mangling:
6550 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6551 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6552 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006553
Willy Tarreau58975672014-04-24 21:13:57 +02006554 /* if the user wants to log as soon as possible, without counting
6555 * bytes from the server, then this is the right moment. We have
6556 * to temporarily assign bytes_out to log what we currently have.
6557 */
6558 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6559 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6560 s->logs.bytes_out = txn->rsp.eoh;
6561 s->do_log(s);
6562 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006563 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006564 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006565}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566
Willy Tarreaud98cf932009-12-27 22:54:55 +01006567/* This function is an analyser which forwards response body (including chunk
6568 * sizes if any). It is called as soon as we must forward, even if we forward
6569 * zero byte. The only situation where it must not be called is when we're in
6570 * tunnel mode and we want to forward till the close. It's used both to forward
6571 * remaining data and to resync after end of body. It expects the msg_state to
6572 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6573 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006574 *
6575 * It is capable of compressing response data both in content-length mode and
6576 * in chunked mode. The state machines follows different flows depending on
6577 * whether content-length and chunked modes are used, since there are no
6578 * trailers in content-length :
6579 *
6580 * chk-mode cl-mode
6581 * ,----- BODY -----.
6582 * / \
6583 * V size > 0 V chk-mode
6584 * .--> SIZE -------------> DATA -------------> CRLF
6585 * | | size == 0 | last byte |
6586 * | v final crlf v inspected |
6587 * | TRAILERS -----------> DONE |
6588 * | |
6589 * `----------------------------------------------'
6590 *
6591 * Compression only happens in the DATA state, and must be flushed in final
6592 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6593 * is performed at once on final states for all bytes parsed, or when leaving
6594 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006595 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006596int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006597{
6598 struct http_txn *txn = &s->txn;
6599 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006600 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006601 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006602 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006603
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006604 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6605 return 0;
6606
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006607 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006608 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006609 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006610 /* Output closed while we were sending data. We must abort and
6611 * wake the other side up.
6612 */
6613 msg->msg_state = HTTP_MSG_ERROR;
6614 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006615 return 1;
6616 }
6617
Willy Tarreau4fe41902010-06-07 22:27:41 +02006618 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006619 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006620
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006621 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006622 /* we have msg->sov which points to the first byte of message
6623 * body, and res->buf.p still points to the beginning of the
6624 * message. We forward the headers now, as we don't need them
6625 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006626 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006627 b_adv(res->buf, msg->sov);
6628 msg->next -= msg->sov;
6629 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006630
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006631 /* The previous analysers guarantee that the state is somewhere
6632 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6633 * msg->next are always correct.
6634 */
6635 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6636 if (msg->flags & HTTP_MSGF_TE_CHNK)
6637 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6638 else
6639 msg->msg_state = HTTP_MSG_DATA;
6640 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006641 }
6642
Willy Tarreauefdf0942014-04-24 20:08:57 +02006643 if (res->to_forward) {
6644 /* We can't process the buffer's contents yet */
6645 res->flags |= CF_WAKE_WRITE;
6646 goto missing_data;
6647 }
6648
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006649 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6650 /* We need a compression buffer in the DATA state to put the
6651 * output of compressed data, and in CRLF state to let the
6652 * TRAILERS state finish the job of removing the trailing CRLF.
6653 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006654 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006655 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006656 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006657 goto aborted_xfer; /* no memory */
6658 }
6659
6660 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006661 if (ret < 0) {
6662 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006663 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006664 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006665 compressing = 1;
6666 }
6667
Willy Tarreaud98cf932009-12-27 22:54:55 +01006668 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006669 switch (msg->msg_state - HTTP_MSG_DATA) {
6670 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006671 /* we may have some pending data starting at res->buf->p */
6672 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006673 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006674 if (ret < 0)
6675 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006676
Willy Tarreaud5a67832014-04-21 10:54:27 +02006677 if (msg->chunk_len) {
6678 /* input empty or output full */
6679 if (res->buf->i > msg->next)
6680 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006681 goto missing_data;
6682 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006683 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006684 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006685 if (msg->chunk_len > res->buf->i - msg->next) {
6686 /* output full */
6687 res->flags |= CF_WAKE_WRITE;
6688 goto missing_data;
6689 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006690 msg->next += msg->chunk_len;
6691 msg->chunk_len = 0;
6692 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006693
6694 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006695 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006696 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006697 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006698 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006699 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006700 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006701 /* fall through for HTTP_MSG_CHUNK_CRLF */
6702
6703 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6704 /* we want the CRLF after the data */
6705
6706 ret = http_skip_chunk_crlf(msg);
6707 if (ret == 0)
6708 goto missing_data;
6709 else if (ret < 0) {
6710 if (msg->err_pos >= 0)
6711 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6712 goto return_bad_res;
6713 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006714 /* we're in MSG_CHUNK_SIZE now, fall through */
6715
6716 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006717 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006718 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006719 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006720 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006721
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006722 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006723 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006724 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006725 else if (ret < 0) {
6726 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006727 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006728 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006729 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006730 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006731 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006732
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006733 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006734 if (unlikely(compressing)) {
6735 /* we need to flush output contents before syncing FSMs */
6736 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6737 compressing = 0;
6738 }
6739
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006740 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006741 if (ret == 0)
6742 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006743 else if (ret < 0) {
6744 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006745 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006746 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006747 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006748 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006749
6750 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006751 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006752 if (unlikely(compressing)) {
6753 /* we need to flush output contents before syncing FSMs */
6754 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6755 compressing = 0;
6756 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006757
Willy Tarreauc623c172014-04-18 09:53:50 +02006758 /* we may have some pending data starting at res->buf->p
6759 * such as a last chunk of data or trailers.
6760 */
6761 b_adv(res->buf, msg->next);
6762 msg->next = 0;
6763
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006764 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006765 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006766 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6767 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006768 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006769
Willy Tarreau610ecce2010-01-04 21:15:02 +01006770 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006771 /* some state changes occurred, maybe the analyser
6772 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006773 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006774 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006775 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006776 /* response errors are most likely due to
6777 * the client aborting the transfer.
6778 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006779 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006780 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006781 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006782 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006783 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006784 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006785 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006786 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006787 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006788 }
6789 }
6790
Willy Tarreaud98cf932009-12-27 22:54:55 +01006791 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006792 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006793 if (unlikely(compressing)) {
6794 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006795 compressing = 0;
6796 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006797
Willy Tarreauc623c172014-04-18 09:53:50 +02006798 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6799 b_adv(res->buf, msg->next);
6800 msg->next = 0;
6801 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6802 }
6803
Willy Tarreauf003d372012-11-26 13:35:37 +01006804 if (res->flags & CF_SHUTW)
6805 goto aborted_xfer;
6806
6807 /* stop waiting for data if the input is closed before the end. If the
6808 * client side was already closed, it means that the client has aborted,
6809 * so we don't want to count this as a server abort. Otherwise it's a
6810 * server abort.
6811 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006812 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006813 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006814 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006815 if (!(s->flags & SN_ERR_MASK))
6816 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006817 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006818 if (objt_server(s->target))
6819 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006820 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006821 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006822
Willy Tarreau40dba092010-03-04 18:14:51 +01006823 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006824 if (!s->req->analysers)
6825 goto return_bad_res;
6826
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006827 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006828 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006829 * Similarly, with keep-alive on the client side, we don't want to forward a
6830 * close.
6831 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006832 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006833 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6834 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006835 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006836
Willy Tarreau5c620922011-05-11 19:56:11 +02006837 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006838 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006839 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006840 * modes are already handled by the stream sock layer. We must not do
6841 * this in content-length mode because it could present the MSG_MORE
6842 * flag with the last block of forwarded data, which would cause an
6843 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006844 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006845 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006846 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006847
Willy Tarreaud98cf932009-12-27 22:54:55 +01006848 /* the session handler will take care of timeouts and errors */
6849 return 0;
6850
Willy Tarreau40dba092010-03-04 18:14:51 +01006851 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006852 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006853 if (objt_server(s->target))
6854 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006855
6856 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006857 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006858 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006859 compressing = 0;
6860 }
6861
Willy Tarreauc623c172014-04-18 09:53:50 +02006862 /* we may have some pending data starting at res->buf->p */
6863 if (s->comp_algo == NULL) {
6864 b_adv(res->buf, msg->next);
6865 msg->next = 0;
6866 }
6867
Willy Tarreaud98cf932009-12-27 22:54:55 +01006868 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006869 /* don't send any error message as we're in the body */
6870 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006871 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006872 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006873 if (objt_server(s->target))
6874 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006875
6876 if (!(s->flags & SN_ERR_MASK))
6877 s->flags |= SN_ERR_PRXCOND;
6878 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006879 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006880 return 0;
6881
6882 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006883 if (unlikely(compressing)) {
6884 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6885 compressing = 0;
6886 }
6887
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006888 txn->rsp.msg_state = HTTP_MSG_ERROR;
6889 /* don't send any error message as we're in the body */
6890 stream_int_retnclose(res->cons, NULL);
6891 res->analysers = 0;
6892 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006894 s->fe->fe_counters.cli_aborts++;
6895 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006896 if (objt_server(s->target))
6897 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006898
6899 if (!(s->flags & SN_ERR_MASK))
6900 s->flags |= SN_ERR_CLICL;
6901 if (!(s->flags & SN_FINST_MASK))
6902 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 return 0;
6904}
6905
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006906/* Iterate the same filter through all request headers.
6907 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 * Since it can manage the switch to another backend, it updates the per-proxy
6909 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006911int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006912{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006913 char *cur_ptr, *cur_end, *cur_next;
6914 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006915 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006916 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006917 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006919 last_hdr = 0;
6920
Willy Tarreau9b28e032012-10-12 23:49:43 +02006921 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006922 old_idx = 0;
6923
6924 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006925 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006926 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006927 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006928 (exp->action == ACT_ALLOW ||
6929 exp->action == ACT_DENY ||
6930 exp->action == ACT_TARPIT))
6931 return 0;
6932
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006933 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006934 if (!cur_idx)
6935 break;
6936
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006937 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006938 cur_ptr = cur_next;
6939 cur_end = cur_ptr + cur_hdr->len;
6940 cur_next = cur_end + cur_hdr->cr + 1;
6941
6942 /* Now we have one header between cur_ptr and cur_end,
6943 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006944 */
6945
Willy Tarreau15a53a42015-01-21 13:39:42 +01006946 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006947 switch (exp->action) {
6948 case ACT_SETBE:
6949 /* It is not possible to jump a second time.
6950 * FIXME: should we return an HTTP/500 here so that
6951 * the admin knows there's a problem ?
6952 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006953 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 break;
6955
6956 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006957 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 last_hdr = 1;
6959 break;
6960
6961 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006962 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006963 last_hdr = 1;
6964 break;
6965
6966 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006967 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006968 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969 break;
6970
6971 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006972 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006973 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006974 break;
6975
6976 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006977 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6978 if (trash.len < 0)
6979 return -1;
6980
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006981 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982 /* FIXME: if the user adds a newline in the replacement, the
6983 * index will not be recalculated for now, and the new line
6984 * will not be counted as a new header.
6985 */
6986
6987 cur_end += delta;
6988 cur_next += delta;
6989 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006990 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 break;
6992
6993 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006994 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006995 cur_next += delta;
6996
Willy Tarreaufa355d42009-11-29 18:12:29 +01006997 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006998 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6999 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 cur_hdr->len = 0;
7001 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007002 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 break;
7004
7005 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006 }
7007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008 /* keep the link from this header to next one in case of later
7009 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007010 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 old_idx = cur_idx;
7012 }
7013 return 0;
7014}
7015
7016
7017/* Apply the filter to the request line.
7018 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7019 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 * Since it can manage the switch to another backend, it updates the per-proxy
7021 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007023int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007024{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 char *cur_ptr, *cur_end;
7026 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007027 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007028 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007029
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 (exp->action == ACT_ALLOW ||
7034 exp->action == ACT_DENY ||
7035 exp->action == ACT_TARPIT))
7036 return 0;
7037 else if (exp->action == ACT_REMOVE)
7038 return 0;
7039
7040 done = 0;
7041
Willy Tarreau9b28e032012-10-12 23:49:43 +02007042 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007043 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044
7045 /* Now we have the request line between cur_ptr and cur_end */
7046
Willy Tarreau15a53a42015-01-21 13:39:42 +01007047 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048 switch (exp->action) {
7049 case ACT_SETBE:
7050 /* It is not possible to jump a second time.
7051 * FIXME: should we return an HTTP/500 here so that
7052 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007054 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 break;
7056
7057 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007058 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007059 done = 1;
7060 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007063 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007064 done = 1;
7065 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007067 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007068 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 done = 1;
7070 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007072 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007073 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 done = 1;
7075 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007078 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7079 if (trash.len < 0)
7080 return -1;
7081
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007082 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 /* FIXME: if the user adds a newline in the replacement, the
7084 * index will not be recalculated for now, and the new line
7085 * will not be counted as a new header.
7086 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007087
Willy Tarreaufa355d42009-11-29 18:12:29 +01007088 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007090 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007091 HTTP_MSG_RQMETH,
7092 cur_ptr, cur_end + 1,
7093 NULL, NULL);
7094 if (unlikely(!cur_end))
7095 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 /* we have a full request and we know that we have either a CR
7098 * or an LF at <ptr>.
7099 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007100 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7101 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 /* there is no point trying this regex on headers */
7103 return 1;
7104 }
7105 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007106 return done;
7107}
Willy Tarreau97de6242006-12-27 17:18:38 +01007108
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007112 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007113 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114 * unparsable request. Since it can manage the switch to another backend, it
7115 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007116 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007117int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007118{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007119 struct http_txn *txn = &s->txn;
7120 struct hdr_exp *exp;
7121
7122 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007125 /*
7126 * The interleaving of transformations and verdicts
7127 * makes it difficult to decide to continue or stop
7128 * the evaluation.
7129 */
7130
Willy Tarreau6c123b12010-01-28 20:22:06 +01007131 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7132 break;
7133
Willy Tarreau3d300592007-03-18 18:34:41 +01007134 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007136 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007138
7139 /* if this filter had a condition, evaluate it now and skip to
7140 * next filter if the condition does not match.
7141 */
7142 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007143 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007144 ret = acl_pass(ret);
7145 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7146 ret = !ret;
7147
7148 if (!ret)
7149 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007150 }
7151
7152 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007153 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 if (unlikely(ret < 0))
7155 return -1;
7156
7157 if (likely(ret == 0)) {
7158 /* The filter did not match the request, it can be
7159 * iterated through all headers.
7160 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007161 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7162 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007166}
7167
7168
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007171 * Try to retrieve the server associated to the appsession.
7172 * If the server is found, it's assigned to the session.
7173 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007174void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7175 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007176 appsess *asession = NULL;
7177 char *sessid_temp = NULL;
7178
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007179 if (len > s->be->appsession_len) {
7180 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007181 }
7182
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007183 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007184 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007185 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007186 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007187 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007188 }
7189
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007190 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007191 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007192 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007193 return;
7194 }
7195
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007196 memcpy(txn->sessid, buf, len);
7197 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007198 }
7199
7200 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7201 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007202 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007203 return;
7204 }
7205
Cyril Bontéb21570a2009-11-29 20:04:48 +01007206 memcpy(sessid_temp, buf, len);
7207 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007208
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007209 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007210 /* free previously allocated memory */
7211 pool_free2(apools.sessid, sessid_temp);
7212
7213 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007214 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7215 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007216 asession->request_count++;
7217
7218 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007219 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007220
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007221 while (srv) {
7222 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007223 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007224 (s->be->options & PR_O_PERSIST) ||
7225 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007226 /* we found the server and it's usable */
7227 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007228 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007229 s->flags |= SN_DIRECT | SN_ASSIGNED;
7230 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007231
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007232 break;
7233 } else {
7234 txn->flags &= ~TX_CK_MASK;
7235 txn->flags |= TX_CK_DOWN;
7236 }
7237 }
7238 srv = srv->next;
7239 }
7240 }
7241 }
7242}
7243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244/* Find the end of a cookie value contained between <s> and <e>. It works the
7245 * same way as with headers above except that the semi-colon also ends a token.
7246 * See RFC2965 for more information. Note that it requires a valid header to
7247 * return a valid result.
7248 */
7249char *find_cookie_value_end(char *s, const char *e)
7250{
7251 int quoted, qdpair;
7252
7253 quoted = qdpair = 0;
7254 for (; s < e; s++) {
7255 if (qdpair) qdpair = 0;
7256 else if (quoted) {
7257 if (*s == '\\') qdpair = 1;
7258 else if (*s == '"') quoted = 0;
7259 }
7260 else if (*s == '"') quoted = 1;
7261 else if (*s == ',' || *s == ';') return s;
7262 }
7263 return s;
7264}
7265
7266/* Delete a value in a header between delimiters <from> and <next> in buffer
7267 * <buf>. The number of characters displaced is returned, and the pointer to
7268 * the first delimiter is updated if required. The function tries as much as
7269 * possible to respect the following principles :
7270 * - replace <from> delimiter by the <next> one unless <from> points to a
7271 * colon, in which case <next> is simply removed
7272 * - set exactly one space character after the new first delimiter, unless
7273 * there are not enough characters in the block being moved to do so.
7274 * - remove unneeded spaces before the previous delimiter and after the new
7275 * one.
7276 *
7277 * It is the caller's responsibility to ensure that :
7278 * - <from> points to a valid delimiter or the colon ;
7279 * - <next> points to a valid delimiter or the final CR/LF ;
7280 * - there are non-space chars before <from> ;
7281 * - there is a CR/LF at or after <next>.
7282 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007283int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007284{
7285 char *prev = *from;
7286
7287 if (*prev == ':') {
7288 /* We're removing the first value, preserve the colon and add a
7289 * space if possible.
7290 */
7291 if (!http_is_crlf[(unsigned char)*next])
7292 next++;
7293 prev++;
7294 if (prev < next)
7295 *prev++ = ' ';
7296
7297 while (http_is_spht[(unsigned char)*next])
7298 next++;
7299 } else {
7300 /* Remove useless spaces before the old delimiter. */
7301 while (http_is_spht[(unsigned char)*(prev-1)])
7302 prev--;
7303 *from = prev;
7304
7305 /* copy the delimiter and if possible a space if we're
7306 * not at the end of the line.
7307 */
7308 if (!http_is_crlf[(unsigned char)*next]) {
7309 *prev++ = *next++;
7310 if (prev + 1 < next)
7311 *prev++ = ' ';
7312 while (http_is_spht[(unsigned char)*next])
7313 next++;
7314 }
7315 }
7316 return buffer_replace2(buf, prev, next, NULL, 0);
7317}
7318
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007319/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007320 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 * desirable to call it only when needed. This code is quite complex because
7322 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7323 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007325void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007326{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007327 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007328 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007329 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007330 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7331 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007333 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007334 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007335 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007337 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007339 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007341 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007342 hdr_beg = hdr_next;
7343 hdr_end = hdr_beg + cur_hdr->len;
7344 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007346 /* We have one full header between hdr_beg and hdr_end, and the
7347 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007348 * "Cookie:" headers.
7349 */
7350
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007351 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007352 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007353 old_idx = cur_idx;
7354 continue;
7355 }
7356
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007357 del_from = NULL; /* nothing to be deleted */
7358 preserve_hdr = 0; /* assume we may kill the whole header */
7359
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360 /* Now look for cookies. Conforming to RFC2109, we have to support
7361 * attributes whose name begin with a '$', and associate them with
7362 * the right cookie, if we want to delete this cookie.
7363 * So there are 3 cases for each cookie read :
7364 * 1) it's a special attribute, beginning with a '$' : ignore it.
7365 * 2) it's a server id cookie that we *MAY* want to delete : save
7366 * some pointers on it (last semi-colon, beginning of cookie...)
7367 * 3) it's an application cookie : we *MAY* have to delete a previous
7368 * "special" cookie.
7369 * At the end of loop, if a "special" cookie remains, we may have to
7370 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007371 * *MUST* delete it.
7372 *
7373 * Note: RFC2965 is unclear about the processing of spaces around
7374 * the equal sign in the ATTR=VALUE form. A careful inspection of
7375 * the RFC explicitly allows spaces before it, and not within the
7376 * tokens (attrs or values). An inspection of RFC2109 allows that
7377 * too but section 10.1.3 lets one think that spaces may be allowed
7378 * after the equal sign too, resulting in some (rare) buggy
7379 * implementations trying to do that. So let's do what servers do.
7380 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7381 * allowed quoted strings in values, with any possible character
7382 * after a backslash, including control chars and delimitors, which
7383 * causes parsing to become ambiguous. Browsers also allow spaces
7384 * within values even without quotes.
7385 *
7386 * We have to keep multiple pointers in order to support cookie
7387 * removal at the beginning, middle or end of header without
7388 * corrupting the header. All of these headers are valid :
7389 *
7390 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7391 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7392 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7393 * | | | | | | | | |
7394 * | | | | | | | | hdr_end <--+
7395 * | | | | | | | +--> next
7396 * | | | | | | +----> val_end
7397 * | | | | | +-----------> val_beg
7398 * | | | | +--------------> equal
7399 * | | | +----------------> att_end
7400 * | | +---------------------> att_beg
7401 * | +--------------------------> prev
7402 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007403 */
7404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7406 /* Iterate through all cookies on this line */
7407
7408 /* find att_beg */
7409 att_beg = prev + 1;
7410 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7411 att_beg++;
7412
7413 /* find att_end : this is the first character after the last non
7414 * space before the equal. It may be equal to hdr_end.
7415 */
7416 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 while (equal < hdr_end) {
7419 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007420 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007421 if (http_is_spht[(unsigned char)*equal++])
7422 continue;
7423 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424 }
7425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007426 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7427 * is between <att_beg> and <equal>, both may be identical.
7428 */
7429
7430 /* look for end of cookie if there is an equal sign */
7431 if (equal < hdr_end && *equal == '=') {
7432 /* look for the beginning of the value */
7433 val_beg = equal + 1;
7434 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7435 val_beg++;
7436
7437 /* find the end of the value, respecting quotes */
7438 next = find_cookie_value_end(val_beg, hdr_end);
7439
7440 /* make val_end point to the first white space or delimitor after the value */
7441 val_end = next;
7442 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7443 val_end--;
7444 } else {
7445 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 /* We have nothing to do with attributes beginning with '$'. However,
7449 * they will automatically be removed if a header before them is removed,
7450 * since they're supposed to be linked together.
7451 */
7452 if (*att_beg == '$')
7453 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455 /* Ignore cookies with no equal sign */
7456 if (equal == next) {
7457 /* This is not our cookie, so we must preserve it. But if we already
7458 * scheduled another cookie for removal, we cannot remove the
7459 * complete header, but we can remove the previous block itself.
7460 */
7461 preserve_hdr = 1;
7462 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007463 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007464 val_end += delta;
7465 next += delta;
7466 hdr_end += delta;
7467 hdr_next += delta;
7468 cur_hdr->len += delta;
7469 http_msg_move_end(&txn->req, delta);
7470 prev = del_from;
7471 del_from = NULL;
7472 }
7473 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 /* if there are spaces around the equal sign, we need to
7477 * strip them otherwise we'll get trouble for cookie captures,
7478 * or even for rewrites. Since this happens extremely rarely,
7479 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7482 int stripped_before = 0;
7483 int stripped_after = 0;
7484
7485 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007486 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 equal += stripped_before;
7488 val_beg += stripped_before;
7489 }
7490
7491 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007492 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 val_beg += stripped_after;
7494 stripped_before += stripped_after;
7495 }
7496
7497 val_end += stripped_before;
7498 next += stripped_before;
7499 hdr_end += stripped_before;
7500 hdr_next += stripped_before;
7501 cur_hdr->len += stripped_before;
7502 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007505
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007506 /* First, let's see if we want to capture this cookie. We check
7507 * that we don't already have a client side cookie, because we
7508 * can only capture one. Also as an optimisation, we ignore
7509 * cookies shorter than the declared name.
7510 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007511 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7512 (val_end - att_beg >= s->fe->capture_namelen) &&
7513 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 int log_len = val_end - att_beg;
7515
7516 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7517 Alert("HTTP logging : out of memory.\n");
7518 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007519 if (log_len > s->fe->capture_len)
7520 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007521 memcpy(txn->cli_cookie, att_beg, log_len);
7522 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525
Willy Tarreaubca99692010-10-06 19:25:55 +02007526 /* Persistence cookies in passive, rewrite or insert mode have the
7527 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007529 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007531 * For cookies in prefix mode, the form is :
7532 *
7533 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007534 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007535 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7536 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7537 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007540 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7541 * have the server ID between val_beg and delim, and the original cookie between
7542 * delim+1 and val_end. Otherwise, delim==val_end :
7543 *
7544 * Cookie: NAME=SRV; # in all but prefix modes
7545 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7546 * | || || | |+-> next
7547 * | || || | +--> val_end
7548 * | || || +---------> delim
7549 * | || |+------------> val_beg
7550 * | || +-------------> att_end = equal
7551 * | |+-----------------> att_beg
7552 * | +------------------> prev
7553 * +-------------------------> hdr_beg
7554 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007556 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 for (delim = val_beg; delim < val_end; delim++)
7558 if (*delim == COOKIE_DELIM)
7559 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007560 } else {
7561 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007563 /* Now check if the cookie contains a date field, which would
7564 * appear after a vertical bar ('|') just after the server name
7565 * and before the delimiter.
7566 */
7567 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7568 if (vbar1) {
7569 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007570 * right is the last seen date. It is a base64 encoded
7571 * 30-bit value representing the UNIX date since the
7572 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007573 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007574 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007575 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007576 if (val_end - vbar1 >= 5) {
7577 val = b64tos30(vbar1);
7578 if (val > 0)
7579 txn->cookie_last_date = val << 2;
7580 }
7581 /* look for a second vertical bar */
7582 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7583 if (vbar1 && (val_end - vbar1 > 5)) {
7584 val = b64tos30(vbar1 + 1);
7585 if (val > 0)
7586 txn->cookie_first_date = val << 2;
7587 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007588 }
7589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590
Willy Tarreauf64d1412010-10-07 20:06:11 +02007591 /* if the cookie has an expiration date and the proxy wants to check
7592 * it, then we do that now. We first check if the cookie is too old,
7593 * then only if it has expired. We detect strict overflow because the
7594 * time resolution here is not great (4 seconds). Cookies with dates
7595 * in the future are ignored if their offset is beyond one day. This
7596 * allows an admin to fix timezone issues without expiring everyone
7597 * and at the same time avoids keeping unwanted side effects for too
7598 * long.
7599 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007600 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7601 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007602 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007603 txn->flags &= ~TX_CK_MASK;
7604 txn->flags |= TX_CK_OLD;
7605 delim = val_beg; // let's pretend we have not found the cookie
7606 txn->cookie_first_date = 0;
7607 txn->cookie_last_date = 0;
7608 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007609 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7610 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007611 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007612 txn->flags &= ~TX_CK_MASK;
7613 txn->flags |= TX_CK_EXPIRED;
7614 delim = val_beg; // let's pretend we have not found the cookie
7615 txn->cookie_first_date = 0;
7616 txn->cookie_last_date = 0;
7617 }
7618
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007619 /* Here, we'll look for the first running server which supports the cookie.
7620 * This allows to share a same cookie between several servers, for example
7621 * to dedicate backup servers to specific servers only.
7622 * However, to prevent clients from sticking to cookie-less backup server
7623 * when they have incidentely learned an empty cookie, we simply ignore
7624 * empty cookies and mark them as invalid.
7625 * The same behaviour is applied when persistence must be ignored.
7626 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007627 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 while (srv) {
7631 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7632 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007633 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007634 (s->be->options & PR_O_PERSIST) ||
7635 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 /* we found the server and we can use it */
7637 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007638 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007639 s->flags |= SN_DIRECT | SN_ASSIGNED;
7640 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 break;
7642 } else {
7643 /* we found a server, but it's down,
7644 * mark it as such and go on in case
7645 * another one is available.
7646 */
7647 txn->flags &= ~TX_CK_MASK;
7648 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 srv = srv->next;
7652 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007653
Willy Tarreauf64d1412010-10-07 20:06:11 +02007654 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007655 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007657 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007658 txn->flags |= TX_CK_UNUSED;
7659 else
7660 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 /* depending on the cookie mode, we may have to either :
7664 * - delete the complete cookie if we're in insert+indirect mode, so that
7665 * the server never sees it ;
7666 * - remove the server id from the cookie value, and tag the cookie as an
7667 * application cookie so that it does not get accidentely removed later,
7668 * if we're in cookie prefix mode
7669 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007670 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreau9b28e032012-10-12 23:49:43 +02007673 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 val_end += delta;
7675 next += delta;
7676 hdr_end += delta;
7677 hdr_next += delta;
7678 cur_hdr->len += delta;
7679 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 del_from = NULL;
7682 preserve_hdr = 1; /* we want to keep this cookie */
7683 }
7684 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007685 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 del_from = prev;
7687 }
7688 } else {
7689 /* This is not our cookie, so we must preserve it. But if we already
7690 * scheduled another cookie for removal, we cannot remove the
7691 * complete header, but we can remove the previous block itself.
7692 */
7693 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007696 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007697 if (att_beg >= del_from)
7698 att_beg += delta;
7699 if (att_end >= del_from)
7700 att_end += delta;
7701 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 val_end += delta;
7703 next += delta;
7704 hdr_end += delta;
7705 hdr_next += delta;
7706 cur_hdr->len += delta;
7707 http_msg_move_end(&txn->req, delta);
7708 prev = del_from;
7709 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007714 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 int cmp_len, value_len;
7716 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007717
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007718 if (s->be->options2 & PR_O2_AS_PFX) {
7719 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7720 value_begin = att_beg + s->be->appsession_name_len;
7721 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 } else {
7723 cmp_len = att_end - att_beg;
7724 value_begin = val_beg;
7725 value_len = val_end - val_beg;
7726 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007727
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007729 if (cmp_len == s->be->appsession_name_len &&
7730 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7731 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 }
7734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 /* continue with next cookie on this header line */
7736 att_beg = next;
7737 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 /* There are no more cookies on this line.
7740 * We may still have one (or several) marked for deletion at the
7741 * end of the line. We must do this now in two ways :
7742 * - if some cookies must be preserved, we only delete from the
7743 * mark to the end of line ;
7744 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007749 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007751 cur_hdr->len += delta;
7752 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007753 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754
7755 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007756 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7757 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007759 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007760 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007762 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007763 }
7764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007768}
7769
7770
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771/* Iterate the same filter through all response headers contained in <rtr>.
7772 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7773 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007774int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 char *cur_ptr, *cur_end, *cur_next;
7777 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007778 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007780 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781
7782 last_hdr = 0;
7783
Willy Tarreau9b28e032012-10-12 23:49:43 +02007784 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785 old_idx = 0;
7786
7787 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007788 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007790 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791 (exp->action == ACT_ALLOW ||
7792 exp->action == ACT_DENY))
7793 return 0;
7794
7795 cur_idx = txn->hdr_idx.v[old_idx].next;
7796 if (!cur_idx)
7797 break;
7798
7799 cur_hdr = &txn->hdr_idx.v[cur_idx];
7800 cur_ptr = cur_next;
7801 cur_end = cur_ptr + cur_hdr->len;
7802 cur_next = cur_end + cur_hdr->cr + 1;
7803
7804 /* Now we have one header between cur_ptr and cur_end,
7805 * and the next header starts at cur_next.
7806 */
7807
Willy Tarreau15a53a42015-01-21 13:39:42 +01007808 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 switch (exp->action) {
7810 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007811 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 last_hdr = 1;
7813 break;
7814
7815 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007816 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817 last_hdr = 1;
7818 break;
7819
7820 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007821 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7822 if (trash.len < 0)
7823 return -1;
7824
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007825 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 /* FIXME: if the user adds a newline in the replacement, the
7827 * index will not be recalculated for now, and the new line
7828 * will not be counted as a new header.
7829 */
7830
7831 cur_end += delta;
7832 cur_next += delta;
7833 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007834 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 break;
7836
7837 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007838 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 cur_next += delta;
7840
Willy Tarreaufa355d42009-11-29 18:12:29 +01007841 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7843 txn->hdr_idx.used--;
7844 cur_hdr->len = 0;
7845 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007846 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847 break;
7848
7849 }
7850 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851
7852 /* keep the link from this header to next one in case of later
7853 * removal of next header.
7854 */
7855 old_idx = cur_idx;
7856 }
7857 return 0;
7858}
7859
7860
7861/* Apply the filter to the status line in the response buffer <rtr>.
7862 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7863 * or -1 if a replacement resulted in an invalid status line.
7864 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007866{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 char *cur_ptr, *cur_end;
7868 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007869 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007870 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871
7872
Willy Tarreau3d300592007-03-18 18:34:41 +01007873 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007875 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 (exp->action == ACT_ALLOW ||
7877 exp->action == ACT_DENY))
7878 return 0;
7879 else if (exp->action == ACT_REMOVE)
7880 return 0;
7881
7882 done = 0;
7883
Willy Tarreau9b28e032012-10-12 23:49:43 +02007884 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007885 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886
7887 /* Now we have the status line between cur_ptr and cur_end */
7888
Willy Tarreau15a53a42015-01-21 13:39:42 +01007889 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 switch (exp->action) {
7891 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007892 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 done = 1;
7894 break;
7895
7896 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007897 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898 done = 1;
7899 break;
7900
7901 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007902 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7903 if (trash.len < 0)
7904 return -1;
7905
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007906 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 /* FIXME: if the user adds a newline in the replacement, the
7908 * index will not be recalculated for now, and the new line
7909 * will not be counted as a new header.
7910 */
7911
Willy Tarreaufa355d42009-11-29 18:12:29 +01007912 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007913 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007914 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007915 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 cur_ptr, cur_end + 1,
7917 NULL, NULL);
7918 if (unlikely(!cur_end))
7919 return -1;
7920
7921 /* we have a full respnse and we know that we have either a CR
7922 * or an LF at <ptr>.
7923 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007924 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007925 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926 /* there is no point trying this regex on headers */
7927 return 1;
7928 }
7929 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 return done;
7931}
7932
7933
7934
7935/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007936 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007937 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7938 * unparsable response.
7939 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007940int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007942 struct http_txn *txn = &s->txn;
7943 struct hdr_exp *exp;
7944
7945 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946 int ret;
7947
7948 /*
7949 * The interleaving of transformations and verdicts
7950 * makes it difficult to decide to continue or stop
7951 * the evaluation.
7952 */
7953
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007954 if (txn->flags & TX_SVDENY)
7955 break;
7956
Willy Tarreau3d300592007-03-18 18:34:41 +01007957 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7959 exp->action == ACT_PASS)) {
7960 exp = exp->next;
7961 continue;
7962 }
7963
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007964 /* if this filter had a condition, evaluate it now and skip to
7965 * next filter if the condition does not match.
7966 */
7967 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007968 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007969 ret = acl_pass(ret);
7970 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7971 ret = !ret;
7972 if (!ret)
7973 continue;
7974 }
7975
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007977 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978 if (unlikely(ret < 0))
7979 return -1;
7980
7981 if (likely(ret == 0)) {
7982 /* The filter did not match the response, it can be
7983 * iterated through all headers.
7984 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007985 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7986 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 }
7989 return 0;
7990}
7991
7992
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007994 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007995 * desirable to call it only when needed. This function is also used when we
7996 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007998void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008000 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01008001 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008002 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008004 char *hdr_beg, *hdr_end, *hdr_next;
8005 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 /* Iterate through the headers.
8008 * we start with the start line.
8009 */
8010 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008011 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012
8013 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8014 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008015 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016
8017 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008018 hdr_beg = hdr_next;
8019 hdr_end = hdr_beg + cur_hdr->len;
8020 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021
Willy Tarreau24581ba2010-08-31 22:39:35 +02008022 /* We have one full header between hdr_beg and hdr_end, and the
8023 * next header starts at hdr_next. We're only interested in
8024 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 */
8026
Willy Tarreau24581ba2010-08-31 22:39:35 +02008027 is_cookie2 = 0;
8028 prev = hdr_beg + 10;
8029 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008030 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008031 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8032 if (!val) {
8033 old_idx = cur_idx;
8034 continue;
8035 }
8036 is_cookie2 = 1;
8037 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 }
8039
Willy Tarreau24581ba2010-08-31 22:39:35 +02008040 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8041 * <prev> points to the colon.
8042 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008043 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044
Willy Tarreau24581ba2010-08-31 22:39:35 +02008045 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8046 * check-cache is enabled) and we are not interested in checking
8047 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008048 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008049 if (s->be->cookie_name == NULL &&
8050 s->be->appsession_name == NULL &&
8051 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 return;
8053
Willy Tarreau24581ba2010-08-31 22:39:35 +02008054 /* OK so now we know we have to process this response cookie.
8055 * The format of the Set-Cookie header is slightly different
8056 * from the format of the Cookie header in that it does not
8057 * support the comma as a cookie delimiter (thus the header
8058 * cannot be folded) because the Expires attribute described in
8059 * the original Netscape's spec may contain an unquoted date
8060 * with a comma inside. We have to live with this because
8061 * many browsers don't support Max-Age and some browsers don't
8062 * support quoted strings. However the Set-Cookie2 header is
8063 * clean.
8064 *
8065 * We have to keep multiple pointers in order to support cookie
8066 * removal at the beginning, middle or end of header without
8067 * corrupting the header (in case of set-cookie2). A special
8068 * pointer, <scav> points to the beginning of the set-cookie-av
8069 * fields after the first semi-colon. The <next> pointer points
8070 * either to the end of line (set-cookie) or next unquoted comma
8071 * (set-cookie2). All of these headers are valid :
8072 *
8073 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8074 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8075 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8076 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8077 * | | | | | | | | | |
8078 * | | | | | | | | +-> next hdr_end <--+
8079 * | | | | | | | +------------> scav
8080 * | | | | | | +--------------> val_end
8081 * | | | | | +--------------------> val_beg
8082 * | | | | +----------------------> equal
8083 * | | | +------------------------> att_end
8084 * | | +----------------------------> att_beg
8085 * | +------------------------------> prev
8086 * +-----------------------------------------> hdr_beg
8087 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088
Willy Tarreau24581ba2010-08-31 22:39:35 +02008089 for (; prev < hdr_end; prev = next) {
8090 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091
Willy Tarreau24581ba2010-08-31 22:39:35 +02008092 /* find att_beg */
8093 att_beg = prev + 1;
8094 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8095 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096
Willy Tarreau24581ba2010-08-31 22:39:35 +02008097 /* find att_end : this is the first character after the last non
8098 * space before the equal. It may be equal to hdr_end.
8099 */
8100 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101
Willy Tarreau24581ba2010-08-31 22:39:35 +02008102 while (equal < hdr_end) {
8103 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8104 break;
8105 if (http_is_spht[(unsigned char)*equal++])
8106 continue;
8107 att_end = equal;
8108 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109
Willy Tarreau24581ba2010-08-31 22:39:35 +02008110 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8111 * is between <att_beg> and <equal>, both may be identical.
8112 */
8113
8114 /* look for end of cookie if there is an equal sign */
8115 if (equal < hdr_end && *equal == '=') {
8116 /* look for the beginning of the value */
8117 val_beg = equal + 1;
8118 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8119 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120
Willy Tarreau24581ba2010-08-31 22:39:35 +02008121 /* find the end of the value, respecting quotes */
8122 next = find_cookie_value_end(val_beg, hdr_end);
8123
8124 /* make val_end point to the first white space or delimitor after the value */
8125 val_end = next;
8126 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8127 val_end--;
8128 } else {
8129 /* <equal> points to next comma, semi-colon or EOL */
8130 val_beg = val_end = next = equal;
8131 }
8132
8133 if (next < hdr_end) {
8134 /* Set-Cookie2 supports multiple cookies, and <next> points to
8135 * a colon or semi-colon before the end. So skip all attr-value
8136 * pairs and look for the next comma. For Set-Cookie, since
8137 * commas are permitted in values, skip to the end.
8138 */
8139 if (is_cookie2)
8140 next = find_hdr_value_end(next, hdr_end);
8141 else
8142 next = hdr_end;
8143 }
8144
8145 /* Now everything is as on the diagram above */
8146
8147 /* Ignore cookies with no equal sign */
8148 if (equal == val_end)
8149 continue;
8150
8151 /* If there are spaces around the equal sign, we need to
8152 * strip them otherwise we'll get trouble for cookie captures,
8153 * or even for rewrites. Since this happens extremely rarely,
8154 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008155 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008156 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8157 int stripped_before = 0;
8158 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159
Willy Tarreau24581ba2010-08-31 22:39:35 +02008160 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008161 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008162 equal += stripped_before;
8163 val_beg += stripped_before;
8164 }
8165
8166 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008167 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 val_beg += stripped_after;
8169 stripped_before += stripped_after;
8170 }
8171
8172 val_end += stripped_before;
8173 next += stripped_before;
8174 hdr_end += stripped_before;
8175 hdr_next += stripped_before;
8176 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008177 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008178 }
8179
8180 /* First, let's see if we want to capture this cookie. We check
8181 * that we don't already have a server side cookie, because we
8182 * can only capture one. Also as an optimisation, we ignore
8183 * cookies shorter than the declared name.
8184 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008185 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008186 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008187 (val_end - att_beg >= s->fe->capture_namelen) &&
8188 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008189 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008190 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 Alert("HTTP logging : out of memory.\n");
8192 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008193 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008194 if (log_len > s->fe->capture_len)
8195 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008196 memcpy(txn->srv_cookie, att_beg, log_len);
8197 txn->srv_cookie[log_len] = 0;
8198 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 }
8200
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008201 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008203 if (!(s->flags & SN_IGNORE_PRST) &&
8204 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8205 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008206 /* assume passive cookie by default */
8207 txn->flags &= ~TX_SCK_MASK;
8208 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209
8210 /* If the cookie is in insert mode on a known server, we'll delete
8211 * this occurrence because we'll insert another one later.
8212 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008213 * a direct access.
8214 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008215 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008216 /* The "preserve" flag was set, we don't want to touch the
8217 * server's cookie.
8218 */
8219 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8221 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 /* this cookie must be deleted */
8223 if (*prev == ':' && next == hdr_end) {
8224 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008225 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8227 txn->hdr_idx.used--;
8228 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008229 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 hdr_next += delta;
8231 http_msg_move_end(&txn->rsp, delta);
8232 /* note: while both invalid now, <next> and <hdr_end>
8233 * are still equal, so the for() will stop as expected.
8234 */
8235 } else {
8236 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008237 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 next = prev;
8239 hdr_end += delta;
8240 hdr_next += delta;
8241 cur_hdr->len += delta;
8242 http_msg_move_end(&txn->rsp, delta);
8243 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008244 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008245 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008248 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250 * with this server since we know it.
8251 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008252 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 next += delta;
8254 hdr_end += delta;
8255 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008257 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258
Willy Tarreauf1348312010-10-07 15:54:11 +02008259 txn->flags &= ~TX_SCK_MASK;
8260 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008262 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008263 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008266 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 next += delta;
8268 hdr_end += delta;
8269 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008271 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272
Willy Tarreau827aee92011-03-10 16:55:02 +01008273 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008274 txn->flags &= ~TX_SCK_MASK;
8275 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008276 }
8277 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008278 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008279 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008280 int cmp_len, value_len;
8281 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008282
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008283 if (s->be->options2 & PR_O2_AS_PFX) {
8284 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8285 value_begin = att_beg + s->be->appsession_name_len;
8286 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008287 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 cmp_len = att_end - att_beg;
8289 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008291 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008292
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008293 if ((cmp_len == s->be->appsession_name_len) &&
8294 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* free a possibly previously allocated memory */
8296 pool_free2(apools.sessid, txn->sessid);
8297
Cyril Bontéb21570a2009-11-29 20:04:48 +01008298 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008299 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008300 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008301 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008302 return;
8303 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008304 memcpy(txn->sessid, value_begin, value_len);
8305 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008306 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008307 }
8308 /* that's done for this cookie, check the next one on the same
8309 * line when next != hdr_end (only if is_cookie2).
8310 */
8311 }
8312 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008314 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008315
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008316 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008317 appsess *asession = NULL;
8318 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008319 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008320 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008321 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008322 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8323 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008324 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008325 return;
8326 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008327 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8328
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008329 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8330 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008331 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8332 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008333 return;
8334 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008335 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8336 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008337
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008338 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008339 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008340 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008341 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8342 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008343 return;
8344 }
8345 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008346 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008347
8348 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008349 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008350 }
8351
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008352 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008353 asession->request_count++;
8354 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008355}
8356
8357
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008359 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008360 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008363 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008364 char *p1, *p2;
8365
8366 char *cur_ptr, *cur_end, *cur_next;
8367 int cur_idx;
8368
Willy Tarreau5df51872007-11-25 16:20:08 +01008369 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370 return;
8371
8372 /* Iterate through the headers.
8373 * we start with the start line.
8374 */
8375 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008376 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377
8378 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8379 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008380 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008381
8382 cur_hdr = &txn->hdr_idx.v[cur_idx];
8383 cur_ptr = cur_next;
8384 cur_end = cur_ptr + cur_hdr->len;
8385 cur_next = cur_end + cur_hdr->cr + 1;
8386
8387 /* We have one full header between cur_ptr and cur_end, and the
8388 * next header starts at cur_next. We're only interested in
8389 * "Cookie:" headers.
8390 */
8391
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008392 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8393 if (val) {
8394 if ((cur_end - (cur_ptr + val) >= 8) &&
8395 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8396 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8397 return;
8398 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399 }
8400
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008401 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8402 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008403 continue;
8404
8405 /* OK, right now we know we have a cache-control header at cur_ptr */
8406
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008407 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008408
8409 if (p1 >= cur_end) /* no more info */
8410 continue;
8411
8412 /* p1 is at the beginning of the value */
8413 p2 = p1;
8414
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008415 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008416 p2++;
8417
8418 /* we have a complete value between p1 and p2 */
8419 if (p2 < cur_end && *p2 == '=') {
8420 /* we have something of the form no-cache="set-cookie" */
8421 if ((cur_end - p1 >= 21) &&
8422 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8423 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008424 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425 continue;
8426 }
8427
8428 /* OK, so we know that either p2 points to the end of string or to a comma */
8429 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008430 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008431 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8432 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8433 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008434 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008435 return;
8436 }
8437
8438 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008439 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440 continue;
8441 }
8442 }
8443}
8444
8445
Willy Tarreau58f10d72006-12-04 02:26:12 +01008446/*
8447 * Try to retrieve a known appsession in the URI, then the associated server.
8448 * If the server is found, it's assigned to the session.
8449 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008450void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008451{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008452 char *end_params, *first_param, *cur_param, *next_param;
8453 char separator;
8454 int value_len;
8455
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008456 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008457
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008458 if (s->be->appsession_name == NULL ||
8459 (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 +01008460 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008462
Cyril Bontéb21570a2009-11-29 20:04:48 +01008463 first_param = NULL;
8464 switch (mode) {
8465 case PR_O2_AS_M_PP:
8466 first_param = memchr(begin, ';', len);
8467 break;
8468 case PR_O2_AS_M_QS:
8469 first_param = memchr(begin, '?', len);
8470 break;
8471 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008472
Cyril Bontéb21570a2009-11-29 20:04:48 +01008473 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008474 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008476
Cyril Bontéb21570a2009-11-29 20:04:48 +01008477 switch (mode) {
8478 case PR_O2_AS_M_PP:
8479 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8480 end_params = (char *) begin + len;
8481 }
8482 separator = ';';
8483 break;
8484 case PR_O2_AS_M_QS:
8485 end_params = (char *) begin + len;
8486 separator = '&';
8487 break;
8488 default:
8489 /* unknown mode, shouldn't happen */
8490 return;
8491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008492
Cyril Bontéb21570a2009-11-29 20:04:48 +01008493 cur_param = next_param = end_params;
8494 while (cur_param > first_param) {
8495 cur_param--;
8496 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8497 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8499 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8500 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008501 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008502 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8503 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008504 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008505 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008506 }
8507 break;
8508 }
8509 next_param = cur_param;
8510 }
8511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008512#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008513 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008514 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008515#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008516}
8517
Willy Tarreaub2513902006-12-17 14:52:38 +01008518/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008519 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008520 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008521 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008522 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008523 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008524 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008525 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008526 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008527int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008528{
8529 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008530 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008531 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008532
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008533 if (!uri_auth)
8534 return 0;
8535
Cyril Bonté70be45d2010-10-12 00:14:35 +02008536 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008537 return 0;
8538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008539 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008540 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008541 return 0;
8542
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008543 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008544 return 0;
8545
Willy Tarreaub2513902006-12-17 14:52:38 +01008546 return 1;
8547}
8548
Willy Tarreau4076a152009-04-02 15:18:36 +02008549/*
8550 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008551 * By default it tries to report the error position as msg->err_pos. However if
8552 * this one is not set, it will then report msg->next, which is the last known
8553 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008554 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008555 */
8556void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008557 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008558 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008559{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008560 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008561 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008562
Willy Tarreau9b28e032012-10-12 23:49:43 +02008563 es->len = MIN(chn->buf->i, sizeof(es->buf));
8564 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008565 len1 = MIN(len1, es->len);
8566 len2 = es->len - len1; /* remaining data if buffer wraps */
8567
Willy Tarreau9b28e032012-10-12 23:49:43 +02008568 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008569 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008570 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008571
Willy Tarreau4076a152009-04-02 15:18:36 +02008572 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008573 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008574 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008575 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008576
Willy Tarreau4076a152009-04-02 15:18:36 +02008577 es->when = date; // user-visible date
8578 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008579 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008580 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008581 if (objt_conn(s->req->prod->end))
8582 es->src = __objt_conn(s->req->prod->end)->addr.from;
8583 else
8584 memset(&es->src, 0, sizeof(es->src));
8585
Willy Tarreau078272e2010-12-12 12:46:33 +01008586 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008587 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008588 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008589 es->s_flags = s->flags;
8590 es->t_flags = s->txn.flags;
8591 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008592 es->b_out = chn->buf->o;
8593 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008594 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008595 es->m_clen = msg->chunk_len;
8596 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008597}
Willy Tarreaub2513902006-12-17 14:52:38 +01008598
Willy Tarreau294c4732011-12-16 21:35:50 +01008599/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8600 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8601 * performed over the whole headers. Otherwise it must contain a valid header
8602 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8603 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8604 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8605 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008606 * -1. The value fetch stops at commas, so this function is suited for use with
8607 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008608 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008609 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008610unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008611 struct hdr_idx *idx, int occ,
8612 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008613{
Willy Tarreau294c4732011-12-16 21:35:50 +01008614 struct hdr_ctx local_ctx;
8615 char *ptr_hist[MAX_HDR_HISTORY];
8616 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008617 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008618 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008619
Willy Tarreau294c4732011-12-16 21:35:50 +01008620 if (!ctx) {
8621 local_ctx.idx = 0;
8622 ctx = &local_ctx;
8623 }
8624
Willy Tarreaubce70882009-09-07 11:51:47 +02008625 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008626 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008627 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008628 occ--;
8629 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008630 *vptr = ctx->line + ctx->val;
8631 *vlen = ctx->vlen;
8632 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008633 }
8634 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008635 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008636 }
8637
8638 /* negative occurrence, we scan all the list then walk back */
8639 if (-occ > MAX_HDR_HISTORY)
8640 return 0;
8641
Willy Tarreau294c4732011-12-16 21:35:50 +01008642 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008643 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008644 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8645 len_hist[hist_ptr] = ctx->vlen;
8646 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008647 hist_ptr = 0;
8648 found++;
8649 }
8650 if (-occ > found)
8651 return 0;
8652 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008653 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8654 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8655 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008656 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008657 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008658 if (hist_ptr >= MAX_HDR_HISTORY)
8659 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008660 *vptr = ptr_hist[hist_ptr];
8661 *vlen = len_hist[hist_ptr];
8662 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008663}
8664
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008665/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8666 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8667 * performed over the whole headers. Otherwise it must contain a valid header
8668 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8669 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8670 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8671 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8672 * -1. This function differs from http_get_hdr() in that it only returns full
8673 * line header values and does not stop at commas.
8674 * The return value is 0 if nothing was found, or non-zero otherwise.
8675 */
8676unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8677 struct hdr_idx *idx, int occ,
8678 struct hdr_ctx *ctx, char **vptr, int *vlen)
8679{
8680 struct hdr_ctx local_ctx;
8681 char *ptr_hist[MAX_HDR_HISTORY];
8682 int len_hist[MAX_HDR_HISTORY];
8683 unsigned int hist_ptr;
8684 int found;
8685
8686 if (!ctx) {
8687 local_ctx.idx = 0;
8688 ctx = &local_ctx;
8689 }
8690
8691 if (occ >= 0) {
8692 /* search from the beginning */
8693 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8694 occ--;
8695 if (occ <= 0) {
8696 *vptr = ctx->line + ctx->val;
8697 *vlen = ctx->vlen;
8698 return 1;
8699 }
8700 }
8701 return 0;
8702 }
8703
8704 /* negative occurrence, we scan all the list then walk back */
8705 if (-occ > MAX_HDR_HISTORY)
8706 return 0;
8707
8708 found = hist_ptr = 0;
8709 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8710 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8711 len_hist[hist_ptr] = ctx->vlen;
8712 if (++hist_ptr >= MAX_HDR_HISTORY)
8713 hist_ptr = 0;
8714 found++;
8715 }
8716 if (-occ > found)
8717 return 0;
8718 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8719 * find occurrence -occ, so we have to check [hist_ptr+occ].
8720 */
8721 hist_ptr += occ;
8722 if (hist_ptr >= MAX_HDR_HISTORY)
8723 hist_ptr -= MAX_HDR_HISTORY;
8724 *vptr = ptr_hist[hist_ptr];
8725 *vlen = len_hist[hist_ptr];
8726 return 1;
8727}
8728
Willy Tarreaubaaee002006-06-26 02:48:02 +02008729/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008730 * Print a debug line with a header. Always stop at the first CR or LF char,
8731 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8732 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008733 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008734void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008735{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008736 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008737 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008738 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008739 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8740 objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008741
8742 for (max = 0; start + max < end; max++)
8743 if (start[max] == '\r' || start[max] == '\n')
8744 break;
8745
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008746 UBOUND(max, trash.size - trash.len - 3);
8747 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8748 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008749 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008750}
8751
Willy Tarreau0937bc42009-12-22 15:03:09 +01008752/*
8753 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8754 * the required fields are properly allocated and that we only need to (re)init
8755 * them. This should be used before processing any new request.
8756 */
8757void http_init_txn(struct session *s)
8758{
8759 struct http_txn *txn = &s->txn;
8760 struct proxy *fe = s->fe;
8761
8762 txn->flags = 0;
8763 txn->status = -1;
8764
Willy Tarreauf64d1412010-10-07 20:06:11 +02008765 txn->cookie_first_date = 0;
8766 txn->cookie_last_date = 0;
8767
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008768 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008769 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008770 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008771 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008772 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008773 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008774 txn->req.chunk_len = 0LL;
8775 txn->req.body_len = 0LL;
8776 txn->rsp.chunk_len = 0LL;
8777 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008778 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8779 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008780 txn->req.chn = s->req;
8781 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008782
8783 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008784
8785 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8786 if (fe->options2 & PR_O2_REQBUG_OK)
8787 txn->req.err_pos = -1; /* let buggy requests pass */
8788
Willy Tarreau46023632010-01-07 22:51:47 +01008789 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008790 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8791
Willy Tarreau46023632010-01-07 22:51:47 +01008792 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008793 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8794
8795 if (txn->hdr_idx.v)
8796 hdr_idx_init(&txn->hdr_idx);
8797}
8798
8799/* to be used at the end of a transaction */
8800void http_end_txn(struct session *s)
8801{
8802 struct http_txn *txn = &s->txn;
8803
Willy Tarreau75195602014-03-11 15:48:55 +01008804 /* release any possible compression context */
8805 if (s->flags & SN_COMP_READY)
8806 s->comp_algo->end(&s->comp_ctx);
8807 s->comp_algo = NULL;
8808 s->flags &= ~SN_COMP_READY;
8809
Willy Tarreau0937bc42009-12-22 15:03:09 +01008810 /* these ones will have been dynamically allocated */
8811 pool_free2(pool2_requri, txn->uri);
8812 pool_free2(pool2_capture, txn->cli_cookie);
8813 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008814 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008815 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008816
William Lallemanda73203e2012-03-12 12:48:57 +01008817 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008818 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008819 txn->uri = NULL;
8820 txn->srv_cookie = NULL;
8821 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008822
8823 if (txn->req.cap) {
8824 struct cap_hdr *h;
8825 for (h = s->fe->req_cap; h; h = h->next)
8826 pool_free2(h->pool, txn->req.cap[h->index]);
8827 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8828 }
8829
8830 if (txn->rsp.cap) {
8831 struct cap_hdr *h;
8832 for (h = s->fe->rsp_cap; h; h = h->next)
8833 pool_free2(h->pool, txn->rsp.cap[h->index]);
8834 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8835 }
8836
Willy Tarreau0937bc42009-12-22 15:03:09 +01008837}
8838
8839/* to be used at the end of a transaction to prepare a new one */
8840void http_reset_txn(struct session *s)
8841{
8842 http_end_txn(s);
8843 http_init_txn(s);
8844
8845 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008847 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008848 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008849 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008850 /* re-init store persistence */
8851 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008852 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008853
Willy Tarreau0937bc42009-12-22 15:03:09 +01008854 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008855
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008856 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008857
Willy Tarreau739cfba2010-01-25 23:11:14 +01008858 /* We must trim any excess data from the response buffer, because we
8859 * may have blocked an invalid response from a server that we don't
8860 * want to accidentely forward once we disable the analysers, nor do
8861 * we want those data to come along with next response. A typical
8862 * example of such data would be from a buggy server responding to
8863 * a HEAD with some data, or sending more than the advertised
8864 * content-length.
8865 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008866 if (unlikely(s->rep->buf->i))
8867 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008868
Willy Tarreau0937bc42009-12-22 15:03:09 +01008869 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008870 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871
Willy Tarreaud04e8582010-05-31 12:31:35 +02008872 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008873 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008874
8875 s->req->rex = TICK_ETERNITY;
8876 s->req->wex = TICK_ETERNITY;
8877 s->req->analyse_exp = TICK_ETERNITY;
8878 s->rep->rex = TICK_ETERNITY;
8879 s->rep->wex = TICK_ETERNITY;
8880 s->rep->analyse_exp = TICK_ETERNITY;
8881}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008882
Sasha Pachev218f0642014-06-16 12:05:59 -06008883void free_http_res_rules(struct list *r)
8884{
8885 struct http_res_rule *tr, *pr;
8886
8887 list_for_each_entry_safe(pr, tr, r, list) {
8888 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008889 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008890 free(pr);
8891 }
8892}
8893
8894void free_http_req_rules(struct list *r)
8895{
Willy Tarreauff011f22011-01-06 17:51:27 +01008896 struct http_req_rule *tr, *pr;
8897
8898 list_for_each_entry_safe(pr, tr, r, list) {
8899 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008900 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008901 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008902
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008903 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008904 free(pr);
8905 }
8906}
8907
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008908/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008909struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8910{
8911 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008912 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008913 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008914 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008915
8916 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8917 if (!rule) {
8918 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008919 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008920 }
8921
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008922 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008923 rule->action = HTTP_REQ_ACT_ALLOW;
8924 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008925 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008926 rule->action = HTTP_REQ_ACT_DENY;
8927 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008928 } else if (!strcmp(args[0], "tarpit")) {
8929 rule->action = HTTP_REQ_ACT_TARPIT;
8930 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008931 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008932 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008933 cur_arg = 1;
8934
8935 while(*args[cur_arg]) {
8936 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008937 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008938 cur_arg+=2;
8939 continue;
8940 } else
8941 break;
8942 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008943 } else if (!strcmp(args[0], "set-nice")) {
8944 rule->action = HTTP_REQ_ACT_SET_NICE;
8945 cur_arg = 1;
8946
8947 if (!*args[cur_arg] ||
8948 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8949 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8950 file, linenum, args[0]);
8951 goto out_err;
8952 }
8953 rule->arg.nice = atoi(args[cur_arg]);
8954 if (rule->arg.nice < -1024)
8955 rule->arg.nice = -1024;
8956 else if (rule->arg.nice > 1024)
8957 rule->arg.nice = 1024;
8958 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008959 } else if (!strcmp(args[0], "set-tos")) {
8960#ifdef IP_TOS
8961 char *err;
8962 rule->action = HTTP_REQ_ACT_SET_TOS;
8963 cur_arg = 1;
8964
8965 if (!*args[cur_arg] ||
8966 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8967 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8968 file, linenum, args[0]);
8969 goto out_err;
8970 }
8971
8972 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8973 if (err && *err != '\0') {
8974 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8975 file, linenum, err, args[0]);
8976 goto out_err;
8977 }
8978 cur_arg++;
8979#else
8980 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8981 goto out_err;
8982#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008983 } else if (!strcmp(args[0], "set-mark")) {
8984#ifdef SO_MARK
8985 char *err;
8986 rule->action = HTTP_REQ_ACT_SET_MARK;
8987 cur_arg = 1;
8988
8989 if (!*args[cur_arg] ||
8990 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8991 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8992 file, linenum, args[0]);
8993 goto out_err;
8994 }
8995
8996 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8997 if (err && *err != '\0') {
8998 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8999 file, linenum, err, args[0]);
9000 goto out_err;
9001 }
9002 cur_arg++;
9003 global.last_checks |= LSTCHK_NETADM;
9004#else
9005 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9006 goto out_err;
9007#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009008 } else if (!strcmp(args[0], "set-log-level")) {
9009 rule->action = HTTP_REQ_ACT_SET_LOGL;
9010 cur_arg = 1;
9011
9012 if (!*args[cur_arg] ||
9013 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9014 bad_log_level:
9015 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9016 file, linenum, args[0]);
9017 goto out_err;
9018 }
9019 if (strcmp(args[cur_arg], "silent") == 0)
9020 rule->arg.loglevel = -1;
9021 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9022 goto bad_log_level;
9023 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009024 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9025 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9026 cur_arg = 1;
9027
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009028 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9029 (*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 +01009030 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9031 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009032 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009033 }
9034
9035 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9036 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9037 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009038
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009039 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009040 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009041 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9042 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009043 free(proxy->conf.lfs_file);
9044 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9045 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009046 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009047 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9048 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009049 cur_arg = 1;
9050
9051 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009052 (*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 -06009053 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9054 file, linenum, args[0]);
9055 goto out_err;
9056 }
9057
9058 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9059 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9060 LIST_INIT(&rule->arg.hdr_add.fmt);
9061
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009062 error = NULL;
9063 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9064 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9065 args[cur_arg + 1], error);
9066 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009067 goto out_err;
9068 }
9069
9070 proxy->conf.args.ctx = ARGC_HRQ;
9071 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9072 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9073 file, linenum);
9074
9075 free(proxy->conf.lfs_file);
9076 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9077 proxy->conf.lfs_line = proxy->conf.args.line;
9078 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009079 } else if (strcmp(args[0], "del-header") == 0) {
9080 rule->action = HTTP_REQ_ACT_DEL_HDR;
9081 cur_arg = 1;
9082
9083 if (!*args[cur_arg] ||
9084 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9085 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9086 file, linenum, args[0]);
9087 goto out_err;
9088 }
9089
9090 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9091 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9092
9093 proxy->conf.args.ctx = ARGC_HRQ;
9094 free(proxy->conf.lfs_file);
9095 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9096 proxy->conf.lfs_line = proxy->conf.args.line;
9097 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009098 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9099 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009100 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009101 struct sample_expr *expr;
9102 unsigned int where;
9103 char *err = NULL;
9104
9105 cur_arg = 1;
9106 proxy->conf.args.ctx = ARGC_TRK;
9107
9108 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9109 if (!expr) {
9110 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9111 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9112 free(err);
9113 goto out_err;
9114 }
9115
9116 where = 0;
9117 if (proxy->cap & PR_CAP_FE)
9118 where |= SMP_VAL_FE_HRQ_HDR;
9119 if (proxy->cap & PR_CAP_BE)
9120 where |= SMP_VAL_BE_HRQ_HDR;
9121
9122 if (!(expr->fetch->val & where)) {
9123 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9124 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9125 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9126 args[cur_arg-1], sample_src_names(expr->fetch->use));
9127 free(expr);
9128 goto out_err;
9129 }
9130
9131 if (strcmp(args[cur_arg], "table") == 0) {
9132 cur_arg++;
9133 if (!args[cur_arg]) {
9134 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9135 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9136 free(expr);
9137 goto out_err;
9138 }
9139 /* we copy the table name for now, it will be resolved later */
9140 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9141 cur_arg++;
9142 }
9143 rule->act_prm.trk_ctr.expr = expr;
9144 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009145 } else if (strcmp(args[0], "redirect") == 0) {
9146 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009147 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009148
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009149 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009150 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9151 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9152 goto out_err;
9153 }
9154
9155 /* this redirect rule might already contain a parsed condition which
9156 * we'll pass to the http-request rule.
9157 */
9158 rule->action = HTTP_REQ_ACT_REDIR;
9159 rule->arg.redir = redir;
9160 rule->cond = redir->cond;
9161 redir->cond = NULL;
9162 cur_arg = 2;
9163 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009164 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9165 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9166 rule->action = HTTP_REQ_ACT_ADD_ACL;
9167 /*
9168 * '+ 8' for 'add-acl('
9169 * '- 9' for 'add-acl(' + trailing ')'
9170 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009171 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009172
9173 cur_arg = 1;
9174
9175 if (!*args[cur_arg] ||
9176 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9177 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9178 file, linenum, args[0]);
9179 goto out_err;
9180 }
9181
9182 LIST_INIT(&rule->arg.map.key);
9183 proxy->conf.args.ctx = ARGC_HRQ;
9184 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9185 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9186 file, linenum);
9187 free(proxy->conf.lfs_file);
9188 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9189 proxy->conf.lfs_line = proxy->conf.args.line;
9190 cur_arg += 1;
9191 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9192 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9193 rule->action = HTTP_REQ_ACT_DEL_ACL;
9194 /*
9195 * '+ 8' for 'del-acl('
9196 * '- 9' for 'del-acl(' + trailing ')'
9197 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009198 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009199
9200 cur_arg = 1;
9201
9202 if (!*args[cur_arg] ||
9203 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9204 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9205 file, linenum, args[0]);
9206 goto out_err;
9207 }
9208
9209 LIST_INIT(&rule->arg.map.key);
9210 proxy->conf.args.ctx = ARGC_HRQ;
9211 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9212 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9213 file, linenum);
9214 free(proxy->conf.lfs_file);
9215 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9216 proxy->conf.lfs_line = proxy->conf.args.line;
9217 cur_arg += 1;
9218 } else if (strncmp(args[0], "del-map", 7) == 0) {
9219 /* http-request del-map(<reference (map name)>) <key pattern> */
9220 rule->action = HTTP_REQ_ACT_DEL_MAP;
9221 /*
9222 * '+ 8' for 'del-map('
9223 * '- 9' for 'del-map(' + trailing ')'
9224 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009225 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009226
9227 cur_arg = 1;
9228
9229 if (!*args[cur_arg] ||
9230 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9231 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9232 file, linenum, args[0]);
9233 goto out_err;
9234 }
9235
9236 LIST_INIT(&rule->arg.map.key);
9237 proxy->conf.args.ctx = ARGC_HRQ;
9238 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9239 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9240 file, linenum);
9241 free(proxy->conf.lfs_file);
9242 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9243 proxy->conf.lfs_line = proxy->conf.args.line;
9244 cur_arg += 1;
9245 } else if (strncmp(args[0], "set-map", 7) == 0) {
9246 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9247 rule->action = HTTP_REQ_ACT_SET_MAP;
9248 /*
9249 * '+ 8' for 'set-map('
9250 * '- 9' for 'set-map(' + trailing ')'
9251 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009252 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009253
9254 cur_arg = 1;
9255
9256 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9257 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9258 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9259 file, linenum, args[0]);
9260 goto out_err;
9261 }
9262
9263 LIST_INIT(&rule->arg.map.key);
9264 LIST_INIT(&rule->arg.map.value);
9265 proxy->conf.args.ctx = ARGC_HRQ;
9266
9267 /* key pattern */
9268 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9269 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9270 file, linenum);
9271
9272 /* value pattern */
9273 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9274 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9275 file, linenum);
9276 free(proxy->conf.lfs_file);
9277 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9278 proxy->conf.lfs_line = proxy->conf.args.line;
9279
9280 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009281 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9282 char *errmsg = NULL;
9283 cur_arg = 1;
9284 /* try in the module list */
9285 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9286 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9287 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9288 free(errmsg);
9289 goto out_err;
9290 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009291 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009292 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 +01009293 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009294 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009295 }
9296
9297 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9298 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009299 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009300
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009301 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9302 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9303 file, linenum, args[0], errmsg);
9304 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009305 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009306 }
9307 rule->cond = cond;
9308 }
9309 else if (*args[cur_arg]) {
9310 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9311 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9312 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009313 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009314 }
9315
9316 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009317 out_err:
9318 free(rule);
9319 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009320}
9321
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009322/* parse an "http-respose" rule */
9323struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9324{
9325 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009326 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009327 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009328 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009329
9330 rule = calloc(1, sizeof(*rule));
9331 if (!rule) {
9332 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9333 goto out_err;
9334 }
9335
9336 if (!strcmp(args[0], "allow")) {
9337 rule->action = HTTP_RES_ACT_ALLOW;
9338 cur_arg = 1;
9339 } else if (!strcmp(args[0], "deny")) {
9340 rule->action = HTTP_RES_ACT_DENY;
9341 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009342 } else if (!strcmp(args[0], "set-nice")) {
9343 rule->action = HTTP_RES_ACT_SET_NICE;
9344 cur_arg = 1;
9345
9346 if (!*args[cur_arg] ||
9347 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9348 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9349 file, linenum, args[0]);
9350 goto out_err;
9351 }
9352 rule->arg.nice = atoi(args[cur_arg]);
9353 if (rule->arg.nice < -1024)
9354 rule->arg.nice = -1024;
9355 else if (rule->arg.nice > 1024)
9356 rule->arg.nice = 1024;
9357 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009358 } else if (!strcmp(args[0], "set-tos")) {
9359#ifdef IP_TOS
9360 char *err;
9361 rule->action = HTTP_RES_ACT_SET_TOS;
9362 cur_arg = 1;
9363
9364 if (!*args[cur_arg] ||
9365 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9366 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9367 file, linenum, args[0]);
9368 goto out_err;
9369 }
9370
9371 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9372 if (err && *err != '\0') {
9373 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9374 file, linenum, err, args[0]);
9375 goto out_err;
9376 }
9377 cur_arg++;
9378#else
9379 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9380 goto out_err;
9381#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009382 } else if (!strcmp(args[0], "set-mark")) {
9383#ifdef SO_MARK
9384 char *err;
9385 rule->action = HTTP_RES_ACT_SET_MARK;
9386 cur_arg = 1;
9387
9388 if (!*args[cur_arg] ||
9389 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9390 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9391 file, linenum, args[0]);
9392 goto out_err;
9393 }
9394
9395 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9396 if (err && *err != '\0') {
9397 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9398 file, linenum, err, args[0]);
9399 goto out_err;
9400 }
9401 cur_arg++;
9402 global.last_checks |= LSTCHK_NETADM;
9403#else
9404 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9405 goto out_err;
9406#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009407 } else if (!strcmp(args[0], "set-log-level")) {
9408 rule->action = HTTP_RES_ACT_SET_LOGL;
9409 cur_arg = 1;
9410
9411 if (!*args[cur_arg] ||
9412 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9413 bad_log_level:
9414 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9415 file, linenum, args[0]);
9416 goto out_err;
9417 }
9418 if (strcmp(args[cur_arg], "silent") == 0)
9419 rule->arg.loglevel = -1;
9420 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9421 goto bad_log_level;
9422 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009423 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9424 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9425 cur_arg = 1;
9426
9427 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9428 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9429 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9430 file, linenum, args[0]);
9431 goto out_err;
9432 }
9433
9434 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9435 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9436 LIST_INIT(&rule->arg.hdr_add.fmt);
9437
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009438 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009439 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009440 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9441 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009442 free(proxy->conf.lfs_file);
9443 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9444 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009445 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009446 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009447 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009448 cur_arg = 1;
9449
9450 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009451 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9452 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009453 file, linenum, args[0]);
9454 goto out_err;
9455 }
9456
9457 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9458 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9459 LIST_INIT(&rule->arg.hdr_add.fmt);
9460
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009461 error = NULL;
9462 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9463 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9464 args[cur_arg + 1], error);
9465 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009466 goto out_err;
9467 }
9468
9469 proxy->conf.args.ctx = ARGC_HRQ;
9470 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9471 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9472 file, linenum);
9473
9474 free(proxy->conf.lfs_file);
9475 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9476 proxy->conf.lfs_line = proxy->conf.args.line;
9477 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009478 } else if (strcmp(args[0], "del-header") == 0) {
9479 rule->action = HTTP_RES_ACT_DEL_HDR;
9480 cur_arg = 1;
9481
9482 if (!*args[cur_arg] ||
9483 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9484 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9485 file, linenum, args[0]);
9486 goto out_err;
9487 }
9488
9489 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9490 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9491
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009492 proxy->conf.args.ctx = ARGC_HRS;
9493 free(proxy->conf.lfs_file);
9494 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9495 proxy->conf.lfs_line = proxy->conf.args.line;
9496 cur_arg += 1;
9497 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9498 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9499 rule->action = HTTP_RES_ACT_ADD_ACL;
9500 /*
9501 * '+ 8' for 'add-acl('
9502 * '- 9' for 'add-acl(' + trailing ')'
9503 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009504 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009505
9506 cur_arg = 1;
9507
9508 if (!*args[cur_arg] ||
9509 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9510 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9511 file, linenum, args[0]);
9512 goto out_err;
9513 }
9514
9515 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009516 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009517 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9518 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9519 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009520 free(proxy->conf.lfs_file);
9521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9522 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009523
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009524 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009525 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9526 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9527 rule->action = HTTP_RES_ACT_DEL_ACL;
9528 /*
9529 * '+ 8' for 'del-acl('
9530 * '- 9' for 'del-acl(' + trailing ')'
9531 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009532 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009533
9534 cur_arg = 1;
9535
9536 if (!*args[cur_arg] ||
9537 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9538 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9539 file, linenum, args[0]);
9540 goto out_err;
9541 }
9542
9543 LIST_INIT(&rule->arg.map.key);
9544 proxy->conf.args.ctx = ARGC_HRS;
9545 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9546 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9547 file, linenum);
9548 free(proxy->conf.lfs_file);
9549 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9550 proxy->conf.lfs_line = proxy->conf.args.line;
9551 cur_arg += 1;
9552 } else if (strncmp(args[0], "del-map", 7) == 0) {
9553 /* http-response del-map(<reference (map name)>) <key pattern> */
9554 rule->action = HTTP_RES_ACT_DEL_MAP;
9555 /*
9556 * '+ 8' for 'del-map('
9557 * '- 9' for 'del-map(' + trailing ')'
9558 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009559 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009560
9561 cur_arg = 1;
9562
9563 if (!*args[cur_arg] ||
9564 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9565 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9566 file, linenum, args[0]);
9567 goto out_err;
9568 }
9569
9570 LIST_INIT(&rule->arg.map.key);
9571 proxy->conf.args.ctx = ARGC_HRS;
9572 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9573 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9574 file, linenum);
9575 free(proxy->conf.lfs_file);
9576 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9577 proxy->conf.lfs_line = proxy->conf.args.line;
9578 cur_arg += 1;
9579 } else if (strncmp(args[0], "set-map", 7) == 0) {
9580 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9581 rule->action = HTTP_RES_ACT_SET_MAP;
9582 /*
9583 * '+ 8' for 'set-map('
9584 * '- 9' for 'set-map(' + trailing ')'
9585 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009586 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009587
9588 cur_arg = 1;
9589
9590 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9591 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9592 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9593 file, linenum, args[0]);
9594 goto out_err;
9595 }
9596
9597 LIST_INIT(&rule->arg.map.key);
9598 LIST_INIT(&rule->arg.map.value);
9599
9600 proxy->conf.args.ctx = ARGC_HRS;
9601
9602 /* key pattern */
9603 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9604 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9605 file, linenum);
9606
9607 /* value pattern */
9608 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9609 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9610 file, linenum);
9611
9612 free(proxy->conf.lfs_file);
9613 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9614 proxy->conf.lfs_line = proxy->conf.args.line;
9615
9616 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009617 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9618 char *errmsg = NULL;
9619 cur_arg = 1;
9620 /* try in the module list */
9621 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9622 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9623 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9624 free(errmsg);
9625 goto out_err;
9626 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009627 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009628 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 +02009629 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9630 goto out_err;
9631 }
9632
9633 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9634 struct acl_cond *cond;
9635 char *errmsg = NULL;
9636
9637 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9638 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9639 file, linenum, args[0], errmsg);
9640 free(errmsg);
9641 goto out_err;
9642 }
9643 rule->cond = cond;
9644 }
9645 else if (*args[cur_arg]) {
9646 Alert("parsing [%s:%d]: 'http-response %s' expects"
9647 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9648 file, linenum, args[0], args[cur_arg]);
9649 goto out_err;
9650 }
9651
9652 return rule;
9653 out_err:
9654 free(rule);
9655 return NULL;
9656}
9657
Willy Tarreau4baae242012-12-27 12:00:31 +01009658/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009659 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9660 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009661 */
9662struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009663 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009664{
9665 struct redirect_rule *rule;
9666 int cur_arg;
9667 int type = REDIRECT_TYPE_NONE;
9668 int code = 302;
9669 const char *destination = NULL;
9670 const char *cookie = NULL;
9671 int cookie_set = 0;
9672 unsigned int flags = REDIRECT_FLAG_NONE;
9673 struct acl_cond *cond = NULL;
9674
9675 cur_arg = 0;
9676 while (*(args[cur_arg])) {
9677 if (strcmp(args[cur_arg], "location") == 0) {
9678 if (!*args[cur_arg + 1])
9679 goto missing_arg;
9680
9681 type = REDIRECT_TYPE_LOCATION;
9682 cur_arg++;
9683 destination = args[cur_arg];
9684 }
9685 else if (strcmp(args[cur_arg], "prefix") == 0) {
9686 if (!*args[cur_arg + 1])
9687 goto missing_arg;
9688
9689 type = REDIRECT_TYPE_PREFIX;
9690 cur_arg++;
9691 destination = args[cur_arg];
9692 }
9693 else if (strcmp(args[cur_arg], "scheme") == 0) {
9694 if (!*args[cur_arg + 1])
9695 goto missing_arg;
9696
9697 type = REDIRECT_TYPE_SCHEME;
9698 cur_arg++;
9699 destination = args[cur_arg];
9700 }
9701 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9702 if (!*args[cur_arg + 1])
9703 goto missing_arg;
9704
9705 cur_arg++;
9706 cookie = args[cur_arg];
9707 cookie_set = 1;
9708 }
9709 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9710 if (!*args[cur_arg + 1])
9711 goto missing_arg;
9712
9713 cur_arg++;
9714 cookie = args[cur_arg];
9715 cookie_set = 0;
9716 }
9717 else if (strcmp(args[cur_arg], "code") == 0) {
9718 if (!*args[cur_arg + 1])
9719 goto missing_arg;
9720
9721 cur_arg++;
9722 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009723 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009724 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009725 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009726 args[cur_arg - 1], args[cur_arg]);
9727 return NULL;
9728 }
9729 }
9730 else if (!strcmp(args[cur_arg],"drop-query")) {
9731 flags |= REDIRECT_FLAG_DROP_QS;
9732 }
9733 else if (!strcmp(args[cur_arg],"append-slash")) {
9734 flags |= REDIRECT_FLAG_APPEND_SLASH;
9735 }
9736 else if (strcmp(args[cur_arg], "if") == 0 ||
9737 strcmp(args[cur_arg], "unless") == 0) {
9738 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9739 if (!cond) {
9740 memprintf(errmsg, "error in condition: %s", *errmsg);
9741 return NULL;
9742 }
9743 break;
9744 }
9745 else {
9746 memprintf(errmsg,
9747 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9748 args[cur_arg]);
9749 return NULL;
9750 }
9751 cur_arg++;
9752 }
9753
9754 if (type == REDIRECT_TYPE_NONE) {
9755 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9756 return NULL;
9757 }
9758
9759 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9760 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009761 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009762
9763 if (!use_fmt) {
9764 /* old-style static redirect rule */
9765 rule->rdr_str = strdup(destination);
9766 rule->rdr_len = strlen(destination);
9767 }
9768 else {
9769 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009770
9771 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9772 * if prefix == "/", we don't want to add anything, otherwise it
9773 * makes it hard for the user to configure a self-redirection.
9774 */
Godbachd9722032014-12-18 15:44:58 +08009775 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009776 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009777 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009778 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9779 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009780 free(curproxy->conf.lfs_file);
9781 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9782 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009783 }
9784 }
9785
Willy Tarreau4baae242012-12-27 12:00:31 +01009786 if (cookie) {
9787 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9788 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9789 */
9790 rule->cookie_len = strlen(cookie);
9791 if (cookie_set) {
9792 rule->cookie_str = malloc(rule->cookie_len + 10);
9793 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9794 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9795 rule->cookie_len += 9;
9796 } else {
9797 rule->cookie_str = malloc(rule->cookie_len + 21);
9798 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9799 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9800 rule->cookie_len += 20;
9801 }
9802 }
9803 rule->type = type;
9804 rule->code = code;
9805 rule->flags = flags;
9806 LIST_INIT(&rule->list);
9807 return rule;
9808
9809 missing_arg:
9810 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9811 return NULL;
9812}
9813
Willy Tarreau8797c062007-05-07 00:55:35 +02009814/************************************************************************/
9815/* The code below is dedicated to ACL parsing and matching */
9816/************************************************************************/
9817
9818
Willy Tarreau14174bc2012-04-16 14:34:04 +02009819/* This function ensures that the prerequisites for an L7 fetch are ready,
9820 * which means that a request or response is ready. If some data is missing,
9821 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009822 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9823 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009824 *
9825 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009826 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9827 * decide whether or not an HTTP message is present ;
9828 * 0 if the requested data cannot be fetched or if it is certain that
9829 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009830 * 1 if an HTTP message is ready
9831 */
9832static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009833smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009834 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009835{
9836 struct http_txn *txn = l7;
9837 struct http_msg *msg = &txn->req;
9838
9839 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9840 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9841 */
9842
9843 if (unlikely(!s || !txn))
9844 return 0;
9845
9846 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009847 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009848
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009849 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009850 if (unlikely(!s->req))
9851 return 0;
9852
Willy Tarreauaae75e32013-03-29 12:31:49 +01009853 /* If the buffer does not leave enough free space at the end,
9854 * we must first realign it.
9855 */
9856 if (s->req->buf->p > s->req->buf->data &&
9857 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9858 buffer_slow_realign(s->req->buf);
9859
Willy Tarreau14174bc2012-04-16 14:34:04 +02009860 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009861 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009862 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009863
9864 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009865 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009866 http_msg_analyzer(msg, &txn->hdr_idx);
9867
9868 /* Still no valid request ? */
9869 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009870 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009871 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009872 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009873 }
9874 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009875 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009876 return 0;
9877 }
9878
9879 /* OK we just got a valid HTTP request. We have some minor
9880 * preparation to perform so that further checks can rely
9881 * on HTTP tests.
9882 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009883
9884 /* If the request was parsed but was too large, we must absolutely
9885 * return an error so that it is not processed. At the moment this
9886 * cannot happen, but if the parsers are to change in the future,
9887 * we want this check to be maintained.
9888 */
9889 if (unlikely(s->req->buf->i + s->req->buf->p >
9890 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9891 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009892 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009893 return 1;
9894 }
9895
Willy Tarreau9b28e032012-10-12 23:49:43 +02009896 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009897 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9898 s->flags |= SN_REDIRECTABLE;
9899
Willy Tarreau506d0502013-07-06 13:29:24 +02009900 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9901 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009902 }
9903
Willy Tarreau506d0502013-07-06 13:29:24 +02009904 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009905 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009906 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009907
9908 /* otherwise everything's ready for the request */
9909 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009910 else {
9911 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009912 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9913 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009914 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009915 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009916 }
9917
9918 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009919 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009920 return 1;
9921}
Willy Tarreau8797c062007-05-07 00:55:35 +02009922
Willy Tarreau6c616e02014-06-25 16:56:41 +02009923/* Note: these functinos *do* modify the sample. Even in case of success, at
9924 * least the type and uint value are modified.
9925 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009926#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009927 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 +02009928
Willy Tarreau24e32d82012-04-23 23:55:44 +02009929#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009930 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 +02009931
Willy Tarreau8797c062007-05-07 00:55:35 +02009932
9933/* 1. Check on METHOD
9934 * We use the pre-parsed method if it is known, and store its number as an
9935 * integer. If it is unknown, we use the pointer and the length.
9936 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009937static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009938{
9939 int len, meth;
9940
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009941 len = strlen(text);
9942 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009943
9944 pattern->val.i = meth;
9945 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009946 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009947 pattern->len = len;
9948 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009949 else {
9950 pattern->ptr.str = NULL;
9951 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009952 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009953 return 1;
9954}
9955
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009956/* This function fetches the method of current HTTP request and stores
9957 * it in the global pattern struct as a chunk. There are two possibilities :
9958 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9959 * in <len> and <ptr> is NULL ;
9960 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9961 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009962 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009963 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009964static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009965smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01009966 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009967{
9968 int meth;
9969 struct http_txn *txn = l7;
9970
Willy Tarreau24e32d82012-04-23 23:55:44 +02009971 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009972
Willy Tarreau8797c062007-05-07 00:55:35 +02009973 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009974 smp->type = SMP_T_METH;
9975 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009976 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009977 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9978 /* ensure the indexes are not affected */
9979 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009980 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009981 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9982 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009983 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009984 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009985 return 1;
9986}
9987
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009988/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009989static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009990{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009991 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009992 struct pattern_list *lst;
9993 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009994
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009995 list_for_each_entry(lst, &expr->patterns, list) {
9996 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009997
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009998 /* well-known method */
9999 if (pattern->val.i != HTTP_METH_OTHER) {
10000 if (smp->data.meth.meth == pattern->val.i)
10001 return pattern;
10002 else
10003 continue;
10004 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010005
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010006 /* Other method, we must compare the strings */
10007 if (pattern->len != smp->data.meth.str.len)
10008 continue;
10009
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010010 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010011 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10012 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010013 return pattern;
10014 }
10015 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010016}
10017
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010018static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010019smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010020 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010021{
10022 struct http_txn *txn = l7;
10023 char *ptr;
10024 int len;
10025
Willy Tarreauc0239e02012-04-16 14:42:55 +020010026 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010027
Willy Tarreau8797c062007-05-07 00:55:35 +020010028 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010029 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010030
10031 while ((len-- > 0) && (*ptr++ != '/'));
10032 if (len <= 0)
10033 return 0;
10034
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010035 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010036 smp->data.str.str = ptr;
10037 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010039 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010040 return 1;
10041}
10042
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010043static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010044smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010045 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010046{
10047 struct http_txn *txn = l7;
10048 char *ptr;
10049 int len;
10050
Willy Tarreauc0239e02012-04-16 14:42:55 +020010051 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010052
Willy Tarreauf26b2522012-12-14 08:33:14 +010010053 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10054 return 0;
10055
Willy Tarreau8797c062007-05-07 00:55:35 +020010056 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010057 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010058
10059 while ((len-- > 0) && (*ptr++ != '/'));
10060 if (len <= 0)
10061 return 0;
10062
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010063 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010064 smp->data.str.str = ptr;
10065 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010066
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010067 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010068 return 1;
10069}
10070
10071/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010072static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010073smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010074 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010075{
10076 struct http_txn *txn = l7;
10077 char *ptr;
10078 int len;
10079
Willy Tarreauc0239e02012-04-16 14:42:55 +020010080 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010081
Willy Tarreauf26b2522012-12-14 08:33:14 +010010082 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10083 return 0;
10084
Willy Tarreau8797c062007-05-07 00:55:35 +020010085 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010086 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010087
Willy Tarreauf853c462012-04-23 18:53:56 +020010088 smp->type = SMP_T_UINT;
10089 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010090 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010091 return 1;
10092}
10093
10094/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010095static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010096smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010097 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010098{
10099 struct http_txn *txn = l7;
10100
Willy Tarreauc0239e02012-04-16 14:42:55 +020010101 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010102
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010103 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010104 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010105 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010106 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010107 return 1;
10108}
10109
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010110static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010111smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010112 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010113{
10114 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010115 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010116
Willy Tarreauc0239e02012-04-16 14:42:55 +020010117 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010118
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010119 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 +020010120 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010121 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010122
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010123 smp->type = SMP_T_IPV4;
10124 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010125 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010126 return 1;
10127}
10128
10129static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010130smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010131 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010132{
10133 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010134 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010135
Willy Tarreauc0239e02012-04-16 14:42:55 +020010136 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010137
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010138 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 +020010139 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10140 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010141
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010142 smp->type = SMP_T_UINT;
10143 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010144 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010145 return 1;
10146}
10147
Willy Tarreau185b5c42012-04-26 15:11:51 +020010148/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10149 * Accepts an optional argument of type string containing the header field name,
10150 * and an optional argument of type signed or unsigned integer to request an
10151 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010152 * headers are considered from the first one. It does not stop on commas and
10153 * returns full lines instead (useful for User-Agent or Date for example).
10154 */
10155static int
10156smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010157 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010158{
10159 struct http_txn *txn = l7;
10160 struct hdr_idx *idx = &txn->hdr_idx;
10161 struct hdr_ctx *ctx = smp->ctx.a[0];
10162 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10163 int occ = 0;
10164 const char *name_str = NULL;
10165 int name_len = 0;
10166
10167 if (!ctx) {
10168 /* first call */
10169 ctx = &static_hdr_ctx;
10170 ctx->idx = 0;
10171 smp->ctx.a[0] = ctx;
10172 }
10173
10174 if (args) {
10175 if (args[0].type != ARGT_STR)
10176 return 0;
10177 name_str = args[0].data.str.str;
10178 name_len = args[0].data.str.len;
10179
10180 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10181 occ = args[1].data.uint;
10182 }
10183
10184 CHECK_HTTP_MESSAGE_FIRST();
10185
10186 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10187 /* search for header from the beginning */
10188 ctx->idx = 0;
10189
10190 if (!occ && !(opt & SMP_OPT_ITERATE))
10191 /* no explicit occurrence and single fetch => last header by default */
10192 occ = -1;
10193
10194 if (!occ)
10195 /* prepare to report multiple occurrences for ACL fetches */
10196 smp->flags |= SMP_F_NOT_LAST;
10197
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010198 smp->type = SMP_T_STR;
10199 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010200 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10201 return 1;
10202
10203 smp->flags &= ~SMP_F_NOT_LAST;
10204 return 0;
10205}
10206
10207/* 6. Check on HTTP header count. The number of occurrences is returned.
10208 * Accepts exactly 1 argument of type string. It does not stop on commas and
10209 * returns full lines instead (useful for User-Agent or Date for example).
10210 */
10211static int
10212smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010213 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010214{
10215 struct http_txn *txn = l7;
10216 struct hdr_idx *idx = &txn->hdr_idx;
10217 struct hdr_ctx ctx;
10218 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10219 int cnt;
10220
10221 if (!args || args->type != ARGT_STR)
10222 return 0;
10223
10224 CHECK_HTTP_MESSAGE_FIRST();
10225
10226 ctx.idx = 0;
10227 cnt = 0;
10228 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10229 cnt++;
10230
10231 smp->type = SMP_T_UINT;
10232 smp->data.uint = cnt;
10233 smp->flags = SMP_F_VOL_HDR;
10234 return 1;
10235}
10236
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010237static int
10238smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010239 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010240{
10241 struct http_txn *txn = l7;
10242 struct hdr_idx *idx = &txn->hdr_idx;
10243 struct hdr_ctx ctx;
10244 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10245 struct chunk *temp;
10246 char del = ',';
10247
10248 if (args && args->type == ARGT_STR)
10249 del = *args[0].data.str.str;
10250
10251 CHECK_HTTP_MESSAGE_FIRST();
10252
10253 temp = get_trash_chunk();
10254
10255 ctx.idx = 0;
10256 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10257 if (temp->len)
10258 temp->str[temp->len++] = del;
10259 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10260 temp->len += ctx.del;
10261 }
10262
10263 smp->type = SMP_T_STR;
10264 smp->data.str.str = temp->str;
10265 smp->data.str.len = temp->len;
10266 smp->flags = SMP_F_VOL_HDR;
10267 return 1;
10268}
10269
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010270/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10271 * Accepts an optional argument of type string containing the header field name,
10272 * and an optional argument of type signed or unsigned integer to request an
10273 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010274 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010275 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010276static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010277smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010278 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010279{
10280 struct http_txn *txn = l7;
10281 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010282 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010283 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010284 int occ = 0;
10285 const char *name_str = NULL;
10286 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010287
Willy Tarreaua890d072013-04-02 12:01:06 +020010288 if (!ctx) {
10289 /* first call */
10290 ctx = &static_hdr_ctx;
10291 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010292 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010293 }
10294
Willy Tarreau185b5c42012-04-26 15:11:51 +020010295 if (args) {
10296 if (args[0].type != ARGT_STR)
10297 return 0;
10298 name_str = args[0].data.str.str;
10299 name_len = args[0].data.str.len;
10300
10301 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10302 occ = args[1].data.uint;
10303 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010304
Willy Tarreaue333ec92012-04-16 16:26:40 +020010305 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010306
Willy Tarreau185b5c42012-04-26 15:11:51 +020010307 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010308 /* search for header from the beginning */
10309 ctx->idx = 0;
10310
Willy Tarreau185b5c42012-04-26 15:11:51 +020010311 if (!occ && !(opt & SMP_OPT_ITERATE))
10312 /* no explicit occurrence and single fetch => last header by default */
10313 occ = -1;
10314
10315 if (!occ)
10316 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010317 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010318
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010319 smp->type = SMP_T_STR;
10320 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010321 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 +020010322 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010323
Willy Tarreau37406352012-04-23 16:16:37 +020010324 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010325 return 0;
10326}
10327
Willy Tarreauc11416f2007-06-17 16:58:38 +020010328/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010329 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010330 */
10331static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010332smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010333 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010334{
10335 struct http_txn *txn = l7;
10336 struct hdr_idx *idx = &txn->hdr_idx;
10337 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010338 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010339 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010340
Willy Tarreau24e32d82012-04-23 23:55:44 +020010341 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010342 return 0;
10343
Willy Tarreaue333ec92012-04-16 16:26:40 +020010344 CHECK_HTTP_MESSAGE_FIRST();
10345
Willy Tarreau33a7e692007-06-10 19:45:56 +020010346 ctx.idx = 0;
10347 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010348 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 +020010349 cnt++;
10350
Willy Tarreauf853c462012-04-23 18:53:56 +020010351 smp->type = SMP_T_UINT;
10352 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010353 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010354 return 1;
10355}
10356
Willy Tarreau185b5c42012-04-26 15:11:51 +020010357/* Fetch an HTTP header's integer value. The integer value is returned. It
10358 * takes a mandatory argument of type string and an optional one of type int
10359 * to designate a specific occurrence. It returns an unsigned integer, which
10360 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010361 */
10362static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010363smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010364 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010365{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010366 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010367
Willy Tarreauf853c462012-04-23 18:53:56 +020010368 if (ret > 0) {
10369 smp->type = SMP_T_UINT;
10370 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10371 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010372
Willy Tarreaud53e2422012-04-16 17:21:11 +020010373 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010374}
10375
Cyril Bonté69fa9922012-10-25 00:01:06 +020010376/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10377 * and an optional one of type int to designate a specific occurrence.
10378 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010379 */
10380static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010381smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010382 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010383{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010384 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010385
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010386 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010387 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10388 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010389 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010390 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010391 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010392 if (smp->data.str.len < temp->size - 1) {
10393 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10394 temp->str[smp->data.str.len] = '\0';
10395 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10396 smp->type = SMP_T_IPV6;
10397 break;
10398 }
10399 }
10400 }
10401
Willy Tarreaud53e2422012-04-16 17:21:11 +020010402 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010403 if (!(smp->flags & SMP_F_NOT_LAST))
10404 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010405 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010406 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010407}
10408
Willy Tarreau737b0c12007-06-10 21:28:46 +020010409/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10410 * the first '/' after the possible hostname, and ends before the possible '?'.
10411 */
10412static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010413smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010414 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010415{
10416 struct http_txn *txn = l7;
10417 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010418
Willy Tarreauc0239e02012-04-16 14:42:55 +020010419 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010420
Willy Tarreau9b28e032012-10-12 23:49:43 +020010421 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010422 ptr = http_get_path(txn);
10423 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010424 return 0;
10425
10426 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010427 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010428 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010429
10430 while (ptr < end && *ptr != '?')
10431 ptr++;
10432
Willy Tarreauf853c462012-04-23 18:53:56 +020010433 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010434 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010435 return 1;
10436}
10437
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010438/* This produces a concatenation of the first occurrence of the Host header
10439 * followed by the path component if it begins with a slash ('/'). This means
10440 * that '*' will not be added, resulting in exactly the first Host entry.
10441 * If no Host header is found, then the path is returned as-is. The returned
10442 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010443 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010444 */
10445static int
10446smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010447 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010448{
10449 struct http_txn *txn = l7;
10450 char *ptr, *end, *beg;
10451 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010452 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010453
10454 CHECK_HTTP_MESSAGE_FIRST();
10455
10456 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010457 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010458 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010459
10460 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010461 temp = get_trash_chunk();
10462 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010463 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010464 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010465 smp->data.str.len = ctx.vlen;
10466
10467 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010468 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010469 beg = http_get_path(txn);
10470 if (!beg)
10471 beg = end;
10472
10473 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10474
10475 if (beg < ptr && *beg == '/') {
10476 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10477 smp->data.str.len += ptr - beg;
10478 }
10479
10480 smp->flags = SMP_F_VOL_1ST;
10481 return 1;
10482}
10483
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010484/* This produces a 32-bit hash of the concatenation of the first occurrence of
10485 * the Host header followed by the path component if it begins with a slash ('/').
10486 * This means that '*' will not be added, resulting in exactly the first Host
10487 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010488 * is hashed using the path hash followed by a full avalanche hash and provides a
10489 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010490 * high-traffic sites without having to store whole paths.
10491 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010492int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010493smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010494 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010495{
10496 struct http_txn *txn = l7;
10497 struct hdr_ctx ctx;
10498 unsigned int hash = 0;
10499 char *ptr, *beg, *end;
10500 int len;
10501
10502 CHECK_HTTP_MESSAGE_FIRST();
10503
10504 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010505 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010506 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10507 ptr = ctx.line + ctx.val;
10508 len = ctx.vlen;
10509 while (len--)
10510 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10511 }
10512
10513 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010514 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010515 beg = http_get_path(txn);
10516 if (!beg)
10517 beg = end;
10518
10519 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10520
10521 if (beg < ptr && *beg == '/') {
10522 while (beg < ptr)
10523 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10524 }
10525 hash = full_hash(hash);
10526
10527 smp->type = SMP_T_UINT;
10528 smp->data.uint = hash;
10529 smp->flags = SMP_F_VOL_1ST;
10530 return 1;
10531}
10532
Willy Tarreau4a550602012-12-09 14:53:32 +010010533/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010534 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10535 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10536 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010537 * that in environments where IPv6 is insignificant, truncating the output to
10538 * 8 bytes would still work.
10539 */
10540static int
10541smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010542 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010543{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010544 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010545 struct connection *cli_conn = objt_conn(l4->si[0].end);
10546
10547 if (!cli_conn)
10548 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010549
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010550 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010551 return 0;
10552
Willy Tarreau47ca5452012-12-23 20:22:19 +010010553 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010554 *(unsigned int *)temp->str = htonl(smp->data.uint);
10555 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010556
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010557 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010558 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010559 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010560 temp->len += 4;
10561 break;
10562 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010563 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010564 temp->len += 16;
10565 break;
10566 default:
10567 return 0;
10568 }
10569
10570 smp->data.str = *temp;
10571 smp->type = SMP_T_BIN;
10572 return 1;
10573}
10574
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010575/* Extracts the query string, which comes after the question mark '?'. If no
10576 * question mark is found, nothing is returned. Otherwise it returns a sample
10577 * of type string carrying the whole query string.
10578 */
10579static int
10580smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010581 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010582{
10583 struct http_txn *txn = l7;
10584 char *ptr, *end;
10585
10586 CHECK_HTTP_MESSAGE_FIRST();
10587
10588 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10589 end = ptr + txn->req.sl.rq.u_l;
10590
10591 /* look up the '?' */
10592 do {
10593 if (ptr == end)
10594 return 0;
10595 } while (*ptr++ != '?');
10596
10597 smp->type = SMP_T_STR;
10598 smp->data.str.str = ptr;
10599 smp->data.str.len = end - ptr;
10600 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10601 return 1;
10602}
10603
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010604static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010605smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010606 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010607{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010608 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10609 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10610 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010611
Willy Tarreau24e32d82012-04-23 23:55:44 +020010612 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010613
Willy Tarreauf853c462012-04-23 18:53:56 +020010614 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010615 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010616 return 1;
10617}
10618
Willy Tarreau7f18e522010-10-22 20:04:13 +020010619/* return a valid test if the current request is the first one on the connection */
10620static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010621smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010622 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010623{
10624 if (!s)
10625 return 0;
10626
Willy Tarreauf853c462012-04-23 18:53:56 +020010627 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010628 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010629 return 1;
10630}
10631
Willy Tarreau34db1082012-04-19 17:16:54 +020010632/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010633static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010634smp_fetch_http_auth(struct proxy *px, struct session *l4, 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)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010636{
10637
Willy Tarreau24e32d82012-04-23 23:55:44 +020010638 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010639 return 0;
10640
Willy Tarreauc0239e02012-04-16 14:42:55 +020010641 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010642
Willy Tarreauc0239e02012-04-16 14:42:55 +020010643 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010644 return 0;
10645
Willy Tarreauf853c462012-04-23 18:53:56 +020010646 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010647 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010648 return 1;
10649}
Willy Tarreau8797c062007-05-07 00:55:35 +020010650
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010651/* Accepts exactly 1 argument of type userlist */
10652static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010653smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010654 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010655{
10656
10657 if (!args || args->type != ARGT_USR)
10658 return 0;
10659
10660 CHECK_HTTP_MESSAGE_FIRST();
10661
10662 if (!get_http_auth(l4))
10663 return 0;
10664
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010665 /* if the user does not belong to the userlist or has a wrong password,
10666 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010667 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010668 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010669 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10670 return 0;
10671
10672 /* pat_match_auth() will need the user list */
10673 smp->ctx.a[0] = args->data.usr;
10674
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010675 smp->type = SMP_T_STR;
10676 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010677 smp->data.str.str = l4->txn.auth.user;
10678 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010679
10680 return 1;
10681}
10682
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010683/* Try to find the next occurrence of a cookie name in a cookie header value.
10684 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10685 * the cookie value is returned into *value and *value_l, and the function
10686 * returns a pointer to the next pointer to search from if the value was found.
10687 * Otherwise if the cookie was not found, NULL is returned and neither value
10688 * nor value_l are touched. The input <hdr> string should first point to the
10689 * header's value, and the <hdr_end> pointer must point to the first character
10690 * not part of the value. <list> must be non-zero if value may represent a list
10691 * of values (cookie headers). This makes it faster to abort parsing when no
10692 * list is expected.
10693 */
10694static char *
10695extract_cookie_value(char *hdr, const char *hdr_end,
10696 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010697 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010698{
10699 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10700 char *next;
10701
10702 /* we search at least a cookie name followed by an equal, and more
10703 * generally something like this :
10704 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10705 */
10706 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10707 /* Iterate through all cookies on this line */
10708
10709 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10710 att_beg++;
10711
10712 /* find att_end : this is the first character after the last non
10713 * space before the equal. It may be equal to hdr_end.
10714 */
10715 equal = att_end = att_beg;
10716
10717 while (equal < hdr_end) {
10718 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10719 break;
10720 if (http_is_spht[(unsigned char)*equal++])
10721 continue;
10722 att_end = equal;
10723 }
10724
10725 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10726 * is between <att_beg> and <equal>, both may be identical.
10727 */
10728
10729 /* look for end of cookie if there is an equal sign */
10730 if (equal < hdr_end && *equal == '=') {
10731 /* look for the beginning of the value */
10732 val_beg = equal + 1;
10733 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10734 val_beg++;
10735
10736 /* find the end of the value, respecting quotes */
10737 next = find_cookie_value_end(val_beg, hdr_end);
10738
10739 /* make val_end point to the first white space or delimitor after the value */
10740 val_end = next;
10741 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10742 val_end--;
10743 } else {
10744 val_beg = val_end = next = equal;
10745 }
10746
10747 /* We have nothing to do with attributes beginning with '$'. However,
10748 * they will automatically be removed if a header before them is removed,
10749 * since they're supposed to be linked together.
10750 */
10751 if (*att_beg == '$')
10752 continue;
10753
10754 /* Ignore cookies with no equal sign */
10755 if (equal == next)
10756 continue;
10757
10758 /* Now we have the cookie name between att_beg and att_end, and
10759 * its value between val_beg and val_end.
10760 */
10761
10762 if (att_end - att_beg == cookie_name_l &&
10763 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10764 /* let's return this value and indicate where to go on from */
10765 *value = val_beg;
10766 *value_l = val_end - val_beg;
10767 return next + 1;
10768 }
10769
10770 /* Set-Cookie headers only have the name in the first attr=value part */
10771 if (!list)
10772 break;
10773 }
10774
10775 return NULL;
10776}
10777
William Lallemanda43ba4e2014-01-28 18:14:25 +010010778/* Fetch a captured HTTP request header. The index is the position of
10779 * the "capture" option in the configuration file
10780 */
10781static int
10782smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010783 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010784{
10785 struct proxy *fe = l4->fe;
10786 struct http_txn *txn = l7;
10787 int idx;
10788
10789 if (!args || args->type != ARGT_UINT)
10790 return 0;
10791
10792 idx = args->data.uint;
10793
10794 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10795 return 0;
10796
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010797 smp->type = SMP_T_STR;
10798 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010799 smp->data.str.str = txn->req.cap[idx];
10800 smp->data.str.len = strlen(txn->req.cap[idx]);
10801
10802 return 1;
10803}
10804
10805/* Fetch a captured HTTP response header. The index is the position of
10806 * the "capture" option in the configuration file
10807 */
10808static int
10809smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010810 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010811{
10812 struct proxy *fe = l4->fe;
10813 struct http_txn *txn = l7;
10814 int idx;
10815
10816 if (!args || args->type != ARGT_UINT)
10817 return 0;
10818
10819 idx = args->data.uint;
10820
10821 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10822 return 0;
10823
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010824 smp->type = SMP_T_STR;
10825 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010826 smp->data.str.str = txn->rsp.cap[idx];
10827 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10828
10829 return 1;
10830}
10831
William Lallemand65ad6e12014-01-31 15:08:02 +010010832/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10833static int
10834smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010835 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010836{
10837 struct chunk *temp;
10838 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010839 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010840
10841 if (!txn->uri)
10842 return 0;
10843
William Lallemand96a77852014-02-05 00:30:02 +010010844 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010845
William Lallemand96a77852014-02-05 00:30:02 +010010846 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10847 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010848
William Lallemand96a77852014-02-05 00:30:02 +010010849 temp = get_trash_chunk();
10850 temp->str = txn->uri;
10851 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010852 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010853 smp->type = SMP_T_STR;
10854 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010855
10856 return 1;
10857
10858}
10859
10860/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10861static int
10862smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010863 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010864{
10865 struct chunk *temp;
10866 struct http_txn *txn = l7;
10867 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010868
10869 if (!txn->uri)
10870 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010871
William Lallemand65ad6e12014-01-31 15:08:02 +010010872 ptr = txn->uri;
10873
10874 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10875 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010876
William Lallemand65ad6e12014-01-31 15:08:02 +010010877 if (!*ptr)
10878 return 0;
10879
10880 ptr++; /* skip the space */
10881
10882 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010883 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010884 if (!ptr)
10885 return 0;
10886 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10887 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010888
10889 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010890 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010891 smp->type = SMP_T_STR;
10892 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010893
10894 return 1;
10895}
10896
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010897/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10898 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10899 */
10900static int
10901smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010902 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010903{
10904 struct http_txn *txn = l7;
10905
10906 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10907 return 0;
10908
10909 if (txn->req.flags & HTTP_MSGF_VER_11)
10910 smp->data.str.str = "HTTP/1.1";
10911 else
10912 smp->data.str.str = "HTTP/1.0";
10913
10914 smp->data.str.len = 8;
10915 smp->type = SMP_T_STR;
10916 smp->flags = SMP_F_CONST;
10917 return 1;
10918
10919}
10920
10921/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10922 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10923 */
10924static int
10925smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010926 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010927{
10928 struct http_txn *txn = l7;
10929
10930 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10931 return 0;
10932
10933 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10934 smp->data.str.str = "HTTP/1.1";
10935 else
10936 smp->data.str.str = "HTTP/1.0";
10937
10938 smp->data.str.len = 8;
10939 smp->type = SMP_T_STR;
10940 smp->flags = SMP_F_CONST;
10941 return 1;
10942
10943}
10944
William Lallemand65ad6e12014-01-31 15:08:02 +010010945
Willy Tarreaue333ec92012-04-16 16:26:40 +020010946/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010947 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010948 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010949 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010950 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010951 * Accepts exactly 1 argument of type string. If the input options indicate
10952 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010953 * The returned sample is of type CSTR. Can be used to parse cookies in other
10954 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010955 */
William Lallemand07c8b242014-05-02 17:11:07 +020010956int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010957 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010958{
10959 struct http_txn *txn = l7;
10960 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010961 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 const struct http_msg *msg;
10963 const char *hdr_name;
10964 int hdr_name_len;
10965 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010966 int occ = 0;
10967 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010968
Willy Tarreau24e32d82012-04-23 23:55:44 +020010969 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010970 return 0;
10971
Willy Tarreaua890d072013-04-02 12:01:06 +020010972 if (!ctx) {
10973 /* first call */
10974 ctx = &static_hdr_ctx;
10975 ctx->idx = 0;
10976 smp->ctx.a[2] = ctx;
10977 }
10978
Willy Tarreaue333ec92012-04-16 16:26:40 +020010979 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010980
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010981 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010982 msg = &txn->req;
10983 hdr_name = "Cookie";
10984 hdr_name_len = 6;
10985 } else {
10986 msg = &txn->rsp;
10987 hdr_name = "Set-Cookie";
10988 hdr_name_len = 10;
10989 }
10990
Willy Tarreau28376d62012-04-26 21:26:10 +020010991 if (!occ && !(opt & SMP_OPT_ITERATE))
10992 /* no explicit occurrence and single fetch => last cookie by default */
10993 occ = -1;
10994
10995 /* OK so basically here, either we want only one value and it's the
10996 * last one, or we want to iterate over all of them and we fetch the
10997 * next one.
10998 */
10999
Willy Tarreau9b28e032012-10-12 23:49:43 +020011000 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011001 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002 /* search for the header from the beginning, we must first initialize
11003 * the search parameters.
11004 */
Willy Tarreau37406352012-04-23 16:16:37 +020011005 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011006 ctx->idx = 0;
11007 }
11008
Willy Tarreau28376d62012-04-26 21:26:10 +020011009 smp->flags |= SMP_F_VOL_HDR;
11010
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011011 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011012 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11013 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011014 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11015 goto out;
11016
Willy Tarreau24e32d82012-04-23 23:55:44 +020011017 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011018 continue;
11019
Willy Tarreau37406352012-04-23 16:16:37 +020011020 smp->ctx.a[0] = ctx->line + ctx->val;
11021 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011022 }
11023
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011024 smp->type = SMP_T_STR;
11025 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011026 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011027 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011028 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011029 &smp->data.str.str,
11030 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011031 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011032 found = 1;
11033 if (occ >= 0) {
11034 /* one value was returned into smp->data.str.{str,len} */
11035 smp->flags |= SMP_F_NOT_LAST;
11036 return 1;
11037 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011038 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011039 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011040 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011041 /* all cookie headers and values were scanned. If we're looking for the
11042 * last occurrence, we may return it now.
11043 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011044 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011045 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011046 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011047}
11048
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011049/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011050 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011051 * multiple cookies may be parsed on the same line. The returned sample is of
11052 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011053 */
11054static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010011055smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011056 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011057{
11058 struct http_txn *txn = l7;
11059 struct hdr_idx *idx = &txn->hdr_idx;
11060 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011061 const struct http_msg *msg;
11062 const char *hdr_name;
11063 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011064 int cnt;
11065 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011066 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011067
Willy Tarreau24e32d82012-04-23 23:55:44 +020011068 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011069 return 0;
11070
Willy Tarreaue333ec92012-04-16 16:26:40 +020011071 CHECK_HTTP_MESSAGE_FIRST();
11072
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011073 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011074 msg = &txn->req;
11075 hdr_name = "Cookie";
11076 hdr_name_len = 6;
11077 } else {
11078 msg = &txn->rsp;
11079 hdr_name = "Set-Cookie";
11080 hdr_name_len = 10;
11081 }
11082
Willy Tarreau9b28e032012-10-12 23:49:43 +020011083 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011084 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011085 ctx.idx = 0;
11086 cnt = 0;
11087
11088 while (1) {
11089 /* Note: val_beg == NULL every time we need to fetch a new header */
11090 if (!val_beg) {
11091 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11092 break;
11093
Willy Tarreau24e32d82012-04-23 23:55:44 +020011094 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011095 continue;
11096
11097 val_beg = ctx.line + ctx.val;
11098 val_end = val_beg + ctx.vlen;
11099 }
11100
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011101 smp->type = SMP_T_STR;
11102 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011103 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011104 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011105 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011106 &smp->data.str.str,
11107 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108 cnt++;
11109 }
11110 }
11111
Willy Tarreaub169eba2013-12-16 15:14:43 +010011112 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011113 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011114 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115 return 1;
11116}
11117
Willy Tarreau51539362012-05-08 12:46:28 +020011118/* Fetch an cookie's integer value. The integer value is returned. It
11119 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11120 */
11121static int
11122smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011123 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011124{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011125 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011126
11127 if (ret > 0) {
11128 smp->type = SMP_T_UINT;
11129 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11130 }
11131
11132 return ret;
11133}
11134
Willy Tarreau8797c062007-05-07 00:55:35 +020011135/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011136/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011137/************************************************************************/
11138
David Cournapeau16023ee2010-12-23 20:55:41 +090011139/*
11140 * Given a path string and its length, find the position of beginning of the
11141 * query string. Returns NULL if no query string is found in the path.
11142 *
11143 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11144 *
11145 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11146 */
bedis4c75cca2012-10-05 08:38:24 +020011147static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011148{
11149 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011150
bedis4c75cca2012-10-05 08:38:24 +020011151 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011152 return p ? p + 1 : NULL;
11153}
11154
bedis4c75cca2012-10-05 08:38:24 +020011155static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011156{
bedis4c75cca2012-10-05 08:38:24 +020011157 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011158}
11159
11160/*
11161 * Given a url parameter, find the starting position of the first occurence,
11162 * or NULL if the parameter is not found.
11163 *
11164 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11165 * the function will return query_string+8.
11166 */
11167static char*
11168find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011169 char* url_param_name, size_t url_param_name_l,
11170 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011171{
11172 char *pos, *last;
11173
11174 pos = query_string;
11175 last = query_string + query_string_l - url_param_name_l - 1;
11176
11177 while (pos <= last) {
11178 if (pos[url_param_name_l] == '=') {
11179 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11180 return pos;
11181 pos += url_param_name_l + 1;
11182 }
bedis4c75cca2012-10-05 08:38:24 +020011183 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011184 pos++;
11185 pos++;
11186 }
11187 return NULL;
11188}
11189
11190/*
11191 * Given a url parameter name, returns its value and size into *value and
11192 * *value_l respectively, and returns non-zero. If the parameter is not found,
11193 * zero is returned and value/value_l are not touched.
11194 */
11195static int
11196find_url_param_value(char* path, size_t path_l,
11197 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011198 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011199{
11200 char *query_string, *qs_end;
11201 char *arg_start;
11202 char *value_start, *value_end;
11203
bedis4c75cca2012-10-05 08:38:24 +020011204 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011205 if (!query_string)
11206 return 0;
11207
11208 qs_end = path + path_l;
11209 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011210 url_param_name, url_param_name_l,
11211 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011212 if (!arg_start)
11213 return 0;
11214
11215 value_start = arg_start + url_param_name_l + 1;
11216 value_end = value_start;
11217
bedis4c75cca2012-10-05 08:38:24 +020011218 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011219 value_end++;
11220
11221 *value = value_start;
11222 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011223 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011224}
11225
11226static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011227smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011228 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011229{
bedis4c75cca2012-10-05 08:38:24 +020011230 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011231 struct http_txn *txn = l7;
11232 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011233
bedis4c75cca2012-10-05 08:38:24 +020011234 if (!args || args[0].type != ARGT_STR ||
11235 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011236 return 0;
11237
11238 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011239
bedis4c75cca2012-10-05 08:38:24 +020011240 if (args[1].type)
11241 delim = *args[1].data.str.str;
11242
Willy Tarreau9b28e032012-10-12 23:49:43 +020011243 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011244 args->data.str.str, args->data.str.len,
11245 &smp->data.str.str, &smp->data.str.len,
11246 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011247 return 0;
11248
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011249 smp->type = SMP_T_STR;
11250 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011251 return 1;
11252}
11253
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011254/* Return the signed integer value for the specified url parameter (see url_param
11255 * above).
11256 */
11257static int
11258smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011259 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011260{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011261 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011262
11263 if (ret > 0) {
11264 smp->type = SMP_T_UINT;
11265 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11266 }
11267
11268 return ret;
11269}
11270
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011271/* This produces a 32-bit hash of the concatenation of the first occurrence of
11272 * the Host header followed by the path component if it begins with a slash ('/').
11273 * This means that '*' will not be added, resulting in exactly the first Host
11274 * entry. If no Host header is found, then the path is used. The resulting value
11275 * is hashed using the url hash followed by a full avalanche hash and provides a
11276 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11277 * high-traffic sites without having to store whole paths.
11278 * this differs from the base32 functions in that it includes the url parameters
11279 * as well as the path
11280 */
11281static int
11282smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011283 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011284{
11285 struct http_txn *txn = l7;
11286 struct hdr_ctx ctx;
11287 unsigned int hash = 0;
11288 char *ptr, *beg, *end;
11289 int len;
11290
11291 CHECK_HTTP_MESSAGE_FIRST();
11292
11293 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011294 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011295 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11296 ptr = ctx.line + ctx.val;
11297 len = ctx.vlen;
11298 while (len--)
11299 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11300 }
11301
11302 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011303 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 +000011304 beg = http_get_path(txn);
11305 if (!beg)
11306 beg = end;
11307
11308 for (ptr = beg; ptr < end ; ptr++);
11309
11310 if (beg < ptr && *beg == '/') {
11311 while (beg < ptr)
11312 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11313 }
11314 hash = full_hash(hash);
11315
11316 smp->type = SMP_T_UINT;
11317 smp->data.uint = hash;
11318 smp->flags = SMP_F_VOL_1ST;
11319 return 1;
11320}
11321
11322/* This concatenates the source address with the 32-bit hash of the Host and
11323 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11324 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11325 * on the source address length. The URL hash is stored before the address so
11326 * that in environments where IPv6 is insignificant, truncating the output to
11327 * 8 bytes would still work.
11328 */
11329static int
11330smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011331 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011332{
11333 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011334 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011335
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011336 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011337 return 0;
11338
11339 temp = get_trash_chunk();
11340 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11341 temp->len += sizeof(smp->data.uint);
11342
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011343 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011344 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011345 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011346 temp->len += 4;
11347 break;
11348 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011349 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011350 temp->len += 16;
11351 break;
11352 default:
11353 return 0;
11354 }
11355
11356 smp->data.str = *temp;
11357 smp->type = SMP_T_BIN;
11358 return 1;
11359}
11360
Willy Tarreau185b5c42012-04-26 15:11:51 +020011361/* This function is used to validate the arguments passed to any "hdr" fetch
11362 * keyword. These keywords support an optional positive or negative occurrence
11363 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11364 * is assumed that the types are already the correct ones. Returns 0 on error,
11365 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11366 * error message in case of error, that the caller is responsible for freeing.
11367 * The initial location must either be freeable or NULL.
11368 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011369int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011370{
11371 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011372 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011373 return 0;
11374 }
11375 return 1;
11376}
11377
Willy Tarreau276fae92013-07-25 14:36:01 +020011378/* takes an UINT value on input supposed to represent the time since EPOCH,
11379 * adds an optional offset found in args[0] and emits a string representing
11380 * the date in RFC-1123/5322 format.
11381 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011382static int sample_conv_http_date(struct session *session, const struct arg *args,
11383 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011384{
11385 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11386 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11387 struct chunk *temp;
11388 struct tm *tm;
11389 time_t curr_date = smp->data.uint;
11390
11391 /* add offset */
11392 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11393 curr_date += args[0].data.sint;
11394
11395 tm = gmtime(&curr_date);
11396
11397 temp = get_trash_chunk();
11398 temp->len = snprintf(temp->str, temp->size - temp->len,
11399 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11400 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11401 tm->tm_hour, tm->tm_min, tm->tm_sec);
11402
11403 smp->data.str = *temp;
11404 smp->type = SMP_T_STR;
11405 return 1;
11406}
11407
Thierry FOURNIERad903512014-04-11 17:51:01 +020011408/* Match language range with language tag. RFC2616 14.4:
11409 *
11410 * A language-range matches a language-tag if it exactly equals
11411 * the tag, or if it exactly equals a prefix of the tag such
11412 * that the first tag character following the prefix is "-".
11413 *
11414 * Return 1 if the strings match, else return 0.
11415 */
11416static inline int language_range_match(const char *range, int range_len,
11417 const char *tag, int tag_len)
11418{
11419 const char *end = range + range_len;
11420 const char *tend = tag + tag_len;
11421 while (range < end) {
11422 if (*range == '-' && tag == tend)
11423 return 1;
11424 if (*range != *tag || tag == tend)
11425 return 0;
11426 range++;
11427 tag++;
11428 }
11429 /* Return true only if the last char of the tag is matched. */
11430 return tag == tend;
11431}
11432
11433/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011434static int sample_conv_q_prefered(struct session *session, const struct arg *args,
11435 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011436{
11437 const char *al = smp->data.str.str;
11438 const char *end = al + smp->data.str.len;
11439 const char *token;
11440 int toklen;
11441 int qvalue;
11442 const char *str;
11443 const char *w;
11444 int best_q = 0;
11445
11446 /* Set the constant to the sample, because the output of the
11447 * function will be peek in the constant configuration string.
11448 */
11449 smp->flags |= SMP_F_CONST;
11450 smp->data.str.size = 0;
11451 smp->data.str.str = "";
11452 smp->data.str.len = 0;
11453
11454 /* Parse the accept language */
11455 while (1) {
11456
11457 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011458 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011459 al++;
11460 if (al >= end)
11461 break;
11462
11463 /* Start of the fisrt word. */
11464 token = al;
11465
11466 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011467 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011468 al++;
11469 if (al == token)
11470 goto expect_comma;
11471
11472 /* Length of the token. */
11473 toklen = al - token;
11474 qvalue = 1000;
11475
11476 /* Check if the token exists in the list. If the token not exists,
11477 * jump to the next token.
11478 */
11479 str = args[0].data.str.str;
11480 w = str;
11481 while (1) {
11482 if (*str == ';' || *str == '\0') {
11483 if (language_range_match(token, toklen, w, str-w))
11484 goto look_for_q;
11485 if (*str == '\0')
11486 goto expect_comma;
11487 w = str + 1;
11488 }
11489 str++;
11490 }
11491 goto expect_comma;
11492
11493look_for_q:
11494
11495 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011496 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011497 al++;
11498 if (al >= end)
11499 goto process_value;
11500
11501 /* If ',' is found, process the result */
11502 if (*al == ',')
11503 goto process_value;
11504
11505 /* If the character is different from ';', look
11506 * for the end of the header part in best effort.
11507 */
11508 if (*al != ';')
11509 goto expect_comma;
11510
11511 /* Assumes that the char is ';', now expect "q=". */
11512 al++;
11513
11514 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011515 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011516 al++;
11517 if (al >= end)
11518 goto process_value;
11519
11520 /* Expect 'q'. If no 'q', continue in best effort */
11521 if (*al != 'q')
11522 goto process_value;
11523 al++;
11524
11525 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011526 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011527 al++;
11528 if (al >= end)
11529 goto process_value;
11530
11531 /* Expect '='. If no '=', continue in best effort */
11532 if (*al != '=')
11533 goto process_value;
11534 al++;
11535
11536 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011537 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011538 al++;
11539 if (al >= end)
11540 goto process_value;
11541
11542 /* Parse the q value. */
11543 qvalue = parse_qvalue(al, &al);
11544
11545process_value:
11546
11547 /* If the new q value is the best q value, then store the associated
11548 * language in the response. If qvalue is the biggest value (1000),
11549 * break the process.
11550 */
11551 if (qvalue > best_q) {
11552 smp->data.str.str = (char *)w;
11553 smp->data.str.len = str - w;
11554 if (qvalue >= 1000)
11555 break;
11556 best_q = qvalue;
11557 }
11558
11559expect_comma:
11560
11561 /* Expect comma or end. If the end is detected, quit the loop. */
11562 while (al < end && *al != ',')
11563 al++;
11564 if (al >= end)
11565 break;
11566
11567 /* Comma is found, jump it and restart the analyzer. */
11568 al++;
11569 }
11570
11571 /* Set default value if required. */
11572 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11573 smp->data.str.str = args[1].data.str.str;
11574 smp->data.str.len = args[1].data.str.len;
11575 }
11576
11577 /* Return true only if a matching language was found. */
11578 return smp->data.str.len != 0;
11579}
11580
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011581/* This function executes one of the set-{method,path,query,uri} actions. It
11582 * builds a string in the trash from the specified format string, then modifies
11583 * the relevant part of the request line accordingly. Then it updates various
11584 * pointers to the next elements which were moved, and the total buffer length.
11585 * It finds the action to be performed in p[2], previously filled by function
11586 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11587 * error, though this can be revisited when this code is finally exploited.
11588 */
11589int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn)
11590{
11591 char *cur_ptr, *cur_end;
11592 int offset;
11593 int delta;
11594
11595 chunk_reset(&trash);
11596
11597 /* prepare a '?' just in case we have to create a query string */
11598 trash.str[trash.len++] = '?';
11599 offset = 1;
11600 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11601
11602 switch (*(int *)&rule->arg.act.p[2]) {
11603 case 0: // method
11604 cur_ptr = s->req->buf->p;
11605 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11606
11607 /* adjust req line offsets and lengths */
11608 delta = trash.len - offset - (cur_end - cur_ptr);
11609 txn->req.sl.rq.m_l += delta;
11610 txn->req.sl.rq.u += delta;
11611 txn->req.sl.rq.v += delta;
11612 break;
11613
11614 case 1: // path
11615 cur_ptr = http_get_path(txn);
11616 if (!cur_ptr)
11617 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11618
11619 cur_end = cur_ptr;
11620 while (cur_end < s->req->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
11621 cur_end++;
11622
11623 /* adjust req line offsets and lengths */
11624 delta = trash.len - offset - (cur_end - cur_ptr);
11625 txn->req.sl.rq.u_l += delta;
11626 txn->req.sl.rq.v += delta;
11627 break;
11628
11629 case 2: // query
11630 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11631 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11632 while (cur_ptr < cur_end && *cur_ptr != '?')
11633 cur_ptr++;
11634
11635 /* skip the question mark or indicate that we must insert it
11636 * (but only if the format string is not empty then).
11637 */
11638 if (cur_ptr < cur_end)
11639 cur_ptr++;
11640 else if (trash.len > 1)
11641 offset = 0;
11642
11643 /* adjust req line offsets and lengths */
11644 delta = trash.len - offset - (cur_end - cur_ptr);
11645 txn->req.sl.rq.u_l += delta;
11646 txn->req.sl.rq.v += delta;
11647 break;
11648
11649 case 3: // uri
11650 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11651 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11652
11653 /* adjust req line offsets and lengths */
11654 delta = trash.len - offset - (cur_end - cur_ptr);
11655 txn->req.sl.rq.u_l += delta;
11656 txn->req.sl.rq.v += delta;
11657 break;
11658
11659 default:
11660 return -1;
11661 }
11662
11663 /* commit changes and adjust end of message */
11664 delta = buffer_replace2(s->req->buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset);
11665 http_msg_move_end(&txn->req, delta);
11666 return 0;
11667}
11668
11669/* parse an http-request action among :
11670 * set-method
11671 * set-path
11672 * set-query
11673 * set-uri
11674 *
11675 * All of them accept a single argument of type string representing a log-format.
11676 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11677 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11678 * It returns 0 on success, < 0 on error.
11679 */
11680int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11681{
11682 int cur_arg = *orig_arg;
11683
11684 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11685
11686 switch (args[0][4]) {
11687 case 'm' :
11688 *(int *)&rule->arg.act.p[2] = 0;
11689 rule->action_ptr = http_action_set_req_line;
11690 break;
11691 case 'p' :
11692 *(int *)&rule->arg.act.p[2] = 1;
11693 rule->action_ptr = http_action_set_req_line;
11694 break;
11695 case 'q' :
11696 *(int *)&rule->arg.act.p[2] = 2;
11697 rule->action_ptr = http_action_set_req_line;
11698 break;
11699 case 'u' :
11700 *(int *)&rule->arg.act.p[2] = 3;
11701 rule->action_ptr = http_action_set_req_line;
11702 break;
11703 default:
11704 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11705 return -1;
11706 }
11707
11708 if (!*args[cur_arg] ||
11709 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11710 memprintf(err, "expects exactly 1 argument <format>");
11711 return -1;
11712 }
11713
11714 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11715 proxy->conf.args.ctx = ARGC_HRQ;
11716 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11717 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11718 proxy->conf.args.file, proxy->conf.args.line);
11719
11720 (*orig_arg)++;
11721 return 0;
11722}
11723
William Lallemand73025dd2014-04-24 14:38:37 +020011724/*
11725 * Return the struct http_req_action_kw associated to a keyword.
11726 */
11727struct http_req_action_kw *action_http_req_custom(const char *kw)
11728{
11729 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11730 struct http_req_action_kw_list *kw_list;
11731 int i;
11732
11733 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11734 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11735 if (!strcmp(kw, kw_list->kw[i].kw))
11736 return &kw_list->kw[i];
11737 }
11738 }
11739 }
11740 return NULL;
11741}
11742
11743/*
11744 * Return the struct http_res_action_kw associated to a keyword.
11745 */
11746struct http_res_action_kw *action_http_res_custom(const char *kw)
11747{
11748 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11749 struct http_res_action_kw_list *kw_list;
11750 int i;
11751
11752 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11753 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11754 if (!strcmp(kw, kw_list->kw[i].kw))
11755 return &kw_list->kw[i];
11756 }
11757 }
11758 }
11759 return NULL;
11760}
11761
Willy Tarreau4a568972010-05-12 08:08:50 +020011762/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011763/* All supported ACL keywords must be declared here. */
11764/************************************************************************/
11765
11766/* Note: must not be declared <const> as its list will be overwritten.
11767 * Please take care of keeping this list alphabetically sorted.
11768 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011769static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011770 { "base", "base", PAT_MATCH_STR },
11771 { "base_beg", "base", PAT_MATCH_BEG },
11772 { "base_dir", "base", PAT_MATCH_DIR },
11773 { "base_dom", "base", PAT_MATCH_DOM },
11774 { "base_end", "base", PAT_MATCH_END },
11775 { "base_len", "base", PAT_MATCH_LEN },
11776 { "base_reg", "base", PAT_MATCH_REG },
11777 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011778
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011779 { "cook", "req.cook", PAT_MATCH_STR },
11780 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11781 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11782 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11783 { "cook_end", "req.cook", PAT_MATCH_END },
11784 { "cook_len", "req.cook", PAT_MATCH_LEN },
11785 { "cook_reg", "req.cook", PAT_MATCH_REG },
11786 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011787
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011788 { "hdr", "req.hdr", PAT_MATCH_STR },
11789 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11790 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11791 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11792 { "hdr_end", "req.hdr", PAT_MATCH_END },
11793 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11794 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11795 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011796
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011797 /* these two declarations uses strings with list storage (in place
11798 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11799 * and delete functions are relative to the list management. The parse
11800 * and match method are related to the corresponding fetch methods. This
11801 * is very particular ACL declaration mode.
11802 */
11803 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11804 { "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 +020011805
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011806 { "path", "path", PAT_MATCH_STR },
11807 { "path_beg", "path", PAT_MATCH_BEG },
11808 { "path_dir", "path", PAT_MATCH_DIR },
11809 { "path_dom", "path", PAT_MATCH_DOM },
11810 { "path_end", "path", PAT_MATCH_END },
11811 { "path_len", "path", PAT_MATCH_LEN },
11812 { "path_reg", "path", PAT_MATCH_REG },
11813 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011814
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011815 { "req_ver", "req.ver", PAT_MATCH_STR },
11816 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011817
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011818 { "scook", "res.cook", PAT_MATCH_STR },
11819 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11820 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11821 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11822 { "scook_end", "res.cook", PAT_MATCH_END },
11823 { "scook_len", "res.cook", PAT_MATCH_LEN },
11824 { "scook_reg", "res.cook", PAT_MATCH_REG },
11825 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011826
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011827 { "shdr", "res.hdr", PAT_MATCH_STR },
11828 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11829 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11830 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11831 { "shdr_end", "res.hdr", PAT_MATCH_END },
11832 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11833 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11834 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011835
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011836 { "url", "url", PAT_MATCH_STR },
11837 { "url_beg", "url", PAT_MATCH_BEG },
11838 { "url_dir", "url", PAT_MATCH_DIR },
11839 { "url_dom", "url", PAT_MATCH_DOM },
11840 { "url_end", "url", PAT_MATCH_END },
11841 { "url_len", "url", PAT_MATCH_LEN },
11842 { "url_reg", "url", PAT_MATCH_REG },
11843 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011844
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011845 { "urlp", "urlp", PAT_MATCH_STR },
11846 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11847 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11848 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11849 { "urlp_end", "urlp", PAT_MATCH_END },
11850 { "urlp_len", "urlp", PAT_MATCH_LEN },
11851 { "urlp_reg", "urlp", PAT_MATCH_REG },
11852 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011853
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011854 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011855}};
11856
11857/************************************************************************/
11858/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011859/************************************************************************/
11860/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011861static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011862 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011863 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11864 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11865
William Lallemanda43ba4e2014-01-28 18:14:25 +010011866 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011867 { "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 +020011868
11869 /* retrieve these captures from the HTTP logs */
11870 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11871 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11872 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11873
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011874 { "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 +020011875 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011876
Willy Tarreau409bcde2013-01-08 00:31:00 +010011877 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11878 * are only here to match the ACL's name, are request-only and are used
11879 * for ACL compatibility only.
11880 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011881 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11882 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011883 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11884 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11885
11886 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11887 * only here to match the ACL's name, are request-only and are used for
11888 * ACL compatibility only.
11889 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011890 { "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 +010011891 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11892 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11893 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11894
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011895 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011896 { "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 +010011897 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011898 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011899 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011900 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011901
11902 /* HTTP protocol on the request path */
11903 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011904 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011905
11906 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011907 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11908 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011909
11910 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011911 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11912 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011913
Willy Tarreau18ed2562013-01-14 15:56:36 +010011914 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011915 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011916 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11917 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11918
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011919 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011920 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011921 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011922 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11923 { "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 +010011924 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011925 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11926
11927 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011928 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011929 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11930 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11931
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011932 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011933 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011934 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011935 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11936 { "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 +010011937 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011938 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11939
Willy Tarreau409bcde2013-01-08 00:31:00 +010011940 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011941 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011942 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11943 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011944 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011945
11946 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011947 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011948 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11949 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11950 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11951
11952 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011953 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011954 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11955 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011956 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11957 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011958 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11959 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011960 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11961 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011962}};
11963
Willy Tarreau8797c062007-05-07 00:55:35 +020011964
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011965/************************************************************************/
11966/* All supported converter keywords must be declared here. */
11967/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020011968/* Note: must not be declared <const> as its list will be overwritten */
11969static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011970 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11971 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011972 { NULL, NULL, 0, 0, 0 },
11973}};
11974
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011975/************************************************************************/
11976/* All supported http-request action keywords must be declared here. */
11977/************************************************************************/
11978struct http_req_action_kw_list http_req_actions = {
11979 .scope = "http",
11980 .kw = {
11981 { "set-method", parse_set_req_line },
11982 { "set-path", parse_set_req_line },
11983 { "set-query", parse_set_req_line },
11984 { "set-uri", parse_set_req_line },
11985 }
11986};
11987
Willy Tarreau8797c062007-05-07 00:55:35 +020011988__attribute__((constructor))
11989static void __http_protocol_init(void)
11990{
11991 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011992 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011993 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011994 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020011995}
11996
11997
Willy Tarreau58f10d72006-12-04 02:26:12 +010011998/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011999 * Local variables:
12000 * c-indent-level: 8
12001 * c-basic-offset: 8
12002 * End:
12003 */