blob: 611a8c1948d67169394cd5eb781b15cf5706e0de [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreauc90dc232015-02-20 13:51:36 +0100590/* Find the first or next header field in message buffer <sol> using headers
591 * index <idx>, and return it in the <ctx> structure. This structure holds
592 * everything necessary to use the header and find next occurrence. If its
593 * <idx> member is 0, the first header is retrieved. Otherwise, the next
594 * occurrence is returned. The function returns 1 when it finds a value, and
595 * 0 when there is no more. It is equivalent to http_find_full_header2() with
596 * no header name.
597 */
598int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
599{
600 char *eol, *sov;
601 int cur_idx, old_idx;
602 int len;
603
604 cur_idx = ctx->idx;
605 if (cur_idx) {
606 /* We have previously returned a header, let's search another one */
607 sol = ctx->line;
608 eol = sol + idx->v[cur_idx].len;
609 goto next_hdr;
610 }
611
612 /* first request for this header */
613 sol += hdr_idx_first_pos(idx);
614 old_idx = 0;
615 cur_idx = hdr_idx_first_idx(idx);
616 while (cur_idx) {
617 eol = sol + idx->v[cur_idx].len;
618
619 len = 0;
620 while (1) {
621 if (len >= eol - sol)
622 goto next_hdr;
623 if (sol[len] == ':')
624 break;
625 len++;
626 }
627
628 ctx->del = len;
629 sov = sol + len + 1;
630 while (sov < eol && http_is_lws[(unsigned char)*sov])
631 sov++;
632
633 ctx->line = sol;
634 ctx->prev = old_idx;
635 ctx->idx = cur_idx;
636 ctx->val = sov - sol;
637 ctx->tws = 0;
638
639 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
640 eol--;
641 ctx->tws++;
642 }
643 ctx->vlen = eol - sov;
644 return 1;
645
646 next_hdr:
647 sol = eol + idx->v[cur_idx].cr + 1;
648 old_idx = cur_idx;
649 cur_idx = idx->v[cur_idx].next;
650 }
651 return 0;
652}
653
Willy Tarreau68085d82010-01-18 14:54:04 +0100654/* Find the end of the header value contained between <s> and <e>. See RFC2616,
655 * par 2.2 for more information. Note that it requires a valid header to return
656 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100658char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200659{
660 int quoted, qdpair;
661
662 quoted = qdpair = 0;
663 for (; s < e; s++) {
664 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200665 else if (quoted) {
666 if (*s == '\\') qdpair = 1;
667 else if (*s == '"') quoted = 0;
668 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 else if (*s == '"') quoted = 1;
670 else if (*s == ',') return s;
671 }
672 return s;
673}
674
675/* Find the first or next occurrence of header <name> in message buffer <sol>
676 * using headers index <idx>, and return it in the <ctx> structure. This
677 * structure holds everything necessary to use the header and find next
678 * occurrence. If its <idx> member is 0, the header is searched from the
679 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * 1 when it finds a value, and 0 when there is no more. It is designed to work
681 * with headers defined as comma-separated lists. As a special case, if ctx->val
682 * is NULL when searching for a new values of a header, the current header is
683 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200684 */
685int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
Willy Tarreau68085d82010-01-18 14:54:04 +0100689 char *eol, *sov;
690 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 cur_idx = ctx->idx;
693 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200694 /* We have previously returned a value, let's search
695 * another one on the same line.
696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200697 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200698 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 eol = sol + idx->v[cur_idx].len;
701
702 if (sov >= eol)
703 /* no more values in this header */
704 goto next_hdr;
705
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 /* values remaining for this header, skip the comma but save it
707 * for later use (eg: for header deletion).
708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200709 sov++;
710 while (sov < eol && http_is_lws[(unsigned char)*sov])
711 sov++;
712
713 goto return_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200723 if (len == 0) {
724 /* No argument was passed, we want any header.
725 * To achieve this, we simply build a fake request. */
726 while (sol + len < eol && sol[len] != ':')
727 len++;
728 name = sol;
729 }
730
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 if ((len < eol - sol) &&
732 (sol[len] == ':') &&
733 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200735 sov = sol + len + 1;
736 while (sov < eol && http_is_lws[(unsigned char)*sov])
737 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100738
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 ctx->prev = old_idx;
741 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744
745 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200747 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200748 eol--;
749 ctx->tws++;
750 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 ctx->vlen = eol - sov;
752 return 1;
753 }
754 next_hdr:
755 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100756 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 cur_idx = idx->v[cur_idx].next;
758 }
759 return 0;
760}
761
762int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 struct hdr_ctx *ctx)
765{
766 return http_find_header2(name, strlen(name), sol, idx, ctx);
767}
768
Willy Tarreau68085d82010-01-18 14:54:04 +0100769/* Remove one value of a header. This only works on a <ctx> returned by one of
770 * the http_find_header functions. The value is removed, as well as surrounding
771 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * message <msg>. The new index is returned. If it is zero, it means there is
774 * no more header, so any processing may stop. The ctx is always left in a form
775 * that can be handled by http_find_header2() to find next occurrence.
776 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100778{
779 int cur_idx = ctx->idx;
780 char *sol = ctx->line;
781 struct hdr_idx_elem *hdr;
782 int delta, skip_comma;
783
784 if (!cur_idx)
785 return 0;
786
787 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 http_msg_move_end(msg, delta);
792 idx->used--;
793 hdr->len = 0; /* unused entry */
794 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100795 if (idx->tail == ctx->idx)
796 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100798 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200800 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 return ctx->idx;
802 }
803
804 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
806 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 */
808
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200809 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200811 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 NULL, 0);
813 hdr->len += delta;
814 http_msg_move_end(msg, delta);
815 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200816 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 return ctx->idx;
818}
819
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820/* This function handles a server error at the stream interface level. The
821 * stream interface is assumed to be already in a closed state. An optional
822 * message is copied into the input buffer, and an HTTP status code stored.
823 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100824 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200826static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100829 channel_auto_read(si_oc(si));
830 channel_abort(si_oc(si));
831 channel_auto_close(si_oc(si));
832 channel_erase(si_oc(si));
833 channel_auto_close(si_ic(si));
834 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100835 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200836 s->txn.status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100837 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200839 if (!(s->flags & SN_ERR_MASK))
840 s->flags |= err;
841 if (!(s->flags & SN_FINST_MASK))
842 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843}
844
Willy Tarreau80587432006-12-24 17:47:20 +0100845/* This function returns the appropriate error location for the given session
846 * and message.
847 */
848
Willy Tarreau783f2582012-09-04 12:19:04 +0200849struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100850{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 if (s->be->errmsg[msgnum].str)
852 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100853 else if (s->fe->errmsg[msgnum].str)
854 return &s->fe->errmsg[msgnum];
855 else
856 return &http_err_chunks[msgnum];
857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreau53b6c742006-12-17 13:37:46 +0100859/*
860 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
861 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
862 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100864{
865 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100867
868 m = ((unsigned)*str - 'A');
869
870 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 for (h = http_methods[m]; h->len > 0; h++) {
872 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100873 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100875 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 };
877 return HTTP_METH_OTHER;
878 }
879 return HTTP_METH_NONE;
880
881}
882
Willy Tarreau21d2af32008-02-14 20:25:24 +0100883/* Parse the URI from the given transaction (which is assumed to be in request
884 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
885 * It is returned otherwise.
886 */
887static char *
888http_get_path(struct http_txn *txn)
889{
890 char *ptr, *end;
891
Willy Tarreau9b28e032012-10-12 23:49:43 +0200892 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 end = ptr + txn->req.sl.rq.u_l;
894
895 if (ptr >= end)
896 return NULL;
897
898 /* RFC2616, par. 5.1.2 :
899 * Request-URI = "*" | absuri | abspath | authority
900 */
901
902 if (*ptr == '*')
903 return NULL;
904
905 if (isalpha((unsigned char)*ptr)) {
906 /* this is a scheme as described by RFC3986, par. 3.1 */
907 ptr++;
908 while (ptr < end &&
909 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
910 ptr++;
911 /* skip '://' */
912 if (ptr == end || *ptr++ != ':')
913 return NULL;
914 if (ptr == end || *ptr++ != '/')
915 return NULL;
916 if (ptr == end || *ptr++ != '/')
917 return NULL;
918 }
919 /* skip [user[:passwd]@]host[:[port]] */
920
921 while (ptr < end && *ptr != '/')
922 ptr++;
923
924 if (ptr == end)
925 return NULL;
926
927 /* OK, we got the '/' ! */
928 return ptr;
929}
930
William Lallemand65ad6e12014-01-31 15:08:02 +0100931/* Parse the URI from the given string and look for the "/" beginning the PATH.
932 * If not found, return NULL. It is returned otherwise.
933 */
934static char *
935http_get_path_from_string(char *str)
936{
937 char *ptr = str;
938
939 /* RFC2616, par. 5.1.2 :
940 * Request-URI = "*" | absuri | abspath | authority
941 */
942
943 if (*ptr == '*')
944 return NULL;
945
946 if (isalpha((unsigned char)*ptr)) {
947 /* this is a scheme as described by RFC3986, par. 3.1 */
948 ptr++;
949 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
950 ptr++;
951 /* skip '://' */
952 if (*ptr == '\0' || *ptr++ != ':')
953 return NULL;
954 if (*ptr == '\0' || *ptr++ != '/')
955 return NULL;
956 if (*ptr == '\0' || *ptr++ != '/')
957 return NULL;
958 }
959 /* skip [user[:passwd]@]host[:[port]] */
960
961 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
962 ptr++;
963
964 if (*ptr == '\0' || *ptr == ' ')
965 return NULL;
966
967 /* OK, we got the '/' ! */
968 return ptr;
969}
970
Willy Tarreau71241ab2012-12-27 11:30:54 +0100971/* Returns a 302 for a redirectable request that reaches a server working in
972 * in redirect mode. This may only be called just after the stream interface
973 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
974 * follow normal proxy processing. NOTE: this function is designed to support
975 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100977void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978{
979 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200982 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 trash.len = strlen(HTTP_302);
986 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100988 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100989
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100991 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992 return;
993
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100994 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100996 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
997 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100998 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001000 /* 3: add the request URI. Since it was already forwarded, we need
1001 * to temporarily rewind the buffer.
1002 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 txn = &s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001004 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001005
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001007 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001009 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001010
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 if (!path)
1012 return;
1013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001014 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 return;
1016
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 memcpy(trash.str + trash.len, path, len);
1018 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001019
1020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001021 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1022 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001023 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001024 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1025 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001026 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001027
1028 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->state = SI_ST_CLO;
1033
1034 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +02001035 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001036
1037 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001038 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001039 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040}
1041
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001043 * that the server side is closed. Note that err_type is actually a
1044 * bitmask, where almost only aborts may be cumulated with other
1045 * values. We consider that aborted operations are more important
1046 * than timeouts or errors due to the fact that nobody else in the
1047 * logs might explain incomplete retries. All others should avoid
1048 * being cumulated. It should normally not be possible to have multiple
1049 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001050 * Note that connection errors appearing on the second request of a keep-alive
1051 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001053void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001054{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001058 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001059 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001061 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001062 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1063 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001064 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001065 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001066 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001068 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001069 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001071 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001072 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1073 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001075 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001076 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001077 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001078 else if (err_type & SI_ET_CONN_RES)
1079 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001080 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1081 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001083 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001084 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085}
1086
Willy Tarreau42250582007-04-01 01:30:43 +02001087extern const char sess_term_cond[8];
1088extern const char sess_fin_state[8];
1089extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001090struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001091struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001092struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Willy Tarreau117f59e2007-03-04 18:17:17 +01001094/*
1095 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001096 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001097 */
1098void capture_headers(char *som, struct hdr_idx *idx,
1099 char **cap, struct cap_hdr *cap_hdr)
1100{
1101 char *eol, *sol, *col, *sov;
1102 int cur_idx;
1103 struct cap_hdr *h;
1104 int len;
1105
1106 sol = som + hdr_idx_first_pos(idx);
1107 cur_idx = hdr_idx_first_idx(idx);
1108
1109 while (cur_idx) {
1110 eol = sol + idx->v[cur_idx].len;
1111
1112 col = sol;
1113 while (col < eol && *col != ':')
1114 col++;
1115
1116 sov = col + 1;
1117 while (sov < eol && http_is_lws[(unsigned char)*sov])
1118 sov++;
1119
1120 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001121 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001122 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1123 if (cap[h->index] == NULL)
1124 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001125 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001126
1127 if (cap[h->index] == NULL) {
1128 Alert("HTTP capture : out of memory.\n");
1129 continue;
1130 }
1131
1132 len = eol - sov;
1133 if (len > h->len)
1134 len = h->len;
1135
1136 memcpy(cap[h->index], sov, len);
1137 cap[h->index][len]=0;
1138 }
1139 }
1140 sol = eol + idx->v[cur_idx].cr + 1;
1141 cur_idx = idx->v[cur_idx].next;
1142 }
1143}
1144
1145
Willy Tarreau42250582007-04-01 01:30:43 +02001146/* either we find an LF at <ptr> or we jump to <bad>.
1147 */
1148#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1149
1150/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1151 * otherwise to <http_msg_ood> with <state> set to <st>.
1152 */
1153#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1154 ptr++; \
1155 if (likely(ptr < end)) \
1156 goto good; \
1157 else { \
1158 state = (st); \
1159 goto http_msg_ood; \
1160 } \
1161 } while (0)
1162
1163
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001165 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1167 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1168 * will give undefined results.
1169 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1170 * and that msg->sol points to the beginning of the response.
1171 * If a complete line is found (which implies that at least one CR or LF is
1172 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1173 * returned indicating an incomplete line (which does not mean that parts have
1174 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1175 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1176 * upon next call.
1177 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001178 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1180 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001181 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001183const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001184 enum ht_state state, const char *ptr, const char *end,
1185 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001188
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001192 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1194
1195 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001196 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1198 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001199 state = HTTP_MSG_ERROR;
1200 break;
1201
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001203 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001205 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 goto http_msg_rpcode;
1207 }
1208 if (likely(HTTP_IS_SPHT(*ptr)))
1209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001215 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 if (likely(!HTTP_IS_LWS(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1218
1219 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1222 }
1223
1224 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 http_msg_rsp_reason:
1227 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001228 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 msg->sl.st.r_l = 0;
1230 goto http_msg_rpline_eol;
1231
Willy Tarreau8973c702007-01-21 23:58:29 +01001232 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 goto http_msg_rpreason;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1240 /* so it's a CR/LF, so there is no reason phrase */
1241 goto http_msg_rsp_reason;
1242
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (likely(!HTTP_IS_CRLF(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 http_msg_rpline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the response would not be
1252 * complete. We can then record the response length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 return ptr;
1257
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001259#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1261 exit(1);
1262#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001263 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 }
1265
1266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001273}
1274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 * This function parses a request line between <ptr> and <end>, starting with
1277 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1278 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the request.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (likely(HTTP_IS_TOKEN(*ptr)))
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_CRLF(*ptr))) {
1312 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001313 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sl.rq.v_l = 0;
1321 goto http_msg_rqline_eol;
1322 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
1325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001329 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rquri;
1331 }
1332 if (likely(HTTP_IS_SPHT(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1334 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1335 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001339 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001343 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001347 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001348 /* non-ASCII chars are forbidden unless option
1349 * accept-invalid-http-request is enabled in the frontend.
1350 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001351 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001352 if (msg->err_pos < -1)
1353 goto invalid_char;
1354 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001356 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1357 }
1358
1359 if (likely(HTTP_IS_CRLF(*ptr))) {
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri_e;
1362 }
1363
1364 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001365 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 state = HTTP_MSG_ERROR;
1368 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001373 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 goto http_msg_rqver;
1375 }
1376 if (likely(HTTP_IS_SPHT(*ptr)))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1378 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1379 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001383 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385
1386 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001387 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001388 http_msg_rqline_eol:
1389 /* We have seen the end of line. Note that we do not
1390 * necessarily have the \n yet, but at least we know that we
1391 * have EITHER \r OR \n, otherwise the request would not be
1392 * complete. We can then record the request length and return
1393 * to the caller which will be able to register it.
1394 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001395 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001396 return ptr;
1397 }
1398
1399 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001400 state = HTTP_MSG_ERROR;
1401 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001404#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
1407#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001408 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001412 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (ret_state)
1414 *ret_state = state;
1415 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001420/*
1421 * Returns the data from Authorization header. Function may be called more
1422 * than once so data is stored in txn->auth_data. When no header is found
1423 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001424 * searching again for something we are unable to find anyway. However, if
1425 * the result if valid, the cache is not reused because we would risk to
1426 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001427 */
1428
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001429/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1430 * set according to global.tune.bufsize.
1431 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001432char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001433
1434int
1435get_http_auth(struct session *s)
1436{
1437
1438 struct http_txn *txn = &s->txn;
1439 struct chunk auth_method;
1440 struct hdr_ctx ctx;
1441 char *h, *p;
1442 int len;
1443
1444#ifdef DEBUG_AUTH
1445 printf("Auth for session %p: %d\n", s, txn->auth.method);
1446#endif
1447
1448 if (txn->auth.method == HTTP_AUTH_WRONG)
1449 return 0;
1450
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001451 txn->auth.method = HTTP_AUTH_WRONG;
1452
1453 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001454
1455 if (txn->flags & TX_USE_PX_CONN) {
1456 h = "Proxy-Authorization";
1457 len = strlen(h);
1458 } else {
1459 h = "Authorization";
1460 len = strlen(h);
1461 }
1462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001463 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001464 return 0;
1465
1466 h = ctx.line + ctx.val;
1467
1468 p = memchr(h, ' ', ctx.vlen);
1469 if (!p || p == h)
1470 return 0;
1471
1472 chunk_initlen(&auth_method, h, 0, p-h);
1473 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1474
1475 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1476
1477 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001478 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001479
1480 if (len < 0)
1481 return 0;
1482
1483
1484 get_http_auth_buff[len] = '\0';
1485
1486 p = strchr(get_http_auth_buff, ':');
1487
1488 if (!p)
1489 return 0;
1490
1491 txn->auth.user = get_http_auth_buff;
1492 *p = '\0';
1493 txn->auth.pass = p+1;
1494
1495 txn->auth.method = HTTP_AUTH_BASIC;
1496 return 1;
1497 }
1498
1499 return 0;
1500}
1501
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503/*
1504 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001505 * depending on the initial msg->msg_state. The caller is responsible for
1506 * ensuring that the message does not wrap. The function can be preempted
1507 * everywhere when data are missing and recalled at the exact same location
1508 * with no information loss. The message may even be realigned between two
1509 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001510 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001511 * fields. Note that msg->sol will be initialized after completing the first
1512 * state, so that none of the msg pointers has to be initialized prior to the
1513 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001515void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516{
Willy Tarreau3770f232013-12-07 00:01:53 +01001517 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001522 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 ptr = buf->p + msg->next;
1524 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(ptr >= end))
1527 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /*
1531 * First, states that are specific to the response only.
1532 * We check them first so that request and headers are
1533 * closer to each other (accessed more often).
1534 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 hdr_idx_init(idx);
1551 state = HTTP_MSG_RPVER;
1552 goto http_msg_rpver;
1553 }
1554
1555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
1557
1558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1561 /* stop here */
1562
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1567 /* stop here */
1568
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER_SP:
1572 case HTTP_MSG_RPCODE:
1573 case HTTP_MSG_RPCODE_SP:
1574 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 if (unlikely(!ptr))
1579 return;
1580
1581 /* we have a full response and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1589 goto http_msg_rpline_end;
1590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* msg->sol must point to the first of CR or LF. */
1594 EXPECT_LF_HERE(ptr, http_msg_invalid);
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1596 /* stop here */
1597
1598 /*
1599 * Second, states that are specific to the request only
1600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001602 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001604 /* we have a start of message, but we have to check
1605 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001606 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 if (likely(ptr != buf->p)) {
1609 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001611 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001612 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 }
Willy Tarreau26927362012-05-18 23:22:52 +02001614 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001615 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 state = HTTP_MSG_RQMETH;
1617 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1621 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(*ptr == '\n'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1625 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH_SP:
1637 case HTTP_MSG_RQURI:
1638 case HTTP_MSG_RQURI_SP:
1639 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001640 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001641 state, ptr, end,
1642 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(!ptr))
1644 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* we have a full request and we know that we have either a CR
1647 * or an LF at <ptr>.
1648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 /* check for HTTP/0.9 request : no version information available.
1659 * msg->sol must point to the first of CR or LF.
1660 */
1661 if (unlikely(msg->sl.rq.v_l == 0))
1662 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /*
1669 * Common states below
1670 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 if (likely(!HTTP_IS_CRLF(*ptr))) {
1675 goto http_msg_hdr_name;
1676 }
1677
1678 if (likely(*ptr == '\r'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1680 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 /* assumes msg->sol points to the first char */
1685 if (likely(HTTP_IS_TOKEN(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001688 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001691 if (likely(msg->err_pos < -1) || *ptr == '\n')
1692 goto http_msg_invalid;
1693
1694 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001696
1697 /* and we still accept this non-token character */
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001701 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_SPHT(*ptr)))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_val;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1715 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 EXPECT_LF_HERE(ptr, http_msg_invalid);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr))) {
1725 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 for (; buf->p + msg->sov < ptr; msg->sov++)
1727 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto http_msg_hdr_l1_sp;
1729 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001730 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001731 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 goto http_msg_complete_header;
1733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001736 /* assumes msg->sol points to the first char, and msg->sov
1737 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 */
1739 if (likely(!HTTP_IS_CRLF(*ptr)))
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* Note: we could also copy eol into ->eoh so that we have the
1744 * real header end in case it ends with lots of LWS, but is this
1745 * really needed ?
1746 */
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1749 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 EXPECT_LF_HERE(ptr, http_msg_invalid);
1754 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1759 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001760 for (; buf->p + msg->eol < ptr; msg->eol++)
1761 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 goto http_msg_hdr_val;
1763 }
1764 http_msg_complete_header:
1765 /*
1766 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001767 * Assumes msg->sol points to the first char, msg->sov points
1768 * to the first character of the value and msg->eol to the
1769 * first CR or LF so we know how the line ends. We insert last
1770 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 idx, idx->tail) < 0))
1774 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001775
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001787 /* Assumes msg->sol points to the first of either CR or LF.
1788 * Sets ->sov and ->next to the total header length, ->eoh to
1789 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EXPECT_LF_HERE(ptr, http_msg_invalid);
1792 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001794 msg->eoh = msg->sol;
1795 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001796 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001797 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001799
1800 case HTTP_MSG_ERROR:
1801 /* this may only happen if we call http_msg_analyser() twice with an error */
1802 break;
1803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001805#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1807 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001809 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
1811 http_msg_ood:
1812 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001813 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 http_msg_invalid:
1818 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001819 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 return;
1822}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001823
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1825 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1826 * nothing is done and 1 is returned.
1827 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001828static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001829{
1830 int delta;
1831 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001832 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001833
1834 if (msg->sl.rq.v_l != 0)
1835 return 1;
1836
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001837 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1838 if (txn->meth != HTTP_METH_GET)
1839 return 0;
1840
Willy Tarreau9b28e032012-10-12 23:49:43 +02001841 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001842 delta = 0;
1843
1844 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001845 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1846 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001847 }
1848 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001850 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001851 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001852 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001854 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 NULL, NULL);
1856 if (unlikely(!cur_end))
1857 return 0;
1858
1859 /* we have a full HTTP/1.0 request now and we know that
1860 * we have either a CR or an LF at <ptr>.
1861 */
1862 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1863 return 1;
1864}
1865
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001867 * and "keep-alive" values. If we already know that some headers may safely
1868 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1870 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001871 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1873 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1874 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001878{
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 const char *hdr_val = "Connection";
1881 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 return;
1885
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1887 hdr_val = "Proxy-Connection";
1888 hdr_len = 16;
1889 }
1890
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001893 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1895 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 if (to_del & 2)
1897 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else
1899 txn->flags |= TX_CON_KAL_SET;
1900 }
1901 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1902 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 if (to_del & 1)
1904 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 else
1906 txn->flags |= TX_CON_CLO_SET;
1907 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001908 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1909 txn->flags |= TX_HDR_CONN_UPG;
1910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 }
1912
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags |= TX_HDR_CONN_PRS;
1914 return;
1915}
Willy Tarreau5b154472009-12-21 20:11:07 +01001916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001917/* Apply desired changes on the Connection: header. Values may be removed and/or
1918 * added depending on the <wanted> flags, which are exclusively composed of
1919 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1920 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1921 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001922void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001923{
1924 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001925 const char *hdr_val = "Connection";
1926 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001927
1928 ctx.idx = 0;
1929
Willy Tarreau88d349d2010-01-25 12:15:43 +01001930
1931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1932 hdr_val = "Proxy-Connection";
1933 hdr_len = 16;
1934 }
1935
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001936 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001937 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1939 if (wanted & TX_CON_KAL_SET)
1940 txn->flags |= TX_CON_KAL_SET;
1941 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1945 if (wanted & TX_CON_CLO_SET)
1946 txn->flags |= TX_CON_CLO_SET;
1947 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001948 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001949 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001950 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951
1952 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1953 return;
1954
1955 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1956 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 hdr_val = "Connection: close";
1958 hdr_len = 17;
1959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1960 hdr_val = "Proxy-Connection: close";
1961 hdr_len = 23;
1962 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 }
1965
1966 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1967 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001968 hdr_val = "Connection: keep-alive";
1969 hdr_len = 22;
1970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection: keep-alive";
1972 hdr_len = 28;
1973 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001974 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 }
1976 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001977}
1978
Willy Tarreauc24715e2014-04-17 15:21:20 +02001979/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1980 * the first byte of data after the chunk size, so that we know we can forward
1981 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1982 * the chunk size. That way it is always possible to differentiate between the
1983 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001987static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001988{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001989 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001991 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *end = buf->data + buf->size;
1993 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 unsigned int chunk = 0;
1995
1996 /* The chunk size is in the following form, though we are only
1997 * interested in the size and CRLF :
1998 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1999 */
2000 while (1) {
2001 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 if (c < 0) /* not a hex digit anymore */
2006 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002009 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002011 chunk = (chunk << 4) + c;
2012 }
2013
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002015 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017
2018 while (http_is_spht[(unsigned char)*ptr]) {
2019 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002021 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 }
2024
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 /* Up to there, we know that at least one byte is present at *ptr. Check
2026 * for the end of chunk size.
2027 */
2028 while (1) {
2029 if (likely(HTTP_IS_CRLF(*ptr))) {
2030 /* we now have a CR or an LF at ptr */
2031 if (likely(*ptr == '\r')) {
2032 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002037
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002039 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 /* done */
2043 break;
2044 }
2045 else if (*ptr == ';') {
2046 /* chunk extension, ends at next CRLF */
2047 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 while (!HTTP_IS_CRLF(*ptr)) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
2058 /* we have a CRLF now, loop above */
2059 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 }
2064
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002066 * which may or may not be present. We save that into ->next,
2067 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002069 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002070 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002073 msg->chunk_len = chunk;
2074 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002077 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080}
2081
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002082/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 * the trailers is found, it is automatically scheduled to be forwarded,
2085 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2086 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * except maybe msg->next if it could parse some lines, and returns zero.
2088 * If a parse error is encountered, the function returns < 0 and does not
2089 * change anything except maybe msg->next. Note that the message must
2090 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002091 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 * forwarding, and that msg->next exactly matches the length of trailers
2093 * already parsed and not forwarded. It is also important to note that this
2094 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002096static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002098 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002099
Willy Tarreaua458b672012-03-05 11:17:50 +01002100 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *ptr = b_ptr(buf, msg->next);
2104 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002105 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106
2107 /* scan current line and stop at LF or CRLF */
2108 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002109 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 return 0;
2111
2112 if (*ptr == '\n') {
2113 if (!p1)
2114 p1 = ptr;
2115 p2 = ptr;
2116 break;
2117 }
2118
2119 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 p1 = ptr;
2125 }
2126
2127 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 if (ptr >= buf->data + buf->size)
2129 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 }
2131
2132 /* after LF; point to beginning of next line */
2133 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (p2 >= buf->data + buf->size)
2135 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002137 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002138 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002140
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 /* LF/CRLF at beginning of line => end of trailers at p2.
2143 * Everything was scheduled for forwarding, there's nothing
2144 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_DONE;
2148 return 1;
2149 }
2150 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 }
2153}
2154
Willy Tarreauc24715e2014-04-17 15:21:20 +02002155/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2156 * or a possible LF alone at the end of a chunk. It automatically adjusts
2157 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002158 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2160 * not enough data are available, the function does not change anything and
2161 * returns zero. If a parse error is encountered, the function returns < 0 and
2162 * does not change anything. Note: this function is designed to parse wrapped
2163 * CRLF at the end of the buffer.
2164 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002165static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002167 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002168 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169 int bytes;
2170
2171 /* NB: we'll check data availabilty at the end. It's not a
2172 * problem because whatever we match first will be checked
2173 * against the correct length.
2174 */
2175 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002176 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 if (*ptr == '\r') {
2178 bytes++;
2179 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 if (ptr >= buf->data + buf->size)
2181 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 }
2183
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002184 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002185 return 0;
2186
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002187 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002188 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191
2192 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002193 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002194 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2198 return 1;
2199}
Willy Tarreau5b154472009-12-21 20:11:07 +01002200
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002201/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2202 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2203 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2204 * Unparsable qvalues return 1000 as "q=1.000".
2205 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002206int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207{
2208 int q = 1000;
2209
Willy Tarreau506c69a2014-07-08 00:59:48 +02002210 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002211 goto out;
2212 q = (*qvalue++ - '0') * 1000;
2213
2214 if (*qvalue++ != '.')
2215 goto out;
2216
Willy Tarreau506c69a2014-07-08 00:59:48 +02002217 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002218 goto out;
2219 q += (*qvalue++ - '0') * 100;
2220
Willy Tarreau506c69a2014-07-08 00:59:48 +02002221 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 goto out;
2223 q += (*qvalue++ - '0') * 10;
2224
Willy Tarreau506c69a2014-07-08 00:59:48 +02002225 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 goto out;
2227 q += (*qvalue++ - '0') * 1;
2228 out:
2229 if (q > 1000)
2230 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002231 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002232 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 return q;
2234}
William Lallemand82fe75c2012-10-23 10:25:10 +02002235
2236/*
2237 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002238 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002239int select_compression_request_header(struct session *s, struct buffer *req)
2240{
2241 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 struct hdr_ctx ctx;
2244 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002245 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002247 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2248 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002249 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2250 */
2251 ctx.idx = 0;
2252 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2253 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002254 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2255 (ctx.vlen < 31 ||
2256 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2257 ctx.line[ctx.val + 30] < '6' ||
2258 (ctx.line[ctx.val + 30] == '6' &&
2259 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2260 s->comp_algo = NULL;
2261 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002262 }
2263
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 int best_q = 0;
2267
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 ctx.idx = 0;
2269 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002270 const char *qval;
2271 int q;
2272 int toklen;
2273
2274 /* try to isolate the token from the optional q-value */
2275 toklen = 0;
2276 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2277 toklen++;
2278
2279 qval = ctx.line + ctx.val + toklen;
2280 while (1) {
2281 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2282 qval++;
2283
2284 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2285 qval = NULL;
2286 break;
2287 }
2288 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002289
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2291 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002292
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002293 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2294 qval = NULL;
2295 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002297 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2298 break;
2299
2300 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2301 qval++;
2302 }
2303
2304 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002305 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306
2307 if (q <= best_q)
2308 continue;
2309
2310 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2311 if (*(ctx.line + ctx.val) == '*' ||
2312 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2313 s->comp_algo = comp_algo;
2314 best_q = q;
2315 break;
2316 }
2317 }
2318 }
2319 }
2320
2321 /* remove all occurrences of the header when "compression offload" is set */
2322 if (s->comp_algo) {
2323 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2324 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2325 ctx.idx = 0;
2326 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2327 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 }
2329 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002330 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
2332
2333 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2335 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 if (comp_algo->add_data == identity_add_data) {
2337 s->comp_algo = comp_algo;
2338 return 1;
2339 }
2340 }
2341 }
2342
2343 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 return 0;
2345}
2346
2347/*
2348 * Selects a comression algorithm depending of the server response.
2349 */
2350int select_compression_response_header(struct session *s, struct buffer *res)
2351{
2352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->rsp;
2354 struct hdr_ctx ctx;
2355 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002356
2357 /* no common compression algorithm was found in request header */
2358 if (s->comp_algo == NULL)
2359 goto fail;
2360
2361 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002362 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 goto fail;
2364
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002365 /* compress 200,201,202,203 responses only */
2366 if ((txn->status != 200) &&
2367 (txn->status != 201) &&
2368 (txn->status != 202) &&
2369 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002370 goto fail;
2371
William Lallemand82fe75c2012-10-23 10:25:10 +02002372 /* Content-Length is null */
2373 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2374 goto fail;
2375
2376 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002377 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002378 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2379 goto fail;
2380
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002381 /* no compression when Cache-Control: no-transform is present in the message */
2382 ctx.idx = 0;
2383 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2384 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2385 goto fail;
2386 }
2387
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 comp_type = NULL;
2389
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002390 /* we don't want to compress multipart content-types, nor content-types that are
2391 * not listed in the "compression type" directive if any. If no content-type was
2392 * found but configuration requires one, we don't compress either. Backend has
2393 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002394 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002395 ctx.idx = 0;
2396 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2397 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2398 goto fail;
2399
2400 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2401 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002403 if (ctx.vlen >= comp_type->name_len &&
2404 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002405 /* this Content-Type should be compressed */
2406 break;
2407 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002408 /* this Content-Type should not be compressed */
2409 if (comp_type == NULL)
2410 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002411 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002412 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002413 else { /* no content-type header */
2414 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2415 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002416 }
2417
William Lallemandd85f9172012-11-09 17:05:39 +01002418 /* limit compression rate */
2419 if (global.comp_rate_lim > 0)
2420 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2421 goto fail;
2422
William Lallemand072a2bf2012-11-20 17:01:01 +01002423 /* limit cpu usage */
2424 if (idle_pct < compress_min_idle)
2425 goto fail;
2426
William Lallemand4c49fae2012-11-07 15:00:23 +01002427 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002428 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002429 goto fail;
2430
William Lallemandec3e3892012-11-12 17:02:18 +01002431 s->flags |= SN_COMP_READY;
2432
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002434 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002435 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2436 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2437
2438 /* add Transfer-Encoding header */
2439 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2440 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2441
2442 /*
2443 * Add Content-Encoding header when it's not identity encoding.
2444 * RFC 2616 : Identity encoding: This content-coding is used only in the
2445 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2446 * header.
2447 */
2448 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002449 trash.len = 18;
2450 memcpy(trash.str, "Content-Encoding: ", trash.len);
2451 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2452 trash.len += s->comp_algo->name_len;
2453 trash.str[trash.len] = '\0';
2454 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002456 return 1;
2457
2458fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002459 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002460 return 0;
2461}
2462
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002463void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2464{
2465 int tmp = TX_CON_WANT_KAL;
2466
2467 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2468 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2469 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2470 tmp = TX_CON_WANT_TUN;
2471
2472 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2473 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2474 tmp = TX_CON_WANT_TUN;
2475 }
2476
2477 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2479 /* option httpclose + server_close => forceclose */
2480 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2482 tmp = TX_CON_WANT_CLO;
2483 else
2484 tmp = TX_CON_WANT_SCL;
2485 }
2486
2487 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2488 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2489 tmp = TX_CON_WANT_CLO;
2490
2491 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2492 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2493
2494 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2495 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2496 /* parse the Connection header and possibly clean it */
2497 int to_del = 0;
2498 if ((msg->flags & HTTP_MSGF_VER_11) ||
2499 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2500 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2501 to_del |= 2; /* remove "keep-alive" */
2502 if (!(msg->flags & HTTP_MSGF_VER_11))
2503 to_del |= 1; /* remove "close" */
2504 http_parse_connection_header(txn, msg, to_del);
2505 }
2506
2507 /* check if client or config asks for explicit close in KAL/SCL */
2508 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2509 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2510 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2511 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2512 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2513 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2514 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2515}
William Lallemand82fe75c2012-10-23 10:25:10 +02002516
Willy Tarreaud787e662009-07-07 10:14:51 +02002517/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2518 * processing can continue on next analysers, or zero if it either needs more
2519 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002520 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 * when it has nothing left to do, and may remove any analyser when it wants to
2522 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002523 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002524int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002525{
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 /*
2527 * We will parse the partial (or complete) lines.
2528 * We will check the request syntax, and also join multi-line
2529 * headers. An index of all the lines will be elaborated while
2530 * parsing.
2531 *
2532 * For the parsing, we use a 28 states FSM.
2533 *
2534 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002535 * req->buf->p = beginning of request
2536 * req->buf->p + msg->eoh = end of processed headers / start of current one
2537 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002538 * msg->eol = end of current header or line (LF or CRLF)
2539 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 *
2541 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002542 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2544 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002546
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002548 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 struct http_txn *txn = &s->txn;
2550 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002551 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002552
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002553 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002554 now_ms, __FUNCTION__,
2555 s,
2556 req,
2557 req->rex, req->wex,
2558 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002559 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002560 req->analysers);
2561
Willy Tarreau52a0c602009-08-16 22:45:38 +02002562 /* we're speaking HTTP here, so let's speak HTTP to the client */
2563 s->srv_error = http_return_srv_error;
2564
Willy Tarreau83e3af02009-12-28 17:39:57 +01002565 /* There's a protected area at the end of the buffer for rewriting
2566 * purposes. We don't want to start to parse the request if the
2567 * protected area is affected, because we may have to move processed
2568 * data later, which is much more complicated.
2569 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002570 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002571 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002572 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002573 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002574 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002575 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002576 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002577 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002578 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002579 return 0;
2580 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002581 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2582 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2583 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002584 }
2585
Willy Tarreau065e8332010-01-08 00:30:20 +01002586 /* Note that we have the same problem with the response ; we
2587 * may want to send a redirect, error or anything which requires
2588 * some spare space. So we'll ensure that we have at least
2589 * maxrewrite bytes available in the response buffer before
2590 * processing that one. This will only affect pipelined
2591 * keep-alive requests.
2592 */
2593 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002594 unlikely(!channel_is_rewritable(&s->res) ||
2595 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2596 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2597 if (s->res.buf->o) {
2598 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002599 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002600 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002601 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002602 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2603 s->res.flags |= CF_WAKE_WRITE;
2604 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002605 return 0;
2606 }
2607 }
2608
Willy Tarreau9b28e032012-10-12 23:49:43 +02002609 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002610 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002611 }
2612
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 /* 1: we might have to print this header in debug mode */
2614 if (unlikely((global.mode & MODE_DEBUG) &&
2615 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002616 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002618
Willy Tarreau9b28e032012-10-12 23:49:43 +02002619 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002620 /* this is a bit complex : in case of error on the request line,
2621 * we know that rq.l is still zero, so we display only the part
2622 * up to the end of the line (truncated by debug_hdr).
2623 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002624 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 sol += hdr_idx_first_pos(&txn->hdr_idx);
2628 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002629
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 while (cur_idx) {
2631 eol = sol + txn->hdr_idx.v[cur_idx].len;
2632 debug_hdr("clihdr", s, sol, eol);
2633 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2634 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
2637
Willy Tarreau58f10d72006-12-04 02:26:12 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /*
2640 * Now we quickly check if we have found a full valid request.
2641 * If not so, we check the FD and buffer states before leaving.
2642 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002643 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002644 * requests are checked first. When waiting for a second request
2645 * on a keep-alive session, if we encounter and error, close, t/o,
2646 * we note the error in the session flags but don't set any state.
2647 * Since the error will be noted there, it will not be counted by
2648 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002649 * Last, we may increase some tracked counters' http request errors on
2650 * the cases that are deliberately the client's fault. For instance,
2651 * a timeout or connection reset is not counted as an error. However
2652 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002654
Willy Tarreau655dce92009-11-08 13:10:58 +01002655 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002656 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002658 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002660 session_inc_http_req_ctr(s);
2661 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002662 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002664 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002665
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 /* 1: Since we are in header mode, if there's no space
2667 * left for headers, we won't be able to free more
2668 * later, so the session will never terminate. We
2669 * must terminate it now.
2670 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 /* FIXME: check if URI is set and return Status
2673 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002674 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002675 session_inc_http_req_ctr(s);
2676 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002677 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002678 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002679 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 goto return_bad_req;
2681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002684 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002685 if (!(s->flags & SN_ERR_MASK))
2686 s->flags |= SN_ERR_CLICL;
2687
Willy Tarreaufcffa692010-01-10 14:21:19 +01002688 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002689 goto failed_keep_alive;
2690
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002692 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002693 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 session_inc_http_err_ctr(s);
2695 }
2696
Willy Tarreaudc979f22012-12-04 10:39:01 +01002697 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002698 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002699 msg->msg_state = HTTP_MSG_ERROR;
2700 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002701
Willy Tarreauda7ff642010-06-23 11:44:09 +02002702 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002703 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002704 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002705 if (s->listener->counters)
2706 s->listener->counters->failed_req++;
2707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 if (!(s->flags & SN_FINST_MASK))
2709 s->flags |= SN_FINST_R;
2710 return 0;
2711 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002714 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002715 if (!(s->flags & SN_ERR_MASK))
2716 s->flags |= SN_ERR_CLITO;
2717
Willy Tarreaufcffa692010-01-10 14:21:19 +01002718 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002719 goto failed_keep_alive;
2720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002722 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002723 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002724 session_inc_http_err_ctr(s);
2725 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002727 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 msg->msg_state = HTTP_MSG_ERROR;
2729 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002730
Willy Tarreauda7ff642010-06-23 11:44:09 +02002731 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002732 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002733 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002734 if (s->listener->counters)
2735 s->listener->counters->failed_req++;
2736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 if (!(s->flags & SN_FINST_MASK))
2738 s->flags |= SN_FINST_R;
2739 return 0;
2740 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002743 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002744 if (!(s->flags & SN_ERR_MASK))
2745 s->flags |= SN_ERR_CLICL;
2746
Willy Tarreaufcffa692010-01-10 14:21:19 +01002747 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002748 goto failed_keep_alive;
2749
Willy Tarreau4076a152009-04-02 15:18:36 +02002750 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002751 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002753 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 msg->msg_state = HTTP_MSG_ERROR;
2755 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002756
Willy Tarreauda7ff642010-06-23 11:44:09 +02002757 session_inc_http_err_ctr(s);
2758 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002759 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002760 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002761 if (s->listener->counters)
2762 s->listener->counters->failed_req++;
2763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 if (!(s->flags & SN_FINST_MASK))
2765 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002766 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002767 }
2768
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002769 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002770 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002771 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002772#ifdef TCP_QUICKACK
Willy Tarreau350f4872014-11-28 14:42:25 +01002773 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->si[0].end) && conn_ctrl_ready(__objt_conn(s->si[0].end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002774 /* We need more data, we have to re-enable quick-ack in case we
2775 * previously disabled it, otherwise we might cause the client
2776 * to delay next data.
2777 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002778 setsockopt(__objt_conn(s->si[0].end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002779 }
2780#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002781
Willy Tarreaufcffa692010-01-10 14:21:19 +01002782 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2783 /* If the client starts to talk, let's fall back to
2784 * request timeout processing.
2785 */
2786 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002787 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002788 }
2789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002791 if (!tick_isset(req->analyse_exp)) {
2792 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2793 (txn->flags & TX_WAIT_NEXT_RQ) &&
2794 tick_isset(s->be->timeout.httpka))
2795 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2796 else
2797 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2798 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* we're not ready yet */
2801 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002802
2803 failed_keep_alive:
2804 /* Here we process low-level errors for keep-alive requests. In
2805 * short, if the request is not the first one and it experiences
2806 * a timeout, read error or shutdown, we just silently close so
2807 * that the client can try again.
2808 */
2809 txn->status = 0;
2810 msg->msg_state = HTTP_MSG_RQBEFORE;
2811 req->analysers = 0;
2812 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002813 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002814 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002815 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002816 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002818
Willy Tarreaud787e662009-07-07 10:14:51 +02002819 /* OK now we have a complete HTTP request with indexed headers. Let's
2820 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002821 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002822 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002823 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002824 * byte after the last LF. msg->sov points to the first byte of data.
2825 * msg->eol cannot be trusted because it may have been left uninitialized
2826 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002827 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002828
Willy Tarreauda7ff642010-06-23 11:44:09 +02002829 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002830 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2831
Willy Tarreaub16a5742010-01-10 14:46:16 +01002832 if (txn->flags & TX_WAIT_NEXT_RQ) {
2833 /* kill the pending keep-alive timeout */
2834 txn->flags &= ~TX_WAIT_NEXT_RQ;
2835 req->analyse_exp = TICK_ETERNITY;
2836 }
2837
2838
Willy Tarreaud787e662009-07-07 10:14:51 +02002839 /* Maybe we found in invalid header name while we were configured not
2840 * to block on that, so we have to capture it now.
2841 */
2842 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002843 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002844
Willy Tarreau59234e92008-11-30 23:51:27 +01002845 /*
2846 * 1: identify the method
2847 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002848 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002849
2850 /* we can make use of server redirect on GET and HEAD */
2851 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2852 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /*
2855 * 2: check if the URI matches the monitor_uri.
2856 * We have to do this for every request which gets in, because
2857 * the monitor-uri is defined by the frontend.
2858 */
2859 if (unlikely((s->fe->monitor_uri_len != 0) &&
2860 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002861 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 s->fe->monitor_uri,
2863 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002864 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002865 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002866 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002870 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002871
Willy Tarreau59234e92008-11-30 23:51:27 +01002872 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002873 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002874 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 ret = acl_pass(ret);
2877 if (cond->pol == ACL_COND_UNLESS)
2878 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002879
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 if (ret) {
2881 /* we fail this request, let's return 503 service unavail */
2882 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002883 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002884 if (!(s->flags & SN_ERR_MASK))
2885 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002886 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002887 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002888 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002889
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 /* nothing to fail, let's reply normaly */
2891 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002892 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002893 if (!(s->flags & SN_ERR_MASK))
2894 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 goto return_prx_cond;
2896 }
2897
2898 /*
2899 * 3: Maybe we have to copy the original REQURI for the logs ?
2900 * Note: we cannot log anymore if the request has been
2901 * classified as invalid.
2902 */
2903 if (unlikely(s->logs.logwait & LW_REQ)) {
2904 /* we have a complete HTTP request that we must log */
2905 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2906 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002907
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 if (urilen >= REQURI_LEN)
2909 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002910 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002912
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002913 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 s->do_log(s);
2915 } else {
2916 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002917 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 }
Willy Tarreau06619262006-12-17 08:37:22 +01002919
Willy Tarreau59234e92008-11-30 23:51:27 +01002920 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002921 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002922 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002923
Willy Tarreau5b154472009-12-21 20:11:07 +01002924 /* ... and check if the request is HTTP/1.1 or above */
2925 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002926 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2927 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2928 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002929 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002930
2931 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002932 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002933
Willy Tarreau88d349d2010-01-25 12:15:43 +01002934 /* if the frontend has "option http-use-proxy-header", we'll check if
2935 * we have what looks like a proxied connection instead of a connection,
2936 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2937 * Note that this is *not* RFC-compliant, however browsers and proxies
2938 * happen to do that despite being non-standard :-(
2939 * We consider that a request not beginning with either '/' or '*' is
2940 * a proxied connection, which covers both "scheme://location" and
2941 * CONNECT ip:port.
2942 */
2943 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002944 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002945 txn->flags |= TX_USE_PX_CONN;
2946
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002947 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002948 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002949
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002951 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002952 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002954
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002955 /* 6: determine the transfer-length.
2956 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2957 * the presence of a message-body in a REQUEST and its transfer length
2958 * must be determined that way (in order of precedence) :
2959 * 1. The presence of a message-body in a request is signaled by the
2960 * inclusion of a Content-Length or Transfer-Encoding header field
2961 * in the request's header fields. When a request message contains
2962 * both a message-body of non-zero length and a method that does
2963 * not define any semantics for that request message-body, then an
2964 * origin server SHOULD either ignore the message-body or respond
2965 * with an appropriate error message (e.g., 413). A proxy or
2966 * gateway, when presented the same request, SHOULD either forward
2967 * the request inbound with the message- body or ignore the
2968 * message-body when determining a response.
2969 *
2970 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2971 * and the "chunked" transfer-coding (Section 6.2) is used, the
2972 * transfer-length is defined by the use of this transfer-coding.
2973 * If a Transfer-Encoding header field is present and the "chunked"
2974 * transfer-coding is not present, the transfer-length is defined
2975 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002976 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002977 * 3. If a Content-Length header field is present, its decimal value in
2978 * OCTETs represents both the entity-length and the transfer-length.
2979 * If a message is received with both a Transfer-Encoding header
2980 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002981 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002982 * 4. By the server closing the connection. (Closing the connection
2983 * cannot be used to indicate the end of a request body, since that
2984 * would leave no possibility for the server to send back a response.)
2985 *
2986 * Whenever a transfer-coding is applied to a message-body, the set of
2987 * transfer-codings MUST include "chunked", unless the message indicates
2988 * it is terminated by closing the connection. When the "chunked"
2989 * transfer-coding is used, it MUST be the last transfer-coding applied
2990 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002991 */
2992
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002994 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002997 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3000 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 /* bad transfer-encoding (chunked followed by something else) */
3002 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003003 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003004 break;
3005 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 }
3007
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003009 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003010 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 signed long long cl;
3012
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003014 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003015 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003016 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003019 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003021 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003025 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003026 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003028 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003029 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003030 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003031 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003033 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003034 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003035 }
3036
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003037 /* bodyless requests have a known length */
3038 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003039 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003040
Willy Tarreau179085c2014-04-28 16:48:56 +02003041 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3042 * only change if both the request and the config reference something else.
3043 * Option httpclose by itself sets tunnel mode where headers are mangled.
3044 * However, if another mode is set, it will affect it (eg: server-close/
3045 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3046 * if FE and BE have the same settings (common). The method consists in
3047 * checking if options changed between the two calls (implying that either
3048 * one is non-null, or one of them is non-null and we are there for the first
3049 * time.
3050 */
3051 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003052 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3053 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003054
Willy Tarreaud787e662009-07-07 10:14:51 +02003055 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003056 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003057 req->analyse_exp = TICK_ETERNITY;
3058 return 1;
3059
3060 return_bad_req:
3061 /* We centralize bad requests processing here */
3062 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3063 /* we detected a parsing error. We want to archive this request
3064 * in the dedicated proxy area for later troubleshooting.
3065 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003066 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003067 }
3068
3069 txn->req.msg_state = HTTP_MSG_ERROR;
3070 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003071 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003073 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003074 if (s->listener->counters)
3075 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003076
3077 return_prx_cond:
3078 if (!(s->flags & SN_ERR_MASK))
3079 s->flags |= SN_ERR_PRXCOND;
3080 if (!(s->flags & SN_FINST_MASK))
3081 s->flags |= SN_FINST_R;
3082
3083 req->analysers = 0;
3084 req->analyse_exp = TICK_ETERNITY;
3085 return 0;
3086}
3087
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003088
Willy Tarreau347a35d2013-11-22 17:51:09 +01003089/* This function prepares an applet to handle the stats. It can deal with the
3090 * "100-continue" expectation, check that admin rules are met for POST requests,
3091 * and program a response message if something was unexpected. It cannot fail
3092 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003093 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 * s->target which is supposed to already point to the stats applet. The caller
3095 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003096 */
3097int http_handle_stats(struct session *s, struct channel *req)
3098{
3099 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003100 struct stream_interface *si = &s->si[1];
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101 struct http_txn *txn = &s->txn;
3102 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103 struct uri_auth *uri_auth = s->be->uri_auth;
3104 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 appctx = si_appctx(si);
3108 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3109 appctx->st1 = appctx->st2 = 0;
3110 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3111 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003112 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3113 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003114
3115 uri = msg->chn->buf->p + msg->sl.rq.u;
3116 lookup = uri + uri_auth->uri_len;
3117
3118 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3119 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003120 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 break;
3122 }
3123 }
3124
3125 if (uri_auth->refresh) {
3126 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3127 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003128 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003129 break;
3130 }
3131 }
3132 }
3133
3134 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3135 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003137 break;
3138 }
3139 }
3140
3141 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3142 if (memcmp(h, ";st=", 4) == 0) {
3143 int i;
3144 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3147 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 break;
3150 }
3151 }
3152 break;
3153 }
3154 }
3155
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003156 appctx->ctx.stats.scope_str = 0;
3157 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003158 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3159 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3160 int itx = 0;
3161 const char *h2;
3162 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3163 const char *err;
3164
3165 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3166 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003167 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003168 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3169 itx++;
3170 h++;
3171 }
3172
3173 if (itx > STAT_SCOPE_TXT_MAXLEN)
3174 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003177 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003178 memcpy(scope_txt, h2, itx);
3179 scope_txt[itx] = '\0';
3180 err = invalid_char(scope_txt);
3181 if (err) {
3182 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003183 appctx->ctx.stats.scope_str = 0;
3184 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003185 }
3186 break;
3187 }
3188 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189
3190 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003191 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003192 int ret = 1;
3193
3194 if (stats_admin_rule->cond) {
3195 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3196 ret = acl_pass(ret);
3197 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3198 ret = !ret;
3199 }
3200
3201 if (ret) {
3202 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003204 break;
3205 }
3206 }
3207
3208 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003209 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003211 /* we'll need the request body, possibly after sending 100-continue */
3212 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003214 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003215 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3217 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003218 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003219 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003220 else {
3221 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003222 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003223 }
3224
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003225 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003226 return 1;
3227}
3228
Lukas Tribus67db8df2013-06-23 17:37:13 +02003229/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3230 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3231 */
3232static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3233{
3234#ifdef IP_TOS
3235 if (from.ss_family == AF_INET)
3236 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3237#endif
3238#ifdef IPV6_TCLASS
3239 if (from.ss_family == AF_INET6) {
3240 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3241 /* v4-mapped addresses need IP_TOS */
3242 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3243 else
3244 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3245 }
3246#endif
3247}
3248
Sasha Pachev218f0642014-06-16 12:05:59 -06003249/* Returns the number of characters written to destination,
3250 * -1 on internal error and -2 if no replacement took place.
3251 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003252static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003253 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003254{
Willy Tarreau15a53a42015-01-21 13:39:42 +01003255 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch, 0))
Sasha Pachev218f0642014-06-16 12:05:59 -06003256 return -2;
3257
3258 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3259}
3260
3261/* Returns the number of characters written to destination,
3262 * -1 on internal error and -2 if no replacement took place.
3263 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003264static 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 +02003265 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003266{
3267 char* p = val;
3268 char* dst_end = dst + dst_size;
3269 char* dst_p = dst;
3270
3271 for (;;) {
3272 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003273
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003274 /* look for delim. */
3275 p_delim = p;
3276 while (p_delim < p + len && *p_delim != delim)
3277 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003278
Willy Tarreau15a53a42015-01-21 13:39:42 +01003279 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch, 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003280 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3281
3282 if (replace_n < 0)
3283 return -1;
3284
3285 dst_p += replace_n;
3286 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003287 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003288
3289 if (dst_p + len >= dst_end)
3290 return -1;
3291
3292 memcpy(dst_p, p, len);
3293 dst_p += len;
3294 }
3295
3296 if (dst_p >= dst_end)
3297 return -1;
3298
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003299 /* end of the replacements. */
3300 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003301 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003302
3303 /* Next part. */
3304 *dst_p++ = delim;
3305 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003306 }
3307
3308 return dst_p - dst;
3309}
3310
3311static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003312 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003313 struct hdr_ctx* ctx, int action)
3314{
3315 ctx->idx = 0;
3316
3317 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3318 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3319 int delta;
Willy Tarreauaa435e72015-01-29 14:01:34 +01003320 char* val = (char*)ctx->line + ctx->val;
Sasha Pachev218f0642014-06-16 12:05:59 -06003321 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003322 char* reg_dst_buf;
3323 uint reg_dst_buf_size;
3324 int n_replaced;
3325
Sasha Pachev218f0642014-06-16 12:05:59 -06003326 trash.len = build_logline(s, trash.str, trash.size, fmt);
3327
3328 if (trash.len >= trash.size - 1)
3329 return -1;
3330
3331 reg_dst_buf = trash.str + trash.len + 1;
3332 reg_dst_buf_size = trash.size - trash.len - 1;
3333
3334 switch (action) {
3335 case HTTP_REQ_ACT_REPLACE_VAL:
3336 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003337 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 -06003338 break;
3339 case HTTP_REQ_ACT_REPLACE_HDR:
3340 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003341 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 -06003342 break;
3343 default: /* impossible */
3344 return -1;
3345 }
3346
Sasha Pachev218f0642014-06-16 12:05:59 -06003347 switch (n_replaced) {
3348 case -1: return -1;
3349 case -2: continue;
3350 }
3351
3352 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3353
3354 hdr->len += delta;
3355 http_msg_move_end(msg, delta);
3356 }
3357
3358 return 0;
3359}
3360
Willy Tarreau20b0de52012-12-24 15:45:22 +01003361/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003362 * transaction <txn>. Returns the verdict of the first rule that prevents
3363 * further processing of the request (auth, deny, ...), and defaults to
3364 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3365 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3366 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003367 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003368enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003369http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003370{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003371 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003372 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003373 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003374 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003375
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003376 /* If "the current_rule_list" match the executed rule list, we are in
3377 * resume condition. If a resume is needed it is always in the action
3378 * and never in the ACL or converters. In this case, we initialise the
3379 * current rule, and go to the action execution point.
3380 */
3381 if (s->current_rule_list == rules) {
3382 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3383 goto resume_execution;
3384 }
3385 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003386 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003387 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003388 continue;
3389
Willy Tarreau96257ec2012-12-27 10:46:37 +01003390 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003391 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 int ret;
3393
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003394 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003395 ret = acl_pass(ret);
3396
Willy Tarreauff011f22011-01-06 17:51:27 +01003397 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003398 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003399
3400 if (!ret) /* condition not matched */
3401 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003402 }
3403
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003404resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003405
Willy Tarreau96257ec2012-12-27 10:46:37 +01003406 switch (rule->action) {
3407 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003408 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003409
3410 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003411 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003412
Willy Tarreauccbcc372012-12-27 12:37:57 +01003413 case HTTP_REQ_ACT_TARPIT:
3414 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003415 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003416
Willy Tarreau96257ec2012-12-27 10:46:37 +01003417 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003418 /* Auth might be performed on regular http-req rules as well as on stats */
3419 auth_realm = rule->arg.auth.realm;
3420 if (!auth_realm) {
3421 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3422 auth_realm = STATS_DEFAULT_REALM;
3423 else
3424 auth_realm = px->id;
3425 }
3426 /* send 401/407 depending on whether we use a proxy or not. We still
3427 * count one error, because normal browsing won't significantly
3428 * increase the counter but brute force attempts will.
3429 */
3430 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3431 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3432 stream_int_retnclose(&s->si[0], &trash);
3433 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003434 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003435
Willy Tarreau81499eb2012-12-27 12:19:02 +01003436 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003437 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3438 return HTTP_RULE_RES_BADREQ;
3439 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003440
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003441 case HTTP_REQ_ACT_SET_NICE:
3442 s->task->nice = rule->arg.nice;
3443 break;
3444
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003445 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003446 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003447 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003448 break;
3449
Willy Tarreau51347ed2013-06-11 19:34:13 +02003450 case HTTP_REQ_ACT_SET_MARK:
3451#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003452 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003453 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003454#endif
3455 break;
3456
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003457 case HTTP_REQ_ACT_SET_LOGL:
3458 s->logs.level = rule->arg.loglevel;
3459 break;
3460
Sasha Pachev218f0642014-06-16 12:05:59 -06003461 case HTTP_REQ_ACT_REPLACE_HDR:
3462 case HTTP_REQ_ACT_REPLACE_VAL:
3463 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3464 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003465 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003466 return HTTP_RULE_RES_BADREQ;
3467 break;
3468
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003469 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003470 ctx.idx = 0;
3471 /* remove all occurrences of the header */
3472 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3473 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3474 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003475 }
Willy Tarreau85603282015-01-21 20:39:27 +01003476 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003477
Willy Tarreau85603282015-01-21 20:39:27 +01003478 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003479 case HTTP_REQ_ACT_ADD_HDR:
3480 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3481 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3482 trash.len = rule->arg.hdr_add.name_len;
3483 trash.str[trash.len++] = ':';
3484 trash.str[trash.len++] = ' ';
3485 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 +01003486
3487 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3488 /* remove all occurrences of the header */
3489 ctx.idx = 0;
3490 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3491 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3492 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3493 }
3494 }
3495
Willy Tarreau96257ec2012-12-27 10:46:37 +01003496 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3497 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003498
3499 case HTTP_REQ_ACT_DEL_ACL:
3500 case HTTP_REQ_ACT_DEL_MAP: {
3501 struct pat_ref *ref;
3502 char *key;
3503 int len;
3504
3505 /* collect reference */
3506 ref = pat_ref_lookup(rule->arg.map.ref);
3507 if (!ref)
3508 continue;
3509
3510 /* collect key */
3511 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3512 key = trash.str;
3513 key[len] = '\0';
3514
3515 /* perform update */
3516 /* returned code: 1=ok, 0=ko */
3517 pat_ref_delete(ref, key);
3518
3519 break;
3520 }
3521
3522 case HTTP_REQ_ACT_ADD_ACL: {
3523 struct pat_ref *ref;
3524 char *key;
3525 struct chunk *trash_key;
3526 int len;
3527
3528 trash_key = get_trash_chunk();
3529
3530 /* collect reference */
3531 ref = pat_ref_lookup(rule->arg.map.ref);
3532 if (!ref)
3533 continue;
3534
3535 /* collect key */
3536 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3537 key = trash_key->str;
3538 key[len] = '\0';
3539
3540 /* perform update */
3541 /* add entry only if it does not already exist */
3542 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003543 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003544
3545 break;
3546 }
3547
3548 case HTTP_REQ_ACT_SET_MAP: {
3549 struct pat_ref *ref;
3550 char *key, *value;
3551 struct chunk *trash_key, *trash_value;
3552 int len;
3553
3554 trash_key = get_trash_chunk();
3555 trash_value = get_trash_chunk();
3556
3557 /* collect reference */
3558 ref = pat_ref_lookup(rule->arg.map.ref);
3559 if (!ref)
3560 continue;
3561
3562 /* collect key */
3563 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3564 key = trash_key->str;
3565 key[len] = '\0';
3566
3567 /* collect value */
3568 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3569 value = trash_value->str;
3570 value[len] = '\0';
3571
3572 /* perform update */
3573 if (pat_ref_find_elt(ref, key) != NULL)
3574 /* update entry if it exists */
3575 pat_ref_set(ref, key, value, NULL);
3576 else
3577 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003578 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003579
3580 break;
3581 }
William Lallemand73025dd2014-04-24 14:38:37 +02003582
3583 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003584 if (!rule->action_ptr(rule, px, s, txn)) {
3585 s->current_rule = &rule->list;
3586 return HTTP_RULE_RES_YIELD;
3587 }
William Lallemand73025dd2014-04-24 14:38:37 +02003588 break;
3589
3590 case HTTP_REQ_ACT_CUSTOM_STOP:
3591 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003592 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003593
3594 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3595 /* Note: only the first valid tracking parameter of each
3596 * applies.
3597 */
3598
3599 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3600 struct stktable *t;
3601 struct stksess *ts;
3602 struct stktable_key *key;
3603 void *ptr;
3604
3605 t = rule->act_prm.trk_ctr.table.t;
3606 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3607
3608 if (key && (ts = stktable_get_entry(t, key))) {
3609 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3610
3611 /* let's count a new HTTP request as it's the first time we do it */
3612 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3613 if (ptr)
3614 stktable_data_cast(ptr, http_req_cnt)++;
3615
3616 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3617 if (ptr)
3618 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3619 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3620
3621 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3622 if (s->fe != s->be)
3623 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3624 }
3625 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003626 }
3627 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003628
3629 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003630 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003631}
3632
Willy Tarreau71241ab2012-12-27 11:30:54 +01003633
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003634/* Executes the http-response rules <rules> for session <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003635 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3636 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3637 * evaluation of next rule list. If *STOP is returned, the process must stop
3638 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3639 * rule. If *YIELD is returned, the czller must call again the function with
3640 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003641 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003642static enum rule_result
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003643http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3644{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003645 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003646 struct http_res_rule *rule;
3647 struct hdr_ctx ctx;
3648
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003649 /* If "the current_rule_list" match the executed rule list, we are in
3650 * resume condition. If a resume is needed it is always in the action
3651 * and never in the ACL or converters. In this case, we initialise the
3652 * current rule, and go to the action execution point.
3653 */
3654 if (s->current_rule_list == rules) {
3655 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3656 goto resume_execution;
3657 }
3658 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003659 list_for_each_entry(rule, rules, list) {
3660 if (rule->action >= HTTP_RES_ACT_MAX)
3661 continue;
3662
3663 /* check optional condition */
3664 if (rule->cond) {
3665 int ret;
3666
3667 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3668 ret = acl_pass(ret);
3669
3670 if (rule->cond->pol == ACL_COND_UNLESS)
3671 ret = !ret;
3672
3673 if (!ret) /* condition not matched */
3674 continue;
3675 }
3676
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003677resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003678
3679 switch (rule->action) {
3680 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003681 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003682
3683 case HTTP_RES_ACT_DENY:
3684 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003685 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003686
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003687 case HTTP_RES_ACT_SET_NICE:
3688 s->task->nice = rule->arg.nice;
3689 break;
3690
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003691 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003692 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003693 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003694 break;
3695
Willy Tarreau51347ed2013-06-11 19:34:13 +02003696 case HTTP_RES_ACT_SET_MARK:
3697#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003698 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003699 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003700#endif
3701 break;
3702
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003703 case HTTP_RES_ACT_SET_LOGL:
3704 s->logs.level = rule->arg.loglevel;
3705 break;
3706
Sasha Pachev218f0642014-06-16 12:05:59 -06003707 case HTTP_RES_ACT_REPLACE_HDR:
3708 case HTTP_RES_ACT_REPLACE_VAL:
3709 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3710 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003711 &rule->arg.hdr_add.re, &ctx, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003712 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003713 break;
3714
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003715 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003716 ctx.idx = 0;
3717 /* remove all occurrences of the header */
3718 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3719 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3720 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3721 }
Willy Tarreau85603282015-01-21 20:39:27 +01003722 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003723
Willy Tarreau85603282015-01-21 20:39:27 +01003724 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003725 case HTTP_RES_ACT_ADD_HDR:
3726 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3727 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3728 trash.len = rule->arg.hdr_add.name_len;
3729 trash.str[trash.len++] = ':';
3730 trash.str[trash.len++] = ' ';
3731 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 +01003732
3733 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3734 /* remove all occurrences of the header */
3735 ctx.idx = 0;
3736 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3737 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3738 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3739 }
3740 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003741 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3742 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003743
3744 case HTTP_RES_ACT_DEL_ACL:
3745 case HTTP_RES_ACT_DEL_MAP: {
3746 struct pat_ref *ref;
3747 char *key;
3748 int len;
3749
3750 /* collect reference */
3751 ref = pat_ref_lookup(rule->arg.map.ref);
3752 if (!ref)
3753 continue;
3754
3755 /* collect key */
3756 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3757 key = trash.str;
3758 key[len] = '\0';
3759
3760 /* perform update */
3761 /* returned code: 1=ok, 0=ko */
3762 pat_ref_delete(ref, key);
3763
3764 break;
3765 }
3766
3767 case HTTP_RES_ACT_ADD_ACL: {
3768 struct pat_ref *ref;
3769 char *key;
3770 struct chunk *trash_key;
3771 int len;
3772
3773 trash_key = get_trash_chunk();
3774
3775 /* collect reference */
3776 ref = pat_ref_lookup(rule->arg.map.ref);
3777 if (!ref)
3778 continue;
3779
3780 /* collect key */
3781 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3782 key = trash_key->str;
3783 key[len] = '\0';
3784
3785 /* perform update */
3786 /* check if the entry already exists */
3787 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003788 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003789
3790 break;
3791 }
3792
3793 case HTTP_RES_ACT_SET_MAP: {
3794 struct pat_ref *ref;
3795 char *key, *value;
3796 struct chunk *trash_key, *trash_value;
3797 int len;
3798
3799 trash_key = get_trash_chunk();
3800 trash_value = get_trash_chunk();
3801
3802 /* collect reference */
3803 ref = pat_ref_lookup(rule->arg.map.ref);
3804 if (!ref)
3805 continue;
3806
3807 /* collect key */
3808 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3809 key = trash_key->str;
3810 key[len] = '\0';
3811
3812 /* collect value */
3813 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3814 value = trash_value->str;
3815 value[len] = '\0';
3816
3817 /* perform update */
3818 if (pat_ref_find_elt(ref, key) != NULL)
3819 /* update entry if it exists */
3820 pat_ref_set(ref, key, value, NULL);
3821 else
3822 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003823 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003824
3825 break;
3826 }
William Lallemand73025dd2014-04-24 14:38:37 +02003827
3828 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003829 if (!rule->action_ptr(rule, px, s, txn)) {
3830 s->current_rule = &rule->list;
3831 return HTTP_RULE_RES_YIELD;
3832 }
William Lallemand73025dd2014-04-24 14:38:37 +02003833 break;
3834
3835 case HTTP_RES_ACT_CUSTOM_STOP:
3836 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003837 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003838 }
3839 }
3840
3841 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003842 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843}
3844
3845
Willy Tarreau71241ab2012-12-27 11:30:54 +01003846/* Perform an HTTP redirect based on the information in <rule>. The function
3847 * returns non-zero on success, or zero in case of a, irrecoverable error such
3848 * as too large a request to build a valid response.
3849 */
3850static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3851{
3852 struct http_msg *msg = &txn->req;
3853 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003854 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003855
3856 /* build redirect message */
3857 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003858 case 308:
3859 msg_fmt = HTTP_308;
3860 break;
3861 case 307:
3862 msg_fmt = HTTP_307;
3863 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003864 case 303:
3865 msg_fmt = HTTP_303;
3866 break;
3867 case 301:
3868 msg_fmt = HTTP_301;
3869 break;
3870 case 302:
3871 default:
3872 msg_fmt = HTTP_302;
3873 break;
3874 }
3875
3876 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3877 return 0;
3878
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003879 location = trash.str + trash.len;
3880
Willy Tarreau71241ab2012-12-27 11:30:54 +01003881 switch(rule->type) {
3882 case REDIRECT_TYPE_SCHEME: {
3883 const char *path;
3884 const char *host;
3885 struct hdr_ctx ctx;
3886 int pathlen;
3887 int hostlen;
3888
3889 host = "";
3890 hostlen = 0;
3891 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003892 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003893 host = ctx.line + ctx.val;
3894 hostlen = ctx.vlen;
3895 }
3896
3897 path = http_get_path(txn);
3898 /* build message using path */
3899 if (path) {
3900 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3901 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3902 int qs = 0;
3903 while (qs < pathlen) {
3904 if (path[qs] == '?') {
3905 pathlen = qs;
3906 break;
3907 }
3908 qs++;
3909 }
3910 }
3911 } else {
3912 path = "/";
3913 pathlen = 1;
3914 }
3915
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003916 if (rule->rdr_str) { /* this is an old "redirect" rule */
3917 /* check if we can add scheme + "://" + host + path */
3918 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3919 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003920
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003921 /* add scheme */
3922 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3923 trash.len += rule->rdr_len;
3924 }
3925 else {
3926 /* add scheme with executing log format */
3927 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003928
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003929 /* check if we can add scheme + "://" + host + path */
3930 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3931 return 0;
3932 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003933 /* add "://" */
3934 memcpy(trash.str + trash.len, "://", 3);
3935 trash.len += 3;
3936
3937 /* add host */
3938 memcpy(trash.str + trash.len, host, hostlen);
3939 trash.len += hostlen;
3940
3941 /* add path */
3942 memcpy(trash.str + trash.len, path, pathlen);
3943 trash.len += pathlen;
3944
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003945 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003946 if (trash.len && trash.str[trash.len - 1] != '/' &&
3947 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3948 if (trash.len > trash.size - 5)
3949 return 0;
3950 trash.str[trash.len] = '/';
3951 trash.len++;
3952 }
3953
3954 break;
3955 }
3956 case REDIRECT_TYPE_PREFIX: {
3957 const char *path;
3958 int pathlen;
3959
3960 path = http_get_path(txn);
3961 /* build message using path */
3962 if (path) {
3963 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3964 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3965 int qs = 0;
3966 while (qs < pathlen) {
3967 if (path[qs] == '?') {
3968 pathlen = qs;
3969 break;
3970 }
3971 qs++;
3972 }
3973 }
3974 } else {
3975 path = "/";
3976 pathlen = 1;
3977 }
3978
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003979 if (rule->rdr_str) { /* this is an old "redirect" rule */
3980 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3981 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003982
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003983 /* add prefix. Note that if prefix == "/", we don't want to
3984 * add anything, otherwise it makes it hard for the user to
3985 * configure a self-redirection.
3986 */
3987 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3988 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3989 trash.len += rule->rdr_len;
3990 }
3991 }
3992 else {
3993 /* add prefix with executing log format */
3994 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3995
3996 /* Check length */
3997 if (trash.len + pathlen > trash.size - 4)
3998 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003999 }
4000
4001 /* add path */
4002 memcpy(trash.str + trash.len, path, pathlen);
4003 trash.len += pathlen;
4004
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004005 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004006 if (trash.len && trash.str[trash.len - 1] != '/' &&
4007 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4008 if (trash.len > trash.size - 5)
4009 return 0;
4010 trash.str[trash.len] = '/';
4011 trash.len++;
4012 }
4013
4014 break;
4015 }
4016 case REDIRECT_TYPE_LOCATION:
4017 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004018 if (rule->rdr_str) { /* this is an old "redirect" rule */
4019 if (trash.len + rule->rdr_len > trash.size - 4)
4020 return 0;
4021
4022 /* add location */
4023 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4024 trash.len += rule->rdr_len;
4025 }
4026 else {
4027 /* add location with executing log format */
4028 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004029
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004030 /* Check left length */
4031 if (trash.len > trash.size - 4)
4032 return 0;
4033 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004034 break;
4035 }
4036
4037 if (rule->cookie_len) {
4038 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4039 trash.len += 14;
4040 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4041 trash.len += rule->cookie_len;
4042 memcpy(trash.str + trash.len, "\r\n", 2);
4043 trash.len += 2;
4044 }
4045
4046 /* add end of headers and the keep-alive/close status.
4047 * We may choose to set keep-alive if the Location begins
4048 * with a slash, because the client will come back to the
4049 * same server.
4050 */
4051 txn->status = rule->code;
4052 /* let's log the request time */
4053 s->logs.tv_request = now;
4054
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004055 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004056 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4057 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4058 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4059 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4060 /* keep-alive possible */
4061 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4062 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4063 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4064 trash.len += 30;
4065 } else {
4066 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4067 trash.len += 24;
4068 }
4069 }
4070 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4071 trash.len += 4;
4072 bo_inject(txn->rsp.chn, trash.str, trash.len);
4073 /* "eat" the request */
4074 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004075 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004076 msg->sov = 0;
4077 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004078 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004079 txn->req.msg_state = HTTP_MSG_CLOSED;
4080 txn->rsp.msg_state = HTTP_MSG_DONE;
4081 } else {
4082 /* keep-alive not possible */
4083 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4084 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4085 trash.len += 29;
4086 } else {
4087 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4088 trash.len += 23;
4089 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004090 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004091 txn->req.chn->analysers = 0;
4092 }
4093
4094 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004095 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004096 if (!(s->flags & SN_FINST_MASK))
4097 s->flags |= SN_FINST_R;
4098
4099 return 1;
4100}
4101
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004102/* This stream analyser runs all HTTP request processing which is common to
4103 * frontends and backends, which means blocking ACLs, filters, connection-close,
4104 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004105 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004106 * either needs more data or wants to immediately abort the request (eg: deny,
4107 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004108 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004109int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004110{
Willy Tarreaud787e662009-07-07 10:14:51 +02004111 struct http_txn *txn = &s->txn;
4112 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004113 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004114 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004115 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004116
Willy Tarreau655dce92009-11-08 13:10:58 +01004117 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004118 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004119 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004120 }
4121
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004122 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 +02004123 now_ms, __FUNCTION__,
4124 s,
4125 req,
4126 req->rex, req->wex,
4127 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004128 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004129 req->analysers);
4130
Willy Tarreau65410832014-04-28 21:25:43 +02004131 /* just in case we have some per-backend tracking */
4132 session_inc_be_http_req_ctr(s);
4133
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004134 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004135 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4136 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004137
Willy Tarreau0b748332014-04-29 00:13:29 +02004138 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004139 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4140 goto return_prx_yield;
4141
Willy Tarreau0b748332014-04-29 00:13:29 +02004142 case HTTP_RULE_RES_CONT:
4143 case HTTP_RULE_RES_STOP: /* nothing to do */
4144 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004145
Willy Tarreau0b748332014-04-29 00:13:29 +02004146 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4147 if (txn->flags & TX_CLTARPIT)
4148 goto tarpit;
4149 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004150
Willy Tarreau0b748332014-04-29 00:13:29 +02004151 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4152 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004153
Willy Tarreau0b748332014-04-29 00:13:29 +02004154 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004155 goto done;
4156
Willy Tarreau0b748332014-04-29 00:13:29 +02004157 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4158 goto return_bad_req;
4159 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004160 }
4161
Willy Tarreau52542592014-04-28 18:33:26 +02004162 /* OK at this stage, we know that the request was accepted according to
4163 * the http-request rules, we can check for the stats. Note that the
4164 * URI is detected *before* the req* rules in order not to be affected
4165 * by a possible reqrep, while they are processed *after* so that a
4166 * reqdeny can still block them. This clearly needs to change in 1.6!
4167 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004168 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004169 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004170 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004171 txn->status = 500;
4172 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004173 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004174
Willy Tarreau52542592014-04-28 18:33:26 +02004175 if (!(s->flags & SN_ERR_MASK))
4176 s->flags |= SN_ERR_RESOURCE;
4177 goto return_prx_cond;
4178 }
4179
4180 /* parse the whole stats request and extract the relevant information */
4181 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004182 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4183 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004184
Willy Tarreau0b748332014-04-29 00:13:29 +02004185 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4186 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004187
Willy Tarreau0b748332014-04-29 00:13:29 +02004188 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4189 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004190 }
4191
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004192 /* evaluate the req* rules except reqadd */
4193 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004194 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004195 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004196
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004197 if (txn->flags & TX_CLDENY)
4198 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004199
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004200 if (txn->flags & TX_CLTARPIT)
4201 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004202 }
Willy Tarreau06619262006-12-17 08:37:22 +01004203
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004204 /* add request headers from the rule sets in the same order */
4205 list_for_each_entry(wl, &px->req_add, list) {
4206 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004207 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004208 ret = acl_pass(ret);
4209 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4210 ret = !ret;
4211 if (!ret)
4212 continue;
4213 }
4214
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004215 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004216 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004217 }
4218
Willy Tarreau52542592014-04-28 18:33:26 +02004219
4220 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004221 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004222 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004223 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4224 s->fe->fe_counters.intercepted_req++;
4225
4226 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4227 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4228 if (!(s->flags & SN_FINST_MASK))
4229 s->flags |= SN_FINST_R;
4230
Willy Tarreau70730dd2014-04-24 18:06:27 +02004231 /* we may want to compress the stats page */
4232 if (s->fe->comp || s->be->comp)
4233 select_compression_request_header(s, req->buf);
4234
4235 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004236 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004237 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004238 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004239
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004240 /* check whether we have some ACLs set to redirect this request */
4241 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004242 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 int ret;
4244
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004245 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004246 ret = acl_pass(ret);
4247 if (rule->cond->pol == ACL_COND_UNLESS)
4248 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 if (!ret)
4250 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004251 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004252 if (!http_apply_redirect_rule(rule, s, txn))
4253 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004254 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004255 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004256
Willy Tarreau2be39392010-01-03 17:24:51 +01004257 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4258 * If this happens, then the data will not come immediately, so we must
4259 * send all what we have without waiting. Note that due to the small gain
4260 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004261 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004262 * itself once used.
4263 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004264 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004265
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 done: /* done with this analyser, continue with next ones that the calling
4267 * points will have set, if any.
4268 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004269 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004270 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4271 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004272 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004273
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004274 tarpit:
4275 /* When a connection is tarpitted, we use the tarpit timeout,
4276 * which may be the same as the connect timeout if unspecified.
4277 * If unset, then set it to zero because we really want it to
4278 * eventually expire. We build the tarpit as an analyser.
4279 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004280 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004281
4282 /* wipe the request out so that we can drop the connection early
4283 * if the client closes first.
4284 */
4285 channel_dont_connect(req);
4286 req->analysers = 0; /* remove switching rules etc... */
4287 req->analysers |= AN_REQ_HTTP_TARPIT;
4288 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4289 if (!req->analyse_exp)
4290 req->analyse_exp = tick_add(now_ms, 0);
4291 session_inc_http_err_ctr(s);
4292 s->fe->fe_counters.denied_req++;
4293 if (s->fe != s->be)
4294 s->be->be_counters.denied_req++;
4295 if (s->listener->counters)
4296 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004297 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004298
4299 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004300 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004301 txn->status = 403;
4302 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004303 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004304 session_inc_http_err_ctr(s);
4305 s->fe->fe_counters.denied_req++;
4306 if (s->fe != s->be)
4307 s->be->be_counters.denied_req++;
4308 if (s->listener->counters)
4309 s->listener->counters->denied_req++;
4310 goto return_prx_cond;
4311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004312 return_bad_req:
4313 /* We centralize bad requests processing here */
4314 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4315 /* we detected a parsing error. We want to archive this request
4316 * in the dedicated proxy area for later troubleshooting.
4317 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004318 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004319 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004320
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004321 txn->req.msg_state = HTTP_MSG_ERROR;
4322 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004323 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004324
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004325 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004326 if (s->listener->counters)
4327 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004328
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004329 return_prx_cond:
4330 if (!(s->flags & SN_ERR_MASK))
4331 s->flags |= SN_ERR_PRXCOND;
4332 if (!(s->flags & SN_FINST_MASK))
4333 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004334
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004335 req->analysers = 0;
4336 req->analyse_exp = TICK_ETERNITY;
4337 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004338
4339 return_prx_yield:
4340 channel_dont_connect(req);
4341 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004342}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004343
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004344/* This function performs all the processing enabled for the current request.
4345 * It returns 1 if the processing can continue on next analysers, or zero if it
4346 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004347 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004348 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004349int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004350{
4351 struct http_txn *txn = &s->txn;
4352 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004353 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354
Willy Tarreau655dce92009-11-08 13:10:58 +01004355 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004356 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004357 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004358 return 0;
4359 }
4360
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004361 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 +02004362 now_ms, __FUNCTION__,
4363 s,
4364 req,
4365 req->rex, req->wex,
4366 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004367 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004368 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004369
William Lallemand82fe75c2012-10-23 10:25:10 +02004370 if (s->fe->comp || s->be->comp)
4371 select_compression_request_header(s, req->buf);
4372
Willy Tarreau59234e92008-11-30 23:51:27 +01004373 /*
4374 * Right now, we know that we have processed the entire headers
4375 * and that unwanted requests have been filtered out. We can do
4376 * whatever we want with the remaining request. Also, now we
4377 * may have separate values for ->fe, ->be.
4378 */
Willy Tarreau06619262006-12-17 08:37:22 +01004379
Willy Tarreau59234e92008-11-30 23:51:27 +01004380 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004381 * If HTTP PROXY is set we simply get remote server address parsing
4382 * incoming request. Note that this requires that a connection is
4383 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004384 */
4385 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004386 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004387 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004388
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004389 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004390 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004391 txn->req.msg_state = HTTP_MSG_ERROR;
4392 txn->status = 500;
4393 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004394 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004395
4396 if (!(s->flags & SN_ERR_MASK))
4397 s->flags |= SN_ERR_RESOURCE;
4398 if (!(s->flags & SN_FINST_MASK))
4399 s->flags |= SN_FINST_R;
4400
4401 return 0;
4402 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004403
4404 path = http_get_path(txn);
4405 url2sa(req->buf->p + msg->sl.rq.u,
4406 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004407 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004408 /* if the path was found, we have to remove everything between
4409 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4410 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4411 * u_l characters by a single "/".
4412 */
4413 if (path) {
4414 char *cur_ptr = req->buf->p;
4415 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4416 int delta;
4417
4418 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4419 http_msg_move_end(&txn->req, delta);
4420 cur_end += delta;
4421 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4422 goto return_bad_req;
4423 }
4424 else {
4425 char *cur_ptr = req->buf->p;
4426 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4427 int delta;
4428
4429 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4430 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4431 http_msg_move_end(&txn->req, delta);
4432 cur_end += delta;
4433 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4434 goto return_bad_req;
4435 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004436 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004437
Willy Tarreau59234e92008-11-30 23:51:27 +01004438 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004439 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004440 * Note that doing so might move headers in the request, but
4441 * the fields will stay coherent and the URI will not move.
4442 * This should only be performed in the backend.
4443 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004444 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004445 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4446 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004447
Willy Tarreau59234e92008-11-30 23:51:27 +01004448 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004449 * 8: the appsession cookie was looked up very early in 1.2,
4450 * so let's do the same now.
4451 */
4452
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004453 /* It needs to look into the URI unless persistence must be ignored */
4454 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004455 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 +01004456 }
4457
William Lallemanda73203e2012-03-12 12:48:57 +01004458 /* add unique-id if "header-unique-id" is specified */
4459
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004460 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4461 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4462 goto return_bad_req;
4463 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004464 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004465 }
William Lallemanda73203e2012-03-12 12:48:57 +01004466
4467 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004468 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4469 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004470 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004471 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004472 goto return_bad_req;
4473 }
4474
Cyril Bontéb21570a2009-11-29 20:04:48 +01004475 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004476 * 9: add X-Forwarded-For if either the frontend or the backend
4477 * asks for it.
4478 */
4479 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004480 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004481 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004482 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4483 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004484 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004485 /* The header is set to be added only if none is present
4486 * and we found it, so don't do anything.
4487 */
4488 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004489 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004490 /* Add an X-Forwarded-For header unless the source IP is
4491 * in the 'except' network range.
4492 */
4493 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 != s->fe->except_net.s_addr) &&
4496 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004497 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004498 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004499 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004500 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004501 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004502
4503 /* Note: we rely on the backend to get the header name to be used for
4504 * x-forwarded-for, because the header is really meant for the backends.
4505 * However, if the backend did not specify any option, we have to rely
4506 * on the frontend's header name.
4507 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004508 if (s->be->fwdfor_hdr_len) {
4509 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004511 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004512 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004513 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004514 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004515 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 +01004516
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004517 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004518 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004519 }
4520 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 /* FIXME: for the sake of completeness, we should also support
4523 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004524 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004525 int len;
4526 char pn[INET6_ADDRSTRLEN];
4527 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004528 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004529 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004530
Willy Tarreau59234e92008-11-30 23:51:27 +01004531 /* Note: we rely on the backend to get the header name to be used for
4532 * x-forwarded-for, because the header is really meant for the backends.
4533 * However, if the backend did not specify any option, we have to rely
4534 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004535 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004536 if (s->be->fwdfor_hdr_len) {
4537 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004538 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 } else {
4540 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004541 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004542 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004543 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004544
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004546 goto return_bad_req;
4547 }
4548 }
4549
4550 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004551 * 10: add X-Original-To if either the frontend or the backend
4552 * asks for it.
4553 */
4554 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4555
4556 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004557 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004558 /* Add an X-Original-To header unless the destination IP is
4559 * in the 'except' network range.
4560 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004561 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004562
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004563 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004564 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004565 (((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 +02004566 != s->fe->except_to.s_addr) &&
4567 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004568 (((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 +02004569 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004570 int len;
4571 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004572 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004573
4574 /* Note: we rely on the backend to get the header name to be used for
4575 * x-original-to, because the header is really meant for the backends.
4576 * However, if the backend did not specify any option, we have to rely
4577 * on the frontend's header name.
4578 */
4579 if (s->be->orgto_hdr_len) {
4580 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004581 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004582 } else {
4583 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004584 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004585 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004586 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 +02004587
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004588 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004589 goto return_bad_req;
4590 }
4591 }
4592 }
4593
Willy Tarreau50fc7772012-11-11 22:19:57 +01004594 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4595 * If an "Upgrade" token is found, the header is left untouched in order not to have
4596 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4597 * "Upgrade" is present in the Connection header.
4598 */
4599 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4600 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004601 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4602 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004603 unsigned int want_flags = 0;
4604
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004605 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004606 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004607 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4608 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004609 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004610 want_flags |= TX_CON_CLO_SET;
4611 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004612 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004613 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4614 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004615 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004616 want_flags |= TX_CON_KAL_SET;
4617 }
4618
4619 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004620 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004621 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004622
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004623
Willy Tarreau522d6c02009-12-06 18:49:18 +01004624 /* If we have no server assigned yet and we're balancing on url_param
4625 * with a POST request, we may be interested in checking the body for
4626 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 */
4628 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4629 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004630 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004631 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004632 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004634
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004635 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004636 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004637#ifdef TCP_QUICKACK
4638 /* We expect some data from the client. Unless we know for sure
4639 * we already have a full request, we have to re-enable quick-ack
4640 * in case we previously disabled it, otherwise we might cause
4641 * the client to delay further data.
4642 */
4643 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004644 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004645 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004646 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004647 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004648#endif
4649 }
Willy Tarreau03945942009-12-22 16:50:27 +01004650
Willy Tarreau59234e92008-11-30 23:51:27 +01004651 /*************************************************************
4652 * OK, that's finished for the headers. We have done what we *
4653 * could. Let's switch to the DATA state. *
4654 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004655 req->analyse_exp = TICK_ETERNITY;
4656 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004657
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004658 /* if the server closes the connection, we want to immediately react
4659 * and close the socket to save packets and syscalls.
4660 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004661 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004662 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004663
Willy Tarreau59234e92008-11-30 23:51:27 +01004664 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 /* OK let's go on with the BODY now */
4666 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004667
Willy Tarreau59234e92008-11-30 23:51:27 +01004668 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004669 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004670 /* we detected a parsing error. We want to archive this request
4671 * in the dedicated proxy area for later troubleshooting.
4672 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004673 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004674 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004675
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 txn->req.msg_state = HTTP_MSG_ERROR;
4677 txn->status = 400;
4678 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004679 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004680
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004681 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004682 if (s->listener->counters)
4683 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004684
Willy Tarreau59234e92008-11-30 23:51:27 +01004685 if (!(s->flags & SN_ERR_MASK))
4686 s->flags |= SN_ERR_PRXCOND;
4687 if (!(s->flags & SN_FINST_MASK))
4688 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004689 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004690}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004691
Willy Tarreau60b85b02008-11-30 23:28:40 +01004692/* This function is an analyser which processes the HTTP tarpit. It always
4693 * returns zero, at the beginning because it prevents any other processing
4694 * from occurring, and at the end because it terminates the request.
4695 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004696int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004697{
4698 struct http_txn *txn = &s->txn;
4699
4700 /* This connection is being tarpitted. The CLIENT side has
4701 * already set the connect expiration date to the right
4702 * timeout. We just have to check that the client is still
4703 * there and that the timeout has not expired.
4704 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004705 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004706 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004707 !tick_is_expired(req->analyse_exp, now_ms))
4708 return 0;
4709
4710 /* We will set the queue timer to the time spent, just for
4711 * logging purposes. We fake a 500 server error, so that the
4712 * attacker will not suspect his connection has been tarpitted.
4713 * It will not cause trouble to the logs because we can exclude
4714 * the tarpitted connections by filtering on the 'PT' status flags.
4715 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004716 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4717
4718 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004719 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004720 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004721
4722 req->analysers = 0;
4723 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004724
Willy Tarreau60b85b02008-11-30 23:28:40 +01004725 if (!(s->flags & SN_ERR_MASK))
4726 s->flags |= SN_ERR_PRXCOND;
4727 if (!(s->flags & SN_FINST_MASK))
4728 s->flags |= SN_FINST_T;
4729 return 0;
4730}
4731
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004732/* This function is an analyser which waits for the HTTP request body. It waits
4733 * for either the buffer to be full, or the full advertised contents to have
4734 * reached the buffer. It must only be called after the standard HTTP request
4735 * processing has occurred, because it expects the request to be parsed and will
4736 * look for the Expect header. It may send a 100-Continue interim response. It
4737 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4738 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4739 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004740 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004741int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004742{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004743 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004744 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004745
4746 /* We have to parse the HTTP request body to find any required data.
4747 * "balance url_param check_post" should have been the only way to get
4748 * into this. We were brought here after HTTP header analysis, so all
4749 * related structures are ready.
4750 */
4751
Willy Tarreau890988f2014-04-10 11:59:33 +02004752 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4753 /* This is the first call */
4754 if (msg->msg_state < HTTP_MSG_BODY)
4755 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004756
Willy Tarreau890988f2014-04-10 11:59:33 +02004757 if (msg->msg_state < HTTP_MSG_100_SENT) {
4758 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4759 * send an HTTP/1.1 100 Continue intermediate response.
4760 */
4761 if (msg->flags & HTTP_MSGF_VER_11) {
4762 struct hdr_ctx ctx;
4763 ctx.idx = 0;
4764 /* Expect is allowed in 1.1, look for it */
4765 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4766 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004767 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004768 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004769 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004770 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004772
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004773 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004774 * req->buf->p still points to the beginning of the message. We
4775 * must save the body in msg->next because it survives buffer
4776 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004778 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004779
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004780 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004781 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4782 else
4783 msg->msg_state = HTTP_MSG_DATA;
4784 }
4785
Willy Tarreau890988f2014-04-10 11:59:33 +02004786 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4787 /* We're in content-length mode, we just have to wait for enough data. */
4788 if (req->buf->i - msg->sov < msg->body_len)
4789 goto missing_data;
4790
4791 /* OK we have everything we need now */
4792 goto http_end;
4793 }
4794
4795 /* OK here we're parsing a chunked-encoded message */
4796
Willy Tarreau522d6c02009-12-06 18:49:18 +01004797 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004798 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004799 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004801 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004802 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004803
Willy Tarreau115acb92009-12-26 13:56:06 +01004804 if (!ret)
4805 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004806 else if (ret < 0) {
4807 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004808 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004809 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004810 }
4811
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004813 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004814 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004815 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004816 if (msg->msg_state == HTTP_MSG_TRAILERS)
4817 goto http_end;
4818
Willy Tarreau226071e2014-04-10 11:55:45 +02004819 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004820 goto http_end;
4821
4822 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004823 /* we get here if we need to wait for more data. If the buffer is full,
4824 * we have the maximum we can expect.
4825 */
4826 if (buffer_full(req->buf, global.tune.maxrewrite))
4827 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004828
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004829 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004830 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004831 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004832
4833 if (!(s->flags & SN_ERR_MASK))
4834 s->flags |= SN_ERR_CLITO;
4835 if (!(s->flags & SN_FINST_MASK))
4836 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004837 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004838 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839
4840 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004841 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004842 /* Not enough data. We'll re-use the http-request
4843 * timeout here. Ideally, we should set the timeout
4844 * relative to the accept() date. We just set the
4845 * request timeout once at the beginning of the
4846 * request.
4847 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004848 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004849 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004850 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004851 return 0;
4852 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004853
4854 http_end:
4855 /* The situation will not evolve, so let's give up on the analysis. */
4856 s->logs.tv_request = now; /* update the request timer to reflect full request */
4857 req->analysers &= ~an_bit;
4858 req->analyse_exp = TICK_ETERNITY;
4859 return 1;
4860
4861 return_bad_req: /* let's centralize all bad requests */
4862 txn->req.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004864 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004865
Willy Tarreau79ebac62010-06-07 13:47:49 +02004866 if (!(s->flags & SN_ERR_MASK))
4867 s->flags |= SN_ERR_PRXCOND;
4868 if (!(s->flags & SN_FINST_MASK))
4869 s->flags |= SN_FINST_R;
4870
Willy Tarreau522d6c02009-12-06 18:49:18 +01004871 return_err_msg:
4872 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004873 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004874 if (s->listener->counters)
4875 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004876 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004877}
4878
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004879/* send a server's name with an outgoing request over an established connection.
4880 * Note: this function is designed to be called once the request has been scheduled
4881 * for being forwarded. This is the reason why it rewinds the buffer before
4882 * proceeding.
4883 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004884int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004885
4886 struct hdr_ctx ctx;
4887
Mark Lamourinec2247f02012-01-04 13:02:01 -05004888 char *hdr_name = be->server_id_hdr_name;
4889 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004890 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004891 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004892 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004893
William Lallemandd9e90662012-01-30 17:27:17 +01004894 ctx.idx = 0;
4895
Willy Tarreau211cdec2014-04-17 20:18:08 +02004896 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004897 if (old_o) {
4898 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004899 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004900 txn->req.next += old_o;
4901 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004902 }
4903
Willy Tarreau9b28e032012-10-12 23:49:43 +02004904 old_i = chn->buf->i;
4905 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 -05004906 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004907 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004908 }
4909
4910 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004911 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004912 memcpy(hdr_val, hdr_name, hdr_name_len);
4913 hdr_val += hdr_name_len;
4914 *hdr_val++ = ':';
4915 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004916 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4917 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004918
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004919 if (old_o) {
4920 /* If this was a forwarded request, we must readjust the amount of
4921 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004922 * variations. Note that the current state is >= HTTP_MSG_BODY,
4923 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004924 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004925 old_o += chn->buf->i - old_i;
4926 b_adv(chn->buf, old_o);
4927 txn->req.next -= old_o;
4928 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004929 }
4930
Mark Lamourinec2247f02012-01-04 13:02:01 -05004931 return 0;
4932}
4933
Willy Tarreau610ecce2010-01-04 21:15:02 +01004934/* Terminate current transaction and prepare a new one. This is very tricky
4935 * right now but it works.
4936 */
4937void http_end_txn_clean_session(struct session *s)
4938{
Willy Tarreau068621e2013-12-23 15:11:25 +01004939 int prev_status = s->txn.status;
4940
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 /* FIXME: We need a more portable way of releasing a backend's and a
4942 * server's connections. We need a safer way to reinitialize buffer
4943 * flags. We also need a more accurate method for computing per-request
4944 * data.
4945 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946
Willy Tarreau4213a112013-12-15 10:25:42 +01004947 /* unless we're doing keep-alive, we want to quickly close the connection
4948 * to the server.
4949 */
4950 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004951 !si_conn_ready(&s->si[1])) {
4952 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4953 si_shutr(&s->si[1]);
4954 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004955 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004957 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004959 if (unlikely(s->srv_conn))
4960 sess_change_server(s, NULL);
4961 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962
4963 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4964 session_process_counters(s);
4965
4966 if (s->txn.status) {
4967 int n;
4968
4969 n = s->txn.status / 100;
4970 if (n < 1 || n > 5)
4971 n = 0;
4972
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004973 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004974 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004975 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004976 s->fe->fe_counters.p.http.comp_rsp++;
4977 }
Willy Tarreau24657792010-02-26 10:30:28 +01004978 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004979 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004980 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004981 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004982 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004983 s->be->be_counters.p.http.comp_rsp++;
4984 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985 }
4986
4987 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004988 s->logs.bytes_in -= s->req.buf->i;
4989 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990
4991 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004992 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 !(s->flags & SN_MONITOR) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004994 (!(s->fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 s->do_log(s);
4996 }
4997
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004998 /* stop tracking content-based counters */
4999 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005000 session_update_time_stats(s);
5001
Willy Tarreau610ecce2010-01-04 21:15:02 +01005002 s->logs.accept_date = date; /* user-visible date for logging */
5003 s->logs.tv_accept = now; /* corrected date for internal use */
5004 tv_zero(&s->logs.tv_request);
5005 s->logs.t_queue = -1;
5006 s->logs.t_connect = -1;
5007 s->logs.t_data = -1;
5008 s->logs.t_close = 0;
5009 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5010 s->logs.srv_queue_size = 0; /* we will get this number soon */
5011
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005012 s->logs.bytes_in = s->req.total = s->req.buf->i;
5013 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014
5015 if (s->pend_pos)
5016 pendconn_free(s->pend_pos);
5017
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005018 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 if (s->flags & SN_CURR_SESS) {
5020 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005021 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005023 if (may_dequeue_tasks(objt_server(s->target), s->be))
5024 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025 }
5026
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005027 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028
Willy Tarreau4213a112013-12-15 10:25:42 +01005029 /* only release our endpoint if we don't intend to reuse the
5030 * connection.
5031 */
5032 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005033 !si_conn_ready(&s->si[1])) {
5034 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005035 }
5036
Willy Tarreau350f4872014-11-28 14:42:25 +01005037 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5038 s->si[1].err_type = SI_ET_NONE;
5039 s->si[1].conn_retries = 0; /* used for logging too */
5040 s->si[1].exp = TICK_ETERNITY;
5041 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005042 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);
5043 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005044 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 +01005045 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02005046 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005047
Willy Tarreau610ecce2010-01-04 21:15:02 +01005048 s->txn.meth = 0;
5049 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005050 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005051
5052 if (prev_status == 401 || prev_status == 407) {
5053 /* In HTTP keep-alive mode, if we receive a 401, we still have
5054 * a chance of being able to send the visitor again to the same
5055 * server over the same connection. This is required by some
5056 * broken protocols such as NTLM, and anyway whenever there is
5057 * an opportunity for sending the challenge to the proper place,
5058 * it's better to do it (at least it helps with debugging).
5059 */
5060 s->txn.flags |= TX_PREFER_LAST;
5061 }
5062
Willy Tarreauee55dc02010-06-01 10:56:34 +02005063 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005064 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065
Willy Tarreau96e31212011-05-30 18:10:30 +02005066 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005067 s->req.flags |= CF_NEVER_WAIT;
5068 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005069 }
5070
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 /* if the request buffer is not empty, it means we're
5072 * about to process another request, so send pending
5073 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005074 * Just don't do this if the buffer is close to be full,
5075 * because the request will wait for it to flush a little
5076 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005078 if (s->req.buf->i) {
5079 if (s->res.buf->o &&
5080 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5081 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5082 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005083 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005084
5085 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005086 channel_auto_read(&s->req);
5087 channel_auto_close(&s->req);
5088 channel_auto_read(&s->res);
5089 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090
Willy Tarreau27375622013-12-17 00:00:28 +01005091 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005092 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005093
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005094 s->req.analysers = s->listener->analysers;
5095 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005096}
5097
5098
5099/* This function updates the request state machine according to the response
5100 * state machine and buffer flags. It returns 1 if it changes anything (flag
5101 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5102 * it is only used to find when a request/response couple is complete. Both
5103 * this function and its equivalent should loop until both return zero. It
5104 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5105 */
5106int http_sync_req_state(struct session *s)
5107{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005108 struct channel *chn = &s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005109 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005110 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 unsigned int old_state = txn->req.msg_state;
5112
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5114 return 0;
5115
5116 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005117 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005118 * We can shut the read side unless we want to abort_on_close,
5119 * or we have a POST request. The issue with POST requests is
5120 * that some browsers still send a CRLF after the request, and
5121 * this CRLF must be read so that it does not remain in the kernel
5122 * buffers, otherwise a close could cause an RST on some systems
5123 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005124 * Note that if we're using keep-alive on the client side, we'd
5125 * rather poll now and keep the polling enabled for the whole
5126 * session's life than enabling/disabling it between each
5127 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005128 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005129 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5130 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5131 !(s->be->options & PR_O_ABRT_CLOSE) &&
5132 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005133 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134
Willy Tarreau40f151a2012-12-20 12:10:09 +01005135 /* if the server closes the connection, we want to immediately react
5136 * and close the socket to save packets and syscalls.
5137 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005138 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005139
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5141 goto wait_other_side;
5142
5143 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5144 /* The server has not finished to respond, so we
5145 * don't want to move in order not to upset it.
5146 */
5147 goto wait_other_side;
5148 }
5149
5150 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5151 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005152 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005154 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 goto wait_other_side;
5156 }
5157
5158 /* When we get here, it means that both the request and the
5159 * response have finished receiving. Depending on the connection
5160 * mode, we'll have to wait for the last bytes to leave in either
5161 * direction, and sometimes for a close to be effective.
5162 */
5163
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5165 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005166 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5167 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005168 }
5169 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5170 /* Option forceclose is set, or either side wants to close,
5171 * let's enforce it now that we're not expecting any new
5172 * data to come. The caller knows the session is complete
5173 * once both states are CLOSED.
5174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5176 channel_shutr_now(chn);
5177 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005179 }
5180 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5182 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005183 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5185 channel_auto_read(chn);
5186 txn->req.msg_state = HTTP_MSG_TUNNEL;
5187 chn->flags |= CF_NEVER_WAIT;
5188 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 }
5190
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005191 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005193 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005194
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 txn->req.msg_state = HTTP_MSG_CLOSING;
5197 goto http_msg_closing;
5198 }
5199 else {
5200 txn->req.msg_state = HTTP_MSG_CLOSED;
5201 goto http_msg_closed;
5202 }
5203 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005204 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005205 }
5206
5207 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5208 http_msg_closing:
5209 /* nothing else to forward, just waiting for the output buffer
5210 * to be empty and for the shutw_now to take effect.
5211 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005212 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 txn->req.msg_state = HTTP_MSG_CLOSED;
5214 goto http_msg_closed;
5215 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005216 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 txn->req.msg_state = HTTP_MSG_ERROR;
5218 goto wait_other_side;
5219 }
5220 }
5221
5222 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5223 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005224 /* see above in MSG_DONE why we only do this in these states */
5225 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5226 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5227 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005228 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 goto wait_other_side;
5230 }
5231
5232 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005233 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234}
5235
5236
5237/* This function updates the response state machine according to the request
5238 * state machine and buffer flags. It returns 1 if it changes anything (flag
5239 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5240 * it is only used to find when a request/response couple is complete. Both
5241 * this function and its equivalent should loop until both return zero. It
5242 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5243 */
5244int http_sync_res_state(struct session *s)
5245{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005246 struct channel *chn = &s->res;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005248 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 unsigned int old_state = txn->rsp.msg_state;
5250
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5252 return 0;
5253
5254 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5255 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005256 * still monitor the server connection for a possible close
5257 * while the request is being uploaded, so we don't disable
5258 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005260 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261
5262 if (txn->req.msg_state == HTTP_MSG_ERROR)
5263 goto wait_other_side;
5264
5265 if (txn->req.msg_state < HTTP_MSG_DONE) {
5266 /* The client seems to still be sending data, probably
5267 * because we got an error response during an upload.
5268 * We have the choice of either breaking the connection
5269 * or letting it pass through. Let's do the later.
5270 */
5271 goto wait_other_side;
5272 }
5273
5274 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5275 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005278 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 goto wait_other_side;
5280 }
5281
5282 /* When we get here, it means that both the request and the
5283 * response have finished receiving. Depending on the connection
5284 * mode, we'll have to wait for the last bytes to leave in either
5285 * direction, and sometimes for a close to be effective.
5286 */
5287
5288 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5289 /* Server-close mode : shut read and wait for the request
5290 * side to close its output buffer. The caller will detect
5291 * when we're in DONE and the other is in CLOSED and will
5292 * catch that for the final cleanup.
5293 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005294 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5295 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005297 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5298 /* Option forceclose is set, or either side wants to close,
5299 * let's enforce it now that we're not expecting any new
5300 * data to come. The caller knows the session is complete
5301 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5304 channel_shutr_now(chn);
5305 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005306 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 }
5308 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005309 /* The last possible modes are keep-alive and tunnel. Tunnel will
5310 * need to forward remaining data. Keep-alive will need to monitor
5311 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005314 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005315 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5316 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 }
5318
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005319 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005321 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5323 goto http_msg_closing;
5324 }
5325 else {
5326 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5327 goto http_msg_closed;
5328 }
5329 }
5330 goto wait_other_side;
5331 }
5332
5333 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5334 http_msg_closing:
5335 /* nothing else to forward, just waiting for the output buffer
5336 * to be empty and for the shutw_now to take effect.
5337 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005338 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5340 goto http_msg_closed;
5341 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005342 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005343 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005344 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005345 if (objt_server(s->target))
5346 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 goto wait_other_side;
5348 }
5349 }
5350
5351 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5352 http_msg_closed:
5353 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005354 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005355 channel_auto_close(chn);
5356 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005357 goto wait_other_side;
5358 }
5359
5360 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005361 /* We force the response to leave immediately if we're waiting for the
5362 * other side, since there is no pending shutdown to push it out.
5363 */
5364 if (!channel_is_empty(chn))
5365 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367}
5368
5369
5370/* Resync the request and response state machines. Return 1 if either state
5371 * changes.
5372 */
5373int http_resync_states(struct session *s)
5374{
5375 struct http_txn *txn = &s->txn;
5376 int old_req_state = txn->req.msg_state;
5377 int old_res_state = txn->rsp.msg_state;
5378
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 http_sync_req_state(s);
5380 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 if (!http_sync_res_state(s))
5382 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 if (!http_sync_req_state(s))
5384 break;
5385 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005386
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 /* OK, both state machines agree on a compatible state.
5388 * There are a few cases we're interested in :
5389 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5390 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5391 * directions, so let's simply disable both analysers.
5392 * - HTTP_MSG_CLOSED on the response only means we must abort the
5393 * request.
5394 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5395 * with server-close mode means we've completed one request and we
5396 * must re-initialize the server connection.
5397 */
5398
5399 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5400 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5401 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5402 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005403 s->req.analysers = 0;
5404 channel_auto_close(&s->req);
5405 channel_auto_read(&s->req);
5406 s->res.analysers = 0;
5407 channel_auto_close(&s->res);
5408 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005410 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005411 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005412 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005413 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005414 s->res.analysers = 0;
5415 channel_auto_close(&s->res);
5416 channel_auto_read(&s->res);
5417 s->req.analysers = 0;
5418 channel_abort(&s->req);
5419 channel_auto_close(&s->req);
5420 channel_auto_read(&s->req);
5421 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005423 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5424 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005426 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5427 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5428 /* server-close/keep-alive: terminate this transaction,
5429 * possibly killing the server connection and reinitialize
5430 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 */
5432 http_end_txn_clean_session(s);
5433 }
5434
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 return txn->req.msg_state != old_req_state ||
5436 txn->rsp.msg_state != old_res_state;
5437}
5438
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439/* This function is an analyser which forwards request body (including chunk
5440 * sizes if any). It is called as soon as we must forward, even if we forward
5441 * zero byte. The only situation where it must not be called is when we're in
5442 * tunnel mode and we want to forward till the close. It's used both to forward
5443 * remaining data and to resync after end of body. It expects the msg_state to
5444 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5445 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005446 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005447 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005449int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450{
5451 struct http_txn *txn = &s->txn;
5452 struct http_msg *msg = &s->txn.req;
5453
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005454 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5455 return 0;
5456
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005457 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005458 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005459 /* Output closed while we were sending data. We must abort and
5460 * wake the other side up.
5461 */
5462 msg->msg_state = HTTP_MSG_ERROR;
5463 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005464 return 1;
5465 }
5466
Willy Tarreaud98cf932009-12-27 22:54:55 +01005467 /* Note that we don't have to send 100-continue back because we don't
5468 * need the data to complete our job, and it's up to the server to
5469 * decide whether to return 100, 417 or anything else in return of
5470 * an "Expect: 100-continue" header.
5471 */
5472
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005473 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005474 /* we have msg->sov which points to the first byte of message
5475 * body, and req->buf.p still points to the beginning of the
5476 * message. We forward the headers now, as we don't need them
5477 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005479 b_adv(req->buf, msg->sov);
5480 msg->next -= msg->sov;
5481 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005482
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005483 /* The previous analysers guarantee that the state is somewhere
5484 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5485 * msg->next are always correct.
5486 */
5487 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5488 if (msg->flags & HTTP_MSGF_TE_CHNK)
5489 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5490 else
5491 msg->msg_state = HTTP_MSG_DATA;
5492 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005493 }
5494
Willy Tarreau7ba23542014-04-17 21:50:00 +02005495 /* Some post-connect processing might want us to refrain from starting to
5496 * forward data. Currently, the only reason for this is "balance url_param"
5497 * whichs need to parse/process the request after we've enabled forwarding.
5498 */
5499 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005500 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005501 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005502 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005503 goto missing_data;
5504 }
5505 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5506 }
5507
Willy Tarreau80a92c02014-03-12 10:41:13 +01005508 /* in most states, we should abort in case of early close */
5509 channel_auto_close(req);
5510
Willy Tarreauefdf0942014-04-24 20:08:57 +02005511 if (req->to_forward) {
5512 /* We can't process the buffer's contents yet */
5513 req->flags |= CF_WAKE_WRITE;
5514 goto missing_data;
5515 }
5516
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005518 if (msg->msg_state == HTTP_MSG_DATA) {
5519 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005520 /* we may have some pending data starting at req->buf->p */
5521 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005522 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005523 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005524 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005525 msg->next += msg->chunk_len;
5526 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005527
5528 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005529 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005530 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005532 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005533 }
5534 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005535 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005536 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537 * TRAILERS state.
5538 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005539 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005540
Willy Tarreau54d23df2012-10-25 19:04:45 +02005541 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005543 else if (ret < 0) {
5544 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005545 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005546 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005547 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005548 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005549 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005551 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005552 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005553 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554
5555 if (ret == 0)
5556 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005557 else if (ret < 0) {
5558 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005559 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005560 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005562 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005563 /* we're in MSG_CHUNK_SIZE now */
5564 }
5565 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005566 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567
5568 if (ret == 0)
5569 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005570 else if (ret < 0) {
5571 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005572 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005573 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005575 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005576 /* we're in HTTP_MSG_DONE now */
5577 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005579 int old_state = msg->msg_state;
5580
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005582
5583 /* we may have some pending data starting at req->buf->p
5584 * such as last chunk of data or trailers.
5585 */
5586 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005587 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005588 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005589 msg->next = 0;
5590
Willy Tarreau4fe41902010-06-07 22:27:41 +02005591 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005592 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5593 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005594 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 if (http_resync_states(s)) {
5596 /* some state changes occurred, maybe the analyser
5597 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005598 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005599 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005600 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005601 /* request errors are most likely due to
5602 * the server aborting the transfer.
5603 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005604 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005605 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005606 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005607 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005608 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005609 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005610 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005611 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005612
5613 /* If "option abortonclose" is set on the backend, we
5614 * want to monitor the client's connection and forward
5615 * any shutdown notification to the server, which will
5616 * decide whether to close or to go on processing the
5617 * request.
5618 */
5619 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005620 channel_auto_read(req);
5621 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005622 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005623 else if (s->txn.meth == HTTP_METH_POST) {
5624 /* POST requests may require to read extra CRLF
5625 * sent by broken browsers and which could cause
5626 * an RST to be sent upon close on some systems
5627 * (eg: Linux).
5628 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005629 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005630 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005631
Willy Tarreau610ecce2010-01-04 21:15:02 +01005632 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005633 }
5634 }
5635
Willy Tarreaud98cf932009-12-27 22:54:55 +01005636 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005637 /* we may have some pending data starting at req->buf->p */
5638 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005639 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005640 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5641
Willy Tarreaubed410e2014-04-22 08:19:34 +02005642 msg->next = 0;
5643 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5644
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005645 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005646 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005647 if (!(s->flags & SN_ERR_MASK))
5648 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005649 if (!(s->flags & SN_FINST_MASK)) {
5650 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5651 s->flags |= SN_FINST_H;
5652 else
5653 s->flags |= SN_FINST_D;
5654 }
5655
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005656 s->fe->fe_counters.cli_aborts++;
5657 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005658 if (objt_server(s->target))
5659 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005660
5661 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005662 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005663
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005664 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005665 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005666 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005667
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005668 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005669 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005670 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005671 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005672 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005673
Willy Tarreau5c620922011-05-11 19:56:11 +02005674 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005675 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005676 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005677 * modes are already handled by the stream sock layer. We must not do
5678 * this in content-length mode because it could present the MSG_MORE
5679 * flag with the last block of forwarded data, which would cause an
5680 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005681 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005682 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005683 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005684
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005685 return 0;
5686
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005688 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005689 if (s->listener->counters)
5690 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005691
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005692 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005693 /* we may have some pending data starting at req->buf->p */
5694 b_adv(req->buf, msg->next);
5695 msg->next = 0;
5696
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697 txn->req.msg_state = HTTP_MSG_ERROR;
5698 if (txn->status) {
5699 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005700 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005701 } else {
5702 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005703 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005704 }
5705 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005706 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005707
5708 if (!(s->flags & SN_ERR_MASK))
5709 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005710 if (!(s->flags & SN_FINST_MASK)) {
5711 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5712 s->flags |= SN_FINST_H;
5713 else
5714 s->flags |= SN_FINST_D;
5715 }
5716 return 0;
5717
5718 aborted_xfer:
5719 txn->req.msg_state = HTTP_MSG_ERROR;
5720 if (txn->status) {
5721 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005722 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005723 } else {
5724 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005725 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005726 }
5727 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005728 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005730 s->fe->fe_counters.srv_aborts++;
5731 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005732 if (objt_server(s->target))
5733 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005734
5735 if (!(s->flags & SN_ERR_MASK))
5736 s->flags |= SN_ERR_SRVCL;
5737 if (!(s->flags & SN_FINST_MASK)) {
5738 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5739 s->flags |= SN_FINST_H;
5740 else
5741 s->flags |= SN_FINST_D;
5742 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 return 0;
5744}
5745
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5747 * processing can continue on next analysers, or zero if it either needs more
5748 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005749 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005750 * when it has nothing left to do, and may remove any analyser when it wants to
5751 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005752 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005753int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005754{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005755 struct http_txn *txn = &s->txn;
5756 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005757 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005758 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005760 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005761
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005762 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 +02005763 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005764 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005765 rep,
5766 rep->rex, rep->wex,
5767 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005768 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005769 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005771 /*
5772 * Now parse the partial (or complete) lines.
5773 * We will check the response syntax, and also join multi-line
5774 * headers. An index of all the lines will be elaborated while
5775 * parsing.
5776 *
5777 * For the parsing, we use a 28 states FSM.
5778 *
5779 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005780 * rep->buf->p = beginning of response
5781 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5782 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005783 * msg->eol = end of current header or line (LF or CRLF)
5784 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 */
5786
Willy Tarreau628c40c2014-04-24 19:11:26 +02005787 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005788 /* There's a protected area at the end of the buffer for rewriting
5789 * purposes. We don't want to start to parse the request if the
5790 * protected area is affected, because we may have to move processed
5791 * data later, which is much more complicated.
5792 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005793 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005794 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005795 /* some data has still not left the buffer, wake us once that's done */
5796 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5797 goto abort_response;
5798 channel_dont_close(rep);
5799 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005800 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005801 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005802 }
5803
Willy Tarreau379357a2013-06-08 12:55:46 +02005804 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5805 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5806 buffer_slow_realign(rep->buf);
5807
Willy Tarreau9b28e032012-10-12 23:49:43 +02005808 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005809 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005810 }
5811
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005812 /* 1: we might have to print this header in debug mode */
5813 if (unlikely((global.mode & MODE_DEBUG) &&
5814 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005815 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005817
Willy Tarreau9b28e032012-10-12 23:49:43 +02005818 sol = rep->buf->p;
5819 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005821
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005822 sol += hdr_idx_first_pos(&txn->hdr_idx);
5823 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005824
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005825 while (cur_idx) {
5826 eol = sol + txn->hdr_idx.v[cur_idx].len;
5827 debug_hdr("srvhdr", s, sol, eol);
5828 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5829 cur_idx = txn->hdr_idx.v[cur_idx].next;
5830 }
5831 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005832
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005833 /*
5834 * Now we quickly check if we have found a full valid response.
5835 * If not so, we check the FD and buffer states before leaving.
5836 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005837 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005838 * responses are checked first.
5839 *
5840 * Depending on whether the client is still there or not, we
5841 * may send an error response back or not. Note that normally
5842 * we should only check for HTTP status there, and check I/O
5843 * errors somewhere else.
5844 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845
Willy Tarreau655dce92009-11-08 13:10:58 +01005846 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005847 /* Invalid response */
5848 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5849 /* we detected a parsing error. We want to archive this response
5850 * in the dedicated proxy area for later troubleshooting.
5851 */
5852 hdr_response_bad:
5853 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005854 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005855
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005856 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005857 if (objt_server(s->target)) {
5858 objt_server(s->target)->counters.failed_resp++;
5859 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005860 }
Willy Tarreau64648412010-03-05 10:41:54 +01005861 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005862 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005863 rep->analysers = 0;
5864 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005865 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005866 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005867 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868
5869 if (!(s->flags & SN_ERR_MASK))
5870 s->flags |= SN_ERR_PRXCOND;
5871 if (!(s->flags & SN_FINST_MASK))
5872 s->flags |= SN_FINST_H;
5873
5874 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005875 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005876
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005878 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005879 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005880 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881 goto hdr_response_bad;
5882 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005883
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005884 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005885 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005887 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005888 else if (txn->flags & TX_NOT_FIRST)
5889 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005890
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005891 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005892 if (objt_server(s->target)) {
5893 objt_server(s->target)->counters.failed_resp++;
5894 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005895 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005896
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005897 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005898 rep->analysers = 0;
5899 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005900 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005901 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005902 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005903
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005904 if (!(s->flags & SN_ERR_MASK))
5905 s->flags |= SN_ERR_SRVCL;
5906 if (!(s->flags & SN_FINST_MASK))
5907 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005908 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005910
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005911 /* read timeout : return a 504 to the client. */
5912 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005914 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005915 else if (txn->flags & TX_NOT_FIRST)
5916 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005917
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005918 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005919 if (objt_server(s->target)) {
5920 objt_server(s->target)->counters.failed_resp++;
5921 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005922 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005923
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005924 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005925 rep->analysers = 0;
5926 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005927 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005928 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005929 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005930
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 if (!(s->flags & SN_ERR_MASK))
5932 s->flags |= SN_ERR_SRVTO;
5933 if (!(s->flags & SN_FINST_MASK))
5934 s->flags |= SN_FINST_H;
5935 return 0;
5936 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005937
Willy Tarreauf003d372012-11-26 13:35:37 +01005938 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005939 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005940 s->fe->fe_counters.cli_aborts++;
5941 s->be->be_counters.cli_aborts++;
5942 if (objt_server(s->target))
5943 objt_server(s->target)->counters.cli_aborts++;
5944
5945 rep->analysers = 0;
5946 channel_auto_close(rep);
5947
5948 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005949 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005950 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005951
5952 if (!(s->flags & SN_ERR_MASK))
5953 s->flags |= SN_ERR_CLICL;
5954 if (!(s->flags & SN_FINST_MASK))
5955 s->flags |= SN_FINST_H;
5956
5957 /* process_session() will take care of the error */
5958 return 0;
5959 }
5960
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005961 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005962 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005963 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005964 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005965 else if (txn->flags & TX_NOT_FIRST)
5966 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005967
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005968 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005969 if (objt_server(s->target)) {
5970 objt_server(s->target)->counters.failed_resp++;
5971 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005972 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005973
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005974 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975 rep->analysers = 0;
5976 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005977 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005978 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005979 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005980
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 if (!(s->flags & SN_ERR_MASK))
5982 s->flags |= SN_ERR_SRVCL;
5983 if (!(s->flags & SN_FINST_MASK))
5984 s->flags |= SN_FINST_H;
5985 return 0;
5986 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005987
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005988 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005989 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005991 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005992 else if (txn->flags & TX_NOT_FIRST)
5993 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005994
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005995 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005996 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005997 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998
5999 if (!(s->flags & SN_ERR_MASK))
6000 s->flags |= SN_ERR_CLICL;
6001 if (!(s->flags & SN_FINST_MASK))
6002 s->flags |= SN_FINST_H;
6003
6004 /* process_session() will take care of the error */
6005 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006006 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006007
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006008 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006009 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006010 return 0;
6011 }
6012
6013 /* More interesting part now : we know that we have a complete
6014 * response which at least looks like HTTP. We have an indicator
6015 * of each header's length, so we can parse them quickly.
6016 */
6017
6018 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006019 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 /*
6022 * 1: get the status code
6023 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006024 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 if (n < 1 || n > 5)
6026 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006027 /* when the client triggers a 4xx from the server, it's most often due
6028 * to a missing object or permission. These events should be tracked
6029 * because if they happen often, it may indicate a brute force or a
6030 * vulnerability scan.
6031 */
6032 if (n == 4)
6033 session_inc_http_err_ctr(s);
6034
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006035 if (objt_server(s->target))
6036 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037
Willy Tarreau5b154472009-12-21 20:11:07 +01006038 /* check if the response is HTTP/1.1 or above */
6039 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006040 ((rep->buf->p[5] > '1') ||
6041 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006042 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006043
6044 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006045 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 +01006046
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006047 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006048 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006049
Willy Tarreau9b28e032012-10-12 23:49:43 +02006050 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051
Willy Tarreau39650402010-03-15 19:44:39 +01006052 /* Adjust server's health based on status code. Note: status codes 501
6053 * and 505 are triggered on demand by client request, so we must not
6054 * count them as server failures.
6055 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006056 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006057 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006059 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006060 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006061 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006062
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006063 /*
6064 * 2: check for cacheability.
6065 */
6066
6067 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006068 case 100:
6069 /*
6070 * We may be facing a 100-continue response, in which case this
6071 * is not the right response, and we're waiting for the next one.
6072 * Let's allow this response to go to the client and wait for the
6073 * next one.
6074 */
6075 hdr_idx_init(&txn->hdr_idx);
6076 msg->next -= channel_forward(rep, msg->next);
6077 msg->msg_state = HTTP_MSG_RPBEFORE;
6078 txn->status = 0;
6079 s->logs.t_data = -1; /* was not a response yet */
6080 goto next_one;
6081
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082 case 200:
6083 case 203:
6084 case 206:
6085 case 300:
6086 case 301:
6087 case 410:
6088 /* RFC2616 @13.4:
6089 * "A response received with a status code of
6090 * 200, 203, 206, 300, 301 or 410 MAY be stored
6091 * by a cache (...) unless a cache-control
6092 * directive prohibits caching."
6093 *
6094 * RFC2616 @9.5: POST method :
6095 * "Responses to this method are not cacheable,
6096 * unless the response includes appropriate
6097 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006098 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006100 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6102 break;
6103 default:
6104 break;
6105 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006106
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006107 /*
6108 * 3: we may need to capture headers
6109 */
6110 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006111 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006112 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006113 txn->rsp.cap, s->fe->rsp_cap);
6114
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006115 /* 4: determine the transfer-length.
6116 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6117 * the presence of a message-body in a RESPONSE and its transfer length
6118 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006119 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006120 * All responses to the HEAD request method MUST NOT include a
6121 * message-body, even though the presence of entity-header fields
6122 * might lead one to believe they do. All 1xx (informational), 204
6123 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6124 * message-body. All other responses do include a message-body,
6125 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006127 * 1. Any response which "MUST NOT" include a message-body (such as the
6128 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6129 * always terminated by the first empty line after the header fields,
6130 * regardless of the entity-header fields present in the message.
6131 *
6132 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6133 * the "chunked" transfer-coding (Section 6.2) is used, the
6134 * transfer-length is defined by the use of this transfer-coding.
6135 * If a Transfer-Encoding header field is present and the "chunked"
6136 * transfer-coding is not present, the transfer-length is defined by
6137 * the sender closing the connection.
6138 *
6139 * 3. If a Content-Length header field is present, its decimal value in
6140 * OCTETs represents both the entity-length and the transfer-length.
6141 * If a message is received with both a Transfer-Encoding header
6142 * field and a Content-Length header field, the latter MUST be ignored.
6143 *
6144 * 4. If the message uses the media type "multipart/byteranges", and
6145 * the transfer-length is not otherwise specified, then this self-
6146 * delimiting media type defines the transfer-length. This media
6147 * type MUST NOT be used unless the sender knows that the recipient
6148 * can parse it; the presence in a request of a Range header with
6149 * multiple byte-range specifiers from a 1.1 client implies that the
6150 * client can parse multipart/byteranges responses.
6151 *
6152 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153 */
6154
6155 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006156 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006157 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 * FIXME: should we parse anyway and return an error on chunked encoding ?
6159 */
6160 if (txn->meth == HTTP_METH_HEAD ||
6161 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006162 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006163 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006164 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006165 goto skip_content_length;
6166 }
6167
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006168 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006169 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006170 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006171 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006172 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006173 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6174 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006175 /* bad transfer-encoding (chunked followed by something else) */
6176 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006177 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006178 break;
6179 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006180 }
6181
6182 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6183 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006184 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006185 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006186 signed long long cl;
6187
Willy Tarreauad14f752011-09-02 20:33:27 +02006188 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006189 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006190 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006191 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006192
Willy Tarreauad14f752011-09-02 20:33:27 +02006193 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006194 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006195 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006196 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006197
Willy Tarreauad14f752011-09-02 20:33:27 +02006198 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006199 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006200 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006201 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006202
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006203 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006204 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006205 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006206 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006207
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006208 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006209 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006210 }
6211
William Lallemand82fe75c2012-10-23 10:25:10 +02006212 if (s->fe->comp || s->be->comp)
6213 select_compression_response_header(s, rep->buf);
6214
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006215skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006216 /* Now we have to check if we need to modify the Connection header.
6217 * This is more difficult on the response than it is on the request,
6218 * because we can have two different HTTP versions and we don't know
6219 * how the client will interprete a response. For instance, let's say
6220 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6221 * HTTP/1.1 response without any header. Maybe it will bound itself to
6222 * HTTP/1.0 because it only knows about it, and will consider the lack
6223 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6224 * the lack of header as a keep-alive. Thus we will use two flags
6225 * indicating how a request MAY be understood by the client. In case
6226 * of multiple possibilities, we'll fix the header to be explicit. If
6227 * ambiguous cases such as both close and keepalive are seen, then we
6228 * will fall back to explicit close. Note that we won't take risks with
6229 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006230 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006231 */
6232
Willy Tarreaudc008c52010-02-01 16:20:08 +01006233 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6234 txn->status == 101)) {
6235 /* Either we've established an explicit tunnel, or we're
6236 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006237 * to understand the next protocols. We have to switch to tunnel
6238 * mode, so that we transfer the request and responses then let
6239 * this protocol pass unmodified. When we later implement specific
6240 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006241 * header which contains information about that protocol for
6242 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006243 */
6244 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6245 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006246 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6247 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006248 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6249 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006250 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006251
Willy Tarreau70dffda2014-01-30 03:07:23 +01006252 /* this situation happens when combining pretend-keepalive with httpclose. */
6253 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006254 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6255 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006256 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6257
Willy Tarreau60466522010-01-18 19:08:45 +01006258 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006259 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006260 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6261 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006262
Willy Tarreau60466522010-01-18 19:08:45 +01006263 /* now adjust header transformations depending on current state */
6264 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6265 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6266 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006267 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006268 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006269 }
Willy Tarreau60466522010-01-18 19:08:45 +01006270 else { /* SCL / KAL */
6271 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006272 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006273 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006274 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006275
Willy Tarreau60466522010-01-18 19:08:45 +01006276 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006277 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006278
Willy Tarreau60466522010-01-18 19:08:45 +01006279 /* Some keep-alive responses are converted to Server-close if
6280 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006281 */
Willy Tarreau60466522010-01-18 19:08:45 +01006282 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6283 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006284 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006285 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006286 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006287 }
6288
Willy Tarreau7959a552013-09-23 16:44:27 +02006289 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006290 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006291
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006292 /* end of job, return OK */
6293 rep->analysers &= ~an_bit;
6294 rep->analyse_exp = TICK_ETERNITY;
6295 channel_auto_close(rep);
6296 return 1;
6297
6298 abort_keep_alive:
6299 /* A keep-alive request to the server failed on a network error.
6300 * The client is required to retry. We need to close without returning
6301 * any other information so that the client retries.
6302 */
6303 txn->status = 0;
6304 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006305 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006306 channel_auto_close(rep);
6307 s->logs.logwait = 0;
6308 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006309 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006310 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006311 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006312 return 0;
6313}
6314
6315/* This function performs all the processing enabled for the current response.
6316 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006317 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006318 * other functions. It works like process_request (see indications above).
6319 */
6320int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6321{
6322 struct http_txn *txn = &s->txn;
6323 struct http_msg *msg = &txn->rsp;
6324 struct proxy *cur_proxy;
6325 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006326 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006327
6328 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6329 now_ms, __FUNCTION__,
6330 s,
6331 rep,
6332 rep->rex, rep->wex,
6333 rep->flags,
6334 rep->buf->i,
6335 rep->analysers);
6336
6337 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6338 return 0;
6339
Willy Tarreau70730dd2014-04-24 18:06:27 +02006340 /* The stats applet needs to adjust the Connection header but we don't
6341 * apply any filter there.
6342 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006343 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6344 rep->analysers &= ~an_bit;
6345 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006346 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006347 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006348
Willy Tarreau58975672014-04-24 21:13:57 +02006349 /*
6350 * We will have to evaluate the filters.
6351 * As opposed to version 1.2, now they will be evaluated in the
6352 * filters order and not in the header order. This means that
6353 * each filter has to be validated among all headers.
6354 *
6355 * Filters are tried with ->be first, then with ->fe if it is
6356 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006357 *
6358 * Maybe we are in resume condiion. In this case I choose the
6359 * "struct proxy" which contains the rule list matching the resume
6360 * pointer. If none of theses "struct proxy" match, I initialise
6361 * the process with the first one.
6362 *
6363 * In fact, I check only correspondance betwwen the current list
6364 * pointer and the ->fe rule list. If it doesn't match, I initialize
6365 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006366 */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006367 if (s->current_rule_list == &s->fe->http_res_rules)
6368 cur_proxy = s->fe;
6369 else
6370 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006371 while (1) {
6372 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006373
Willy Tarreau58975672014-04-24 21:13:57 +02006374 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006375 if (ret == HTTP_RULE_RES_CONT)
6376 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006377
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006378 /* we need to be called again. */
6379 if (ret == HTTP_RULE_RES_YIELD) {
6380 channel_dont_close(rep);
6381 return 0;
6382 }
6383
Willy Tarreau58975672014-04-24 21:13:57 +02006384 /* try headers filters */
6385 if (rule_set->rsp_exp != NULL) {
6386 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6387 return_bad_resp:
6388 if (objt_server(s->target)) {
6389 objt_server(s->target)->counters.failed_resp++;
6390 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006391 }
Willy Tarreau58975672014-04-24 21:13:57 +02006392 s->be->be_counters.failed_resp++;
6393 return_srv_prx_502:
6394 rep->analysers = 0;
6395 txn->status = 502;
6396 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006397 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006398 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006399 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau58975672014-04-24 21:13:57 +02006400 if (!(s->flags & SN_ERR_MASK))
6401 s->flags |= SN_ERR_PRXCOND;
6402 if (!(s->flags & SN_FINST_MASK))
6403 s->flags |= SN_FINST_H;
6404 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006405 }
Willy Tarreau58975672014-04-24 21:13:57 +02006406 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006407
Willy Tarreau58975672014-04-24 21:13:57 +02006408 /* has the response been denied ? */
6409 if (txn->flags & TX_SVDENY) {
6410 if (objt_server(s->target))
6411 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006412
Willy Tarreau58975672014-04-24 21:13:57 +02006413 s->be->be_counters.denied_resp++;
6414 s->fe->fe_counters.denied_resp++;
6415 if (s->listener->counters)
6416 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 goto return_srv_prx_502;
6419 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 /* add response headers from the rule sets in the same order */
6422 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006423 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006424 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006425 if (wl->cond) {
6426 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6427 ret = acl_pass(ret);
6428 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6429 ret = !ret;
6430 if (!ret)
6431 continue;
6432 }
6433 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6434 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006435 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006436
Willy Tarreau58975672014-04-24 21:13:57 +02006437 /* check whether we're already working on the frontend */
6438 if (cur_proxy == s->fe)
6439 break;
6440 cur_proxy = s->fe;
6441 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006442
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006443 /* After this point, this anayzer can't return yield, so we can
6444 * remove the bit corresponding to this analyzer from the list.
6445 *
6446 * Note that the intermediate returns and goto found previously
6447 * reset the analyzers.
6448 */
6449 rep->analysers &= ~an_bit;
6450 rep->analyse_exp = TICK_ETERNITY;
6451
Willy Tarreau58975672014-04-24 21:13:57 +02006452 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006453 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006454 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006455
Willy Tarreau58975672014-04-24 21:13:57 +02006456 /*
6457 * Now check for a server cookie.
6458 */
6459 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6460 (s->be->options & PR_O_CHK_CACHE))
6461 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006462
Willy Tarreau58975672014-04-24 21:13:57 +02006463 /*
6464 * Check for cache-control or pragma headers if required.
6465 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006466 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 +02006467 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006468
Willy Tarreau58975672014-04-24 21:13:57 +02006469 /*
6470 * Add server cookie in the response if needed
6471 */
6472 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6473 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6474 (!(s->flags & SN_DIRECT) ||
6475 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6476 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6477 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6478 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6479 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6480 !(s->flags & SN_IGNORE_PRST)) {
6481 /* the server is known, it's not the one the client requested, or the
6482 * cookie's last seen date needs to be refreshed. We have to
6483 * insert a set-cookie here, except if we want to insert only on POST
6484 * requests and this one isn't. Note that servers which don't have cookies
6485 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006486 */
Willy Tarreau58975672014-04-24 21:13:57 +02006487 if (!objt_server(s->target)->cookie) {
6488 chunk_printf(&trash,
6489 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6490 s->be->cookie_name);
6491 }
6492 else {
6493 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494
Willy Tarreau58975672014-04-24 21:13:57 +02006495 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6496 /* emit last_date, which is mandatory */
6497 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6498 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6499 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006500
Willy Tarreau58975672014-04-24 21:13:57 +02006501 if (s->be->cookie_maxlife) {
6502 /* emit first_date, which is either the original one or
6503 * the current date.
6504 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006505 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006506 s30tob64(txn->cookie_first_date ?
6507 txn->cookie_first_date >> 2 :
6508 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006509 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006510 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006511 }
Willy Tarreau58975672014-04-24 21:13:57 +02006512 chunk_appendf(&trash, "; path=/");
6513 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006514
Willy Tarreau58975672014-04-24 21:13:57 +02006515 if (s->be->cookie_domain)
6516 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006517
Willy Tarreau58975672014-04-24 21:13:57 +02006518 if (s->be->ck_opts & PR_CK_HTTPONLY)
6519 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006520
Willy Tarreau58975672014-04-24 21:13:57 +02006521 if (s->be->ck_opts & PR_CK_SECURE)
6522 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006523
Willy Tarreau58975672014-04-24 21:13:57 +02006524 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6525 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 txn->flags &= ~TX_SCK_MASK;
6528 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6529 /* the server did not change, only the date was updated */
6530 txn->flags |= TX_SCK_UPDATED;
6531 else
6532 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006533
Willy Tarreau58975672014-04-24 21:13:57 +02006534 /* Here, we will tell an eventual cache on the client side that we don't
6535 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6536 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6537 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006538 */
Willy Tarreau58975672014-04-24 21:13:57 +02006539 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006540
Willy Tarreau58975672014-04-24 21:13:57 +02006541 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006542
Willy Tarreau58975672014-04-24 21:13:57 +02006543 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6544 "Cache-control: private", 22) < 0))
6545 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006546 }
Willy Tarreau58975672014-04-24 21:13:57 +02006547 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548
Willy Tarreau58975672014-04-24 21:13:57 +02006549 /*
6550 * Check if result will be cacheable with a cookie.
6551 * We'll block the response if security checks have caught
6552 * nasty things such as a cacheable cookie.
6553 */
6554 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6555 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6556 (s->be->options & PR_O_CHK_CACHE)) {
6557 /* we're in presence of a cacheable response containing
6558 * a set-cookie header. We'll block it as requested by
6559 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006560 */
Willy Tarreau58975672014-04-24 21:13:57 +02006561 if (objt_server(s->target))
6562 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006563
Willy Tarreau58975672014-04-24 21:13:57 +02006564 s->be->be_counters.denied_resp++;
6565 s->fe->fe_counters.denied_resp++;
6566 if (s->listener->counters)
6567 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568
Willy Tarreau58975672014-04-24 21:13:57 +02006569 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6570 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6571 send_log(s->be, LOG_ALERT,
6572 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6573 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6574 goto return_srv_prx_502;
6575 }
Willy Tarreau03945942009-12-22 16:50:27 +01006576
Willy Tarreau70730dd2014-04-24 18:06:27 +02006577 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006578 /*
6579 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6580 * If an "Upgrade" token is found, the header is left untouched in order
6581 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006582 * if anything but "Upgrade" is present in the Connection header. We don't
6583 * want to touch any 101 response either since it's switching to another
6584 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006585 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006586 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006587 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6588 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6589 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6590 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006591
Willy Tarreau58975672014-04-24 21:13:57 +02006592 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6593 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6594 /* we want a keep-alive response here. Keep-alive header
6595 * required if either side is not 1.1.
6596 */
6597 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6598 want_flags |= TX_CON_KAL_SET;
6599 }
6600 else {
6601 /* we want a close response here. Close header required if
6602 * the server is 1.1, regardless of the client.
6603 */
6604 if (msg->flags & HTTP_MSGF_VER_11)
6605 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006606 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607
Willy Tarreau58975672014-04-24 21:13:57 +02006608 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6609 http_change_connection_header(txn, msg, want_flags);
6610 }
6611
6612 skip_header_mangling:
6613 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6614 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6615 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
Willy Tarreau58975672014-04-24 21:13:57 +02006617 /* if the user wants to log as soon as possible, without counting
6618 * bytes from the server, then this is the right moment. We have
6619 * to temporarily assign bytes_out to log what we currently have.
6620 */
6621 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6622 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6623 s->logs.bytes_out = txn->rsp.eoh;
6624 s->do_log(s);
6625 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006626 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006627 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006628}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629
Willy Tarreaud98cf932009-12-27 22:54:55 +01006630/* This function is an analyser which forwards response body (including chunk
6631 * sizes if any). It is called as soon as we must forward, even if we forward
6632 * zero byte. The only situation where it must not be called is when we're in
6633 * tunnel mode and we want to forward till the close. It's used both to forward
6634 * remaining data and to resync after end of body. It expects the msg_state to
6635 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6636 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006637 *
6638 * It is capable of compressing response data both in content-length mode and
6639 * in chunked mode. The state machines follows different flows depending on
6640 * whether content-length and chunked modes are used, since there are no
6641 * trailers in content-length :
6642 *
6643 * chk-mode cl-mode
6644 * ,----- BODY -----.
6645 * / \
6646 * V size > 0 V chk-mode
6647 * .--> SIZE -------------> DATA -------------> CRLF
6648 * | | size == 0 | last byte |
6649 * | v final crlf v inspected |
6650 * | TRAILERS -----------> DONE |
6651 * | |
6652 * `----------------------------------------------'
6653 *
6654 * Compression only happens in the DATA state, and must be flushed in final
6655 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6656 * is performed at once on final states for all bytes parsed, or when leaving
6657 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006658 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006659int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006660{
6661 struct http_txn *txn = &s->txn;
6662 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006663 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006664 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006665 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006666
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006667 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6668 return 0;
6669
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006670 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006671 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006672 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006673 /* Output closed while we were sending data. We must abort and
6674 * wake the other side up.
6675 */
6676 msg->msg_state = HTTP_MSG_ERROR;
6677 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006678 return 1;
6679 }
6680
Willy Tarreau4fe41902010-06-07 22:27:41 +02006681 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006682 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006683
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006684 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006685 /* we have msg->sov which points to the first byte of message
6686 * body, and res->buf.p still points to the beginning of the
6687 * message. We forward the headers now, as we don't need them
6688 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006689 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006690 b_adv(res->buf, msg->sov);
6691 msg->next -= msg->sov;
6692 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006693
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006694 /* The previous analysers guarantee that the state is somewhere
6695 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6696 * msg->next are always correct.
6697 */
6698 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6699 if (msg->flags & HTTP_MSGF_TE_CHNK)
6700 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6701 else
6702 msg->msg_state = HTTP_MSG_DATA;
6703 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006704 }
6705
Willy Tarreauefdf0942014-04-24 20:08:57 +02006706 if (res->to_forward) {
6707 /* We can't process the buffer's contents yet */
6708 res->flags |= CF_WAKE_WRITE;
6709 goto missing_data;
6710 }
6711
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006712 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6713 /* We need a compression buffer in the DATA state to put the
6714 * output of compressed data, and in CRLF state to let the
6715 * TRAILERS state finish the job of removing the trailing CRLF.
6716 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006717 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006718 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006719 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006720 goto aborted_xfer; /* no memory */
6721 }
6722
6723 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006724 if (ret < 0) {
6725 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006726 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006727 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006728 compressing = 1;
6729 }
6730
Willy Tarreaud98cf932009-12-27 22:54:55 +01006731 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006732 switch (msg->msg_state - HTTP_MSG_DATA) {
6733 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006734 /* we may have some pending data starting at res->buf->p */
6735 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006736 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006737 if (ret < 0)
6738 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006739
Willy Tarreaud5a67832014-04-21 10:54:27 +02006740 if (msg->chunk_len) {
6741 /* input empty or output full */
6742 if (res->buf->i > msg->next)
6743 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006744 goto missing_data;
6745 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006746 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006747 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006748 if (msg->chunk_len > res->buf->i - msg->next) {
6749 /* output full */
6750 res->flags |= CF_WAKE_WRITE;
6751 goto missing_data;
6752 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006753 msg->next += msg->chunk_len;
6754 msg->chunk_len = 0;
6755 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006756
6757 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006758 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006759 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006760 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006761 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006762 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006763 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006764 /* fall through for HTTP_MSG_CHUNK_CRLF */
6765
6766 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6767 /* we want the CRLF after the data */
6768
6769 ret = http_skip_chunk_crlf(msg);
6770 if (ret == 0)
6771 goto missing_data;
6772 else if (ret < 0) {
6773 if (msg->err_pos >= 0)
6774 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6775 goto return_bad_res;
6776 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006777 /* we're in MSG_CHUNK_SIZE now, fall through */
6778
6779 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006780 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006781 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006782 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006783 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006784
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006785 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006786 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006787 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006788 else if (ret < 0) {
6789 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006790 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006791 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006792 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006793 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006794 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006795
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006796 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006797 if (unlikely(compressing)) {
6798 /* we need to flush output contents before syncing FSMs */
6799 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6800 compressing = 0;
6801 }
6802
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006803 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006804 if (ret == 0)
6805 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006806 else if (ret < 0) {
6807 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006808 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006809 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006810 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006811 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006812
6813 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006814 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006815 if (unlikely(compressing)) {
6816 /* we need to flush output contents before syncing FSMs */
6817 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6818 compressing = 0;
6819 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006820
Willy Tarreauc623c172014-04-18 09:53:50 +02006821 /* we may have some pending data starting at res->buf->p
6822 * such as a last chunk of data or trailers.
6823 */
6824 b_adv(res->buf, msg->next);
6825 msg->next = 0;
6826
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006827 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006828 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006829 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6830 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006831 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006832
Willy Tarreau610ecce2010-01-04 21:15:02 +01006833 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006834 /* some state changes occurred, maybe the analyser
6835 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006836 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006837 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006838 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006839 /* response errors are most likely due to
6840 * the client aborting the transfer.
6841 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006842 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006843 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006844 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006845 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006846 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006847 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006848 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006849 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006850 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006851 }
6852 }
6853
Willy Tarreaud98cf932009-12-27 22:54:55 +01006854 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006855 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006856 if (unlikely(compressing)) {
6857 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006858 compressing = 0;
6859 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006860
Willy Tarreauc623c172014-04-18 09:53:50 +02006861 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6862 b_adv(res->buf, msg->next);
6863 msg->next = 0;
6864 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6865 }
6866
Willy Tarreauf003d372012-11-26 13:35:37 +01006867 if (res->flags & CF_SHUTW)
6868 goto aborted_xfer;
6869
6870 /* stop waiting for data if the input is closed before the end. If the
6871 * client side was already closed, it means that the client has aborted,
6872 * so we don't want to count this as a server abort. Otherwise it's a
6873 * server abort.
6874 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006875 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006876 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006877 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006878 if (!(s->flags & SN_ERR_MASK))
6879 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006880 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006881 if (objt_server(s->target))
6882 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006883 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006884 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006885
Willy Tarreau40dba092010-03-04 18:14:51 +01006886 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006887 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006888 goto return_bad_res;
6889
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006890 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006891 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006892 * Similarly, with keep-alive on the client side, we don't want to forward a
6893 * close.
6894 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006895 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006896 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6897 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006898 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006899
Willy Tarreau5c620922011-05-11 19:56:11 +02006900 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006901 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006902 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006903 * modes are already handled by the stream sock layer. We must not do
6904 * this in content-length mode because it could present the MSG_MORE
6905 * flag with the last block of forwarded data, which would cause an
6906 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006907 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006908 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006909 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006910
Willy Tarreaud98cf932009-12-27 22:54:55 +01006911 /* the session handler will take care of timeouts and errors */
6912 return 0;
6913
Willy Tarreau40dba092010-03-04 18:14:51 +01006914 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006915 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006916 if (objt_server(s->target))
6917 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006918
6919 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006920 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006921 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006922 compressing = 0;
6923 }
6924
Willy Tarreauc623c172014-04-18 09:53:50 +02006925 /* we may have some pending data starting at res->buf->p */
6926 if (s->comp_algo == NULL) {
6927 b_adv(res->buf, msg->next);
6928 msg->next = 0;
6929 }
6930
Willy Tarreaud98cf932009-12-27 22:54:55 +01006931 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006932 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006933 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006934 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006935 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006936 if (objt_server(s->target))
6937 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006938
6939 if (!(s->flags & SN_ERR_MASK))
6940 s->flags |= SN_ERR_PRXCOND;
6941 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006942 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006943 return 0;
6944
6945 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006946 if (unlikely(compressing)) {
6947 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6948 compressing = 0;
6949 }
6950
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006951 txn->rsp.msg_state = HTTP_MSG_ERROR;
6952 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006953 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006954 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006955 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006956
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006957 s->fe->fe_counters.cli_aborts++;
6958 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006959 if (objt_server(s->target))
6960 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006961
6962 if (!(s->flags & SN_ERR_MASK))
6963 s->flags |= SN_ERR_CLICL;
6964 if (!(s->flags & SN_FINST_MASK))
6965 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006966 return 0;
6967}
6968
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969/* Iterate the same filter through all request headers.
6970 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971 * Since it can manage the switch to another backend, it updates the per-proxy
6972 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006974int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976 char *cur_ptr, *cur_end, *cur_next;
6977 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006980 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982 last_hdr = 0;
6983
Willy Tarreau9b28e032012-10-12 23:49:43 +02006984 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006985 old_idx = 0;
6986
6987 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006988 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006990 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 (exp->action == ACT_ALLOW ||
6992 exp->action == ACT_DENY ||
6993 exp->action == ACT_TARPIT))
6994 return 0;
6995
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006996 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 if (!cur_idx)
6998 break;
6999
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007000 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007001 cur_ptr = cur_next;
7002 cur_end = cur_ptr + cur_hdr->len;
7003 cur_next = cur_end + cur_hdr->cr + 1;
7004
7005 /* Now we have one header between cur_ptr and cur_end,
7006 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007007 */
7008
Willy Tarreau15a53a42015-01-21 13:39:42 +01007009 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 switch (exp->action) {
7011 case ACT_SETBE:
7012 /* It is not possible to jump a second time.
7013 * FIXME: should we return an HTTP/500 here so that
7014 * the admin knows there's a problem ?
7015 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007016 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 break;
7018
7019 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007020 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007021 last_hdr = 1;
7022 break;
7023
7024 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007025 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007026 last_hdr = 1;
7027 break;
7028
7029 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007032 break;
7033
7034 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007037 break;
7038
7039 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007040 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7041 if (trash.len < 0)
7042 return -1;
7043
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007044 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 /* FIXME: if the user adds a newline in the replacement, the
7046 * index will not be recalculated for now, and the new line
7047 * will not be counted as a new header.
7048 */
7049
7050 cur_end += delta;
7051 cur_next += delta;
7052 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007053 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007054 break;
7055
7056 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007057 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 cur_next += delta;
7059
Willy Tarreaufa355d42009-11-29 18:12:29 +01007060 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007061 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7062 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007063 cur_hdr->len = 0;
7064 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007065 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 break;
7067
7068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069 }
7070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 /* keep the link from this header to next one in case of later
7072 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 old_idx = cur_idx;
7075 }
7076 return 0;
7077}
7078
7079
7080/* Apply the filter to the request line.
7081 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7082 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 * Since it can manage the switch to another backend, it updates the per-proxy
7084 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007085 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007087{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 char *cur_ptr, *cur_end;
7089 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007090 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007091 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092
Willy Tarreau3d300592007-03-18 18:34:41 +01007093 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007094 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007095 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 (exp->action == ACT_ALLOW ||
7097 exp->action == ACT_DENY ||
7098 exp->action == ACT_TARPIT))
7099 return 0;
7100 else if (exp->action == ACT_REMOVE)
7101 return 0;
7102
7103 done = 0;
7104
Willy Tarreau9b28e032012-10-12 23:49:43 +02007105 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007106 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107
7108 /* Now we have the request line between cur_ptr and cur_end */
7109
Willy Tarreau15a53a42015-01-21 13:39:42 +01007110 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 switch (exp->action) {
7112 case ACT_SETBE:
7113 /* It is not possible to jump a second time.
7114 * FIXME: should we return an HTTP/500 here so that
7115 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007117 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007118 break;
7119
7120 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007121 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 done = 1;
7123 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007125 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007126 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 done = 1;
7128 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007130 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007131 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 done = 1;
7133 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007136 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 done = 1;
7138 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007140 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007141 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7142 if (trash.len < 0)
7143 return -1;
7144
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007145 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007146 /* FIXME: if the user adds a newline in the replacement, the
7147 * index will not be recalculated for now, and the new line
7148 * will not be counted as a new header.
7149 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007150
Willy Tarreaufa355d42009-11-29 18:12:29 +01007151 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007153 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 HTTP_MSG_RQMETH,
7155 cur_ptr, cur_end + 1,
7156 NULL, NULL);
7157 if (unlikely(!cur_end))
7158 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007159
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 /* we have a full request and we know that we have either a CR
7161 * or an LF at <ptr>.
7162 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007163 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7164 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 /* there is no point trying this regex on headers */
7166 return 1;
7167 }
7168 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007169 return done;
7170}
Willy Tarreau97de6242006-12-27 17:18:38 +01007171
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007175 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 * unparsable request. Since it can manage the switch to another backend, it
7178 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007179 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007180int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007182 struct http_txn *txn = &s->txn;
7183 struct hdr_exp *exp;
7184
7185 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007188 /*
7189 * The interleaving of transformations and verdicts
7190 * makes it difficult to decide to continue or stop
7191 * the evaluation.
7192 */
7193
Willy Tarreau6c123b12010-01-28 20:22:06 +01007194 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7195 break;
7196
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007198 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007199 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007200 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007201
7202 /* if this filter had a condition, evaluate it now and skip to
7203 * next filter if the condition does not match.
7204 */
7205 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007206 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007207 ret = acl_pass(ret);
7208 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7209 ret = !ret;
7210
7211 if (!ret)
7212 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 }
7214
7215 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007216 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007217 if (unlikely(ret < 0))
7218 return -1;
7219
7220 if (likely(ret == 0)) {
7221 /* The filter did not match the request, it can be
7222 * iterated through all headers.
7223 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007224 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7225 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007227 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007228 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007229}
7230
7231
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232
Willy Tarreau58f10d72006-12-04 02:26:12 +01007233/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007234 * Try to retrieve the server associated to the appsession.
7235 * If the server is found, it's assigned to the session.
7236 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007237void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7238 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007239 appsess *asession = NULL;
7240 char *sessid_temp = NULL;
7241
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007242 if (len > s->be->appsession_len) {
7243 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007244 }
7245
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007246 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007247 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007248 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007249 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007250 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007251 }
7252
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007253 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007254 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007255 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007256 return;
7257 }
7258
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007259 memcpy(txn->sessid, buf, len);
7260 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007261 }
7262
7263 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7264 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007265 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007266 return;
7267 }
7268
Cyril Bontéb21570a2009-11-29 20:04:48 +01007269 memcpy(sessid_temp, buf, len);
7270 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007271
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007272 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007273 /* free previously allocated memory */
7274 pool_free2(apools.sessid, sessid_temp);
7275
7276 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007277 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7278 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007279 asession->request_count++;
7280
7281 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007282 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007283
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007284 while (srv) {
7285 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007286 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007287 (s->be->options & PR_O_PERSIST) ||
7288 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007289 /* we found the server and it's usable */
7290 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007291 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007292 s->flags |= SN_DIRECT | SN_ASSIGNED;
7293 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007294
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007295 break;
7296 } else {
7297 txn->flags &= ~TX_CK_MASK;
7298 txn->flags |= TX_CK_DOWN;
7299 }
7300 }
7301 srv = srv->next;
7302 }
7303 }
7304 }
7305}
7306
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307/* Find the end of a cookie value contained between <s> and <e>. It works the
7308 * same way as with headers above except that the semi-colon also ends a token.
7309 * See RFC2965 for more information. Note that it requires a valid header to
7310 * return a valid result.
7311 */
7312char *find_cookie_value_end(char *s, const char *e)
7313{
7314 int quoted, qdpair;
7315
7316 quoted = qdpair = 0;
7317 for (; s < e; s++) {
7318 if (qdpair) qdpair = 0;
7319 else if (quoted) {
7320 if (*s == '\\') qdpair = 1;
7321 else if (*s == '"') quoted = 0;
7322 }
7323 else if (*s == '"') quoted = 1;
7324 else if (*s == ',' || *s == ';') return s;
7325 }
7326 return s;
7327}
7328
7329/* Delete a value in a header between delimiters <from> and <next> in buffer
7330 * <buf>. The number of characters displaced is returned, and the pointer to
7331 * the first delimiter is updated if required. The function tries as much as
7332 * possible to respect the following principles :
7333 * - replace <from> delimiter by the <next> one unless <from> points to a
7334 * colon, in which case <next> is simply removed
7335 * - set exactly one space character after the new first delimiter, unless
7336 * there are not enough characters in the block being moved to do so.
7337 * - remove unneeded spaces before the previous delimiter and after the new
7338 * one.
7339 *
7340 * It is the caller's responsibility to ensure that :
7341 * - <from> points to a valid delimiter or the colon ;
7342 * - <next> points to a valid delimiter or the final CR/LF ;
7343 * - there are non-space chars before <from> ;
7344 * - there is a CR/LF at or after <next>.
7345 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007346int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007347{
7348 char *prev = *from;
7349
7350 if (*prev == ':') {
7351 /* We're removing the first value, preserve the colon and add a
7352 * space if possible.
7353 */
7354 if (!http_is_crlf[(unsigned char)*next])
7355 next++;
7356 prev++;
7357 if (prev < next)
7358 *prev++ = ' ';
7359
7360 while (http_is_spht[(unsigned char)*next])
7361 next++;
7362 } else {
7363 /* Remove useless spaces before the old delimiter. */
7364 while (http_is_spht[(unsigned char)*(prev-1)])
7365 prev--;
7366 *from = prev;
7367
7368 /* copy the delimiter and if possible a space if we're
7369 * not at the end of the line.
7370 */
7371 if (!http_is_crlf[(unsigned char)*next]) {
7372 *prev++ = *next++;
7373 if (prev + 1 < next)
7374 *prev++ = ' ';
7375 while (http_is_spht[(unsigned char)*next])
7376 next++;
7377 }
7378 }
7379 return buffer_replace2(buf, prev, next, NULL, 0);
7380}
7381
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007382/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007383 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007384 * desirable to call it only when needed. This code is quite complex because
7385 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7386 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007388void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007390 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007392 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7394 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007395
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007396 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007397 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007398 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007399
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007400 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007401 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007402 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007403
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007404 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405 hdr_beg = hdr_next;
7406 hdr_end = hdr_beg + cur_hdr->len;
7407 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007409 /* We have one full header between hdr_beg and hdr_end, and the
7410 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007411 * "Cookie:" headers.
7412 */
7413
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007414 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007415 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416 old_idx = cur_idx;
7417 continue;
7418 }
7419
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007420 del_from = NULL; /* nothing to be deleted */
7421 preserve_hdr = 0; /* assume we may kill the whole header */
7422
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423 /* Now look for cookies. Conforming to RFC2109, we have to support
7424 * attributes whose name begin with a '$', and associate them with
7425 * the right cookie, if we want to delete this cookie.
7426 * So there are 3 cases for each cookie read :
7427 * 1) it's a special attribute, beginning with a '$' : ignore it.
7428 * 2) it's a server id cookie that we *MAY* want to delete : save
7429 * some pointers on it (last semi-colon, beginning of cookie...)
7430 * 3) it's an application cookie : we *MAY* have to delete a previous
7431 * "special" cookie.
7432 * At the end of loop, if a "special" cookie remains, we may have to
7433 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007434 * *MUST* delete it.
7435 *
7436 * Note: RFC2965 is unclear about the processing of spaces around
7437 * the equal sign in the ATTR=VALUE form. A careful inspection of
7438 * the RFC explicitly allows spaces before it, and not within the
7439 * tokens (attrs or values). An inspection of RFC2109 allows that
7440 * too but section 10.1.3 lets one think that spaces may be allowed
7441 * after the equal sign too, resulting in some (rare) buggy
7442 * implementations trying to do that. So let's do what servers do.
7443 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7444 * allowed quoted strings in values, with any possible character
7445 * after a backslash, including control chars and delimitors, which
7446 * causes parsing to become ambiguous. Browsers also allow spaces
7447 * within values even without quotes.
7448 *
7449 * We have to keep multiple pointers in order to support cookie
7450 * removal at the beginning, middle or end of header without
7451 * corrupting the header. All of these headers are valid :
7452 *
7453 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7454 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7455 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7456 * | | | | | | | | |
7457 * | | | | | | | | hdr_end <--+
7458 * | | | | | | | +--> next
7459 * | | | | | | +----> val_end
7460 * | | | | | +-----------> val_beg
7461 * | | | | +--------------> equal
7462 * | | | +----------------> att_end
7463 * | | +---------------------> att_beg
7464 * | +--------------------------> prev
7465 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466 */
7467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7469 /* Iterate through all cookies on this line */
7470
7471 /* find att_beg */
7472 att_beg = prev + 1;
7473 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7474 att_beg++;
7475
7476 /* find att_end : this is the first character after the last non
7477 * space before the equal. It may be equal to hdr_end.
7478 */
7479 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 while (equal < hdr_end) {
7482 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007483 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007484 if (http_is_spht[(unsigned char)*equal++])
7485 continue;
7486 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487 }
7488
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7490 * is between <att_beg> and <equal>, both may be identical.
7491 */
7492
7493 /* look for end of cookie if there is an equal sign */
7494 if (equal < hdr_end && *equal == '=') {
7495 /* look for the beginning of the value */
7496 val_beg = equal + 1;
7497 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7498 val_beg++;
7499
7500 /* find the end of the value, respecting quotes */
7501 next = find_cookie_value_end(val_beg, hdr_end);
7502
7503 /* make val_end point to the first white space or delimitor after the value */
7504 val_end = next;
7505 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7506 val_end--;
7507 } else {
7508 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007511 /* We have nothing to do with attributes beginning with '$'. However,
7512 * they will automatically be removed if a header before them is removed,
7513 * since they're supposed to be linked together.
7514 */
7515 if (*att_beg == '$')
7516 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 /* Ignore cookies with no equal sign */
7519 if (equal == next) {
7520 /* This is not our cookie, so we must preserve it. But if we already
7521 * scheduled another cookie for removal, we cannot remove the
7522 * complete header, but we can remove the previous block itself.
7523 */
7524 preserve_hdr = 1;
7525 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007526 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527 val_end += delta;
7528 next += delta;
7529 hdr_end += delta;
7530 hdr_next += delta;
7531 cur_hdr->len += delta;
7532 http_msg_move_end(&txn->req, delta);
7533 prev = del_from;
7534 del_from = NULL;
7535 }
7536 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007537 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007538
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 /* if there are spaces around the equal sign, we need to
7540 * strip them otherwise we'll get trouble for cookie captures,
7541 * or even for rewrites. Since this happens extremely rarely,
7542 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007543 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7545 int stripped_before = 0;
7546 int stripped_after = 0;
7547
7548 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007549 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 equal += stripped_before;
7551 val_beg += stripped_before;
7552 }
7553
7554 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007555 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007556 val_beg += stripped_after;
7557 stripped_before += stripped_after;
7558 }
7559
7560 val_end += stripped_before;
7561 next += stripped_before;
7562 hdr_end += stripped_before;
7563 hdr_next += stripped_before;
7564 cur_hdr->len += stripped_before;
7565 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007568
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 /* First, let's see if we want to capture this cookie. We check
7570 * that we don't already have a client side cookie, because we
7571 * can only capture one. Also as an optimisation, we ignore
7572 * cookies shorter than the declared name.
7573 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007574 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7575 (val_end - att_beg >= s->fe->capture_namelen) &&
7576 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 int log_len = val_end - att_beg;
7578
7579 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7580 Alert("HTTP logging : out of memory.\n");
7581 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007582 if (log_len > s->fe->capture_len)
7583 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 memcpy(txn->cli_cookie, att_beg, log_len);
7585 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588
Willy Tarreaubca99692010-10-06 19:25:55 +02007589 /* Persistence cookies in passive, rewrite or insert mode have the
7590 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007591 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007592 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007594 * For cookies in prefix mode, the form is :
7595 *
7596 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007598 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7599 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7600 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7604 * have the server ID between val_beg and delim, and the original cookie between
7605 * delim+1 and val_end. Otherwise, delim==val_end :
7606 *
7607 * Cookie: NAME=SRV; # in all but prefix modes
7608 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7609 * | || || | |+-> next
7610 * | || || | +--> val_end
7611 * | || || +---------> delim
7612 * | || |+------------> val_beg
7613 * | || +-------------> att_end = equal
7614 * | |+-----------------> att_beg
7615 * | +------------------> prev
7616 * +-------------------------> hdr_beg
7617 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007618
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007619 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 for (delim = val_beg; delim < val_end; delim++)
7621 if (*delim == COOKIE_DELIM)
7622 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007623 } else {
7624 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007625 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007626 /* Now check if the cookie contains a date field, which would
7627 * appear after a vertical bar ('|') just after the server name
7628 * and before the delimiter.
7629 */
7630 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7631 if (vbar1) {
7632 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007633 * right is the last seen date. It is a base64 encoded
7634 * 30-bit value representing the UNIX date since the
7635 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007636 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007637 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007638 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007639 if (val_end - vbar1 >= 5) {
7640 val = b64tos30(vbar1);
7641 if (val > 0)
7642 txn->cookie_last_date = val << 2;
7643 }
7644 /* look for a second vertical bar */
7645 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7646 if (vbar1 && (val_end - vbar1 > 5)) {
7647 val = b64tos30(vbar1 + 1);
7648 if (val > 0)
7649 txn->cookie_first_date = val << 2;
7650 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007651 }
7652 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007653
Willy Tarreauf64d1412010-10-07 20:06:11 +02007654 /* if the cookie has an expiration date and the proxy wants to check
7655 * it, then we do that now. We first check if the cookie is too old,
7656 * then only if it has expired. We detect strict overflow because the
7657 * time resolution here is not great (4 seconds). Cookies with dates
7658 * in the future are ignored if their offset is beyond one day. This
7659 * allows an admin to fix timezone issues without expiring everyone
7660 * and at the same time avoids keeping unwanted side effects for too
7661 * long.
7662 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007663 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7664 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007665 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007666 txn->flags &= ~TX_CK_MASK;
7667 txn->flags |= TX_CK_OLD;
7668 delim = val_beg; // let's pretend we have not found the cookie
7669 txn->cookie_first_date = 0;
7670 txn->cookie_last_date = 0;
7671 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007672 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7673 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007674 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007675 txn->flags &= ~TX_CK_MASK;
7676 txn->flags |= TX_CK_EXPIRED;
7677 delim = val_beg; // let's pretend we have not found the cookie
7678 txn->cookie_first_date = 0;
7679 txn->cookie_last_date = 0;
7680 }
7681
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 /* Here, we'll look for the first running server which supports the cookie.
7683 * This allows to share a same cookie between several servers, for example
7684 * to dedicate backup servers to specific servers only.
7685 * However, to prevent clients from sticking to cookie-less backup server
7686 * when they have incidentely learned an empty cookie, we simply ignore
7687 * empty cookies and mark them as invalid.
7688 * The same behaviour is applied when persistence must be ignored.
7689 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007690 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 while (srv) {
7694 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7695 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007696 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007697 (s->be->options & PR_O_PERSIST) ||
7698 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 /* we found the server and we can use it */
7700 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007701 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007702 s->flags |= SN_DIRECT | SN_ASSIGNED;
7703 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 break;
7705 } else {
7706 /* we found a server, but it's down,
7707 * mark it as such and go on in case
7708 * another one is available.
7709 */
7710 txn->flags &= ~TX_CK_MASK;
7711 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 srv = srv->next;
7715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716
Willy Tarreauf64d1412010-10-07 20:06:11 +02007717 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007718 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007720 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007721 txn->flags |= TX_CK_UNUSED;
7722 else
7723 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 /* depending on the cookie mode, we may have to either :
7727 * - delete the complete cookie if we're in insert+indirect mode, so that
7728 * the server never sees it ;
7729 * - remove the server id from the cookie value, and tag the cookie as an
7730 * application cookie so that it does not get accidentely removed later,
7731 * if we're in cookie prefix mode
7732 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007733 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 val_end += delta;
7738 next += delta;
7739 hdr_end += delta;
7740 hdr_next += delta;
7741 cur_hdr->len += delta;
7742 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007743
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 del_from = NULL;
7745 preserve_hdr = 1; /* we want to keep this cookie */
7746 }
7747 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007748 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 del_from = prev;
7750 }
7751 } else {
7752 /* This is not our cookie, so we must preserve it. But if we already
7753 * scheduled another cookie for removal, we cannot remove the
7754 * complete header, but we can remove the previous block itself.
7755 */
7756 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007759 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007760 if (att_beg >= del_from)
7761 att_beg += delta;
7762 if (att_end >= del_from)
7763 att_end += delta;
7764 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 val_end += delta;
7766 next += delta;
7767 hdr_end += delta;
7768 hdr_next += delta;
7769 cur_hdr->len += delta;
7770 http_msg_move_end(&txn->req, delta);
7771 prev = del_from;
7772 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007775
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007777 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 int cmp_len, value_len;
7779 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007780
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007781 if (s->be->options2 & PR_O2_AS_PFX) {
7782 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7783 value_begin = att_beg + s->be->appsession_name_len;
7784 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007785 } else {
7786 cmp_len = att_end - att_beg;
7787 value_begin = val_beg;
7788 value_len = val_end - val_beg;
7789 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007791 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007792 if (cmp_len == s->be->appsession_name_len &&
7793 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7794 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007796 }
7797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007798 /* continue with next cookie on this header line */
7799 att_beg = next;
7800 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007801
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007802 /* There are no more cookies on this line.
7803 * We may still have one (or several) marked for deletion at the
7804 * end of the line. We must do this now in two ways :
7805 * - if some cookies must be preserved, we only delete from the
7806 * mark to the end of line ;
7807 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007809 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007810 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007811 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007812 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007814 cur_hdr->len += delta;
7815 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007816 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007817
7818 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007819 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7820 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007821 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007822 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007823 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007824 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007825 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007826 }
7827
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007828 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007829 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007830 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007831}
7832
7833
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834/* Iterate the same filter through all response headers contained in <rtr>.
7835 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7836 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007837int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 char *cur_ptr, *cur_end, *cur_next;
7840 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007841 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007843 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844
7845 last_hdr = 0;
7846
Willy Tarreau9b28e032012-10-12 23:49:43 +02007847 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 old_idx = 0;
7849
7850 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007851 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007853 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854 (exp->action == ACT_ALLOW ||
7855 exp->action == ACT_DENY))
7856 return 0;
7857
7858 cur_idx = txn->hdr_idx.v[old_idx].next;
7859 if (!cur_idx)
7860 break;
7861
7862 cur_hdr = &txn->hdr_idx.v[cur_idx];
7863 cur_ptr = cur_next;
7864 cur_end = cur_ptr + cur_hdr->len;
7865 cur_next = cur_end + cur_hdr->cr + 1;
7866
7867 /* Now we have one header between cur_ptr and cur_end,
7868 * and the next header starts at cur_next.
7869 */
7870
Willy Tarreau15a53a42015-01-21 13:39:42 +01007871 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 switch (exp->action) {
7873 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007874 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007875 last_hdr = 1;
7876 break;
7877
7878 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007879 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007880 last_hdr = 1;
7881 break;
7882
7883 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007884 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7885 if (trash.len < 0)
7886 return -1;
7887
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007888 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889 /* FIXME: if the user adds a newline in the replacement, the
7890 * index will not be recalculated for now, and the new line
7891 * will not be counted as a new header.
7892 */
7893
7894 cur_end += delta;
7895 cur_next += delta;
7896 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007897 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898 break;
7899
7900 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007901 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902 cur_next += delta;
7903
Willy Tarreaufa355d42009-11-29 18:12:29 +01007904 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7906 txn->hdr_idx.used--;
7907 cur_hdr->len = 0;
7908 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007909 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 break;
7911
7912 }
7913 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914
7915 /* keep the link from this header to next one in case of later
7916 * removal of next header.
7917 */
7918 old_idx = cur_idx;
7919 }
7920 return 0;
7921}
7922
7923
7924/* Apply the filter to the status line in the response buffer <rtr>.
7925 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7926 * or -1 if a replacement resulted in an invalid status line.
7927 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007928int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 char *cur_ptr, *cur_end;
7931 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007932 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007933 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934
7935
Willy Tarreau3d300592007-03-18 18:34:41 +01007936 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007937 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007938 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939 (exp->action == ACT_ALLOW ||
7940 exp->action == ACT_DENY))
7941 return 0;
7942 else if (exp->action == ACT_REMOVE)
7943 return 0;
7944
7945 done = 0;
7946
Willy Tarreau9b28e032012-10-12 23:49:43 +02007947 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007948 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949
7950 /* Now we have the status line between cur_ptr and cur_end */
7951
Willy Tarreau15a53a42015-01-21 13:39:42 +01007952 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 switch (exp->action) {
7954 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007955 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 done = 1;
7957 break;
7958
7959 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007960 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 done = 1;
7962 break;
7963
7964 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007965 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7966 if (trash.len < 0)
7967 return -1;
7968
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007969 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970 /* FIXME: if the user adds a newline in the replacement, the
7971 * index will not be recalculated for now, and the new line
7972 * will not be counted as a new header.
7973 */
7974
Willy Tarreaufa355d42009-11-29 18:12:29 +01007975 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007977 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007978 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 cur_ptr, cur_end + 1,
7980 NULL, NULL);
7981 if (unlikely(!cur_end))
7982 return -1;
7983
7984 /* we have a full respnse and we know that we have either a CR
7985 * or an LF at <ptr>.
7986 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007988 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 /* there is no point trying this regex on headers */
7990 return 1;
7991 }
7992 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 return done;
7994}
7995
7996
7997
7998/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007999 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8001 * unparsable response.
8002 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02008003int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008005 struct http_txn *txn = &s->txn;
8006 struct hdr_exp *exp;
8007
8008 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 int ret;
8010
8011 /*
8012 * The interleaving of transformations and verdicts
8013 * makes it difficult to decide to continue or stop
8014 * the evaluation.
8015 */
8016
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008017 if (txn->flags & TX_SVDENY)
8018 break;
8019
Willy Tarreau3d300592007-03-18 18:34:41 +01008020 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8022 exp->action == ACT_PASS)) {
8023 exp = exp->next;
8024 continue;
8025 }
8026
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008027 /* if this filter had a condition, evaluate it now and skip to
8028 * next filter if the condition does not match.
8029 */
8030 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008031 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008032 ret = acl_pass(ret);
8033 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8034 ret = !ret;
8035 if (!ret)
8036 continue;
8037 }
8038
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008040 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 if (unlikely(ret < 0))
8042 return -1;
8043
8044 if (likely(ret == 0)) {
8045 /* The filter did not match the response, it can be
8046 * iterated through all headers.
8047 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008048 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8049 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 }
8052 return 0;
8053}
8054
8055
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008057 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008058 * desirable to call it only when needed. This function is also used when we
8059 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008061void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008063 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01008064 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 char *hdr_beg, *hdr_end, *hdr_next;
8068 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 /* Iterate through the headers.
8071 * we start with the start line.
8072 */
8073 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008074 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075
8076 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8077 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008078 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079
8080 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008081 hdr_beg = hdr_next;
8082 hdr_end = hdr_beg + cur_hdr->len;
8083 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008084
Willy Tarreau24581ba2010-08-31 22:39:35 +02008085 /* We have one full header between hdr_beg and hdr_end, and the
8086 * next header starts at hdr_next. We're only interested in
8087 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 */
8089
Willy Tarreau24581ba2010-08-31 22:39:35 +02008090 is_cookie2 = 0;
8091 prev = hdr_beg + 10;
8092 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008093 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008094 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8095 if (!val) {
8096 old_idx = cur_idx;
8097 continue;
8098 }
8099 is_cookie2 = 1;
8100 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 }
8102
Willy Tarreau24581ba2010-08-31 22:39:35 +02008103 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8104 * <prev> points to the colon.
8105 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008106 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107
Willy Tarreau24581ba2010-08-31 22:39:35 +02008108 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8109 * check-cache is enabled) and we are not interested in checking
8110 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008111 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008112 if (s->be->cookie_name == NULL &&
8113 s->be->appsession_name == NULL &&
8114 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008115 return;
8116
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 /* OK so now we know we have to process this response cookie.
8118 * The format of the Set-Cookie header is slightly different
8119 * from the format of the Cookie header in that it does not
8120 * support the comma as a cookie delimiter (thus the header
8121 * cannot be folded) because the Expires attribute described in
8122 * the original Netscape's spec may contain an unquoted date
8123 * with a comma inside. We have to live with this because
8124 * many browsers don't support Max-Age and some browsers don't
8125 * support quoted strings. However the Set-Cookie2 header is
8126 * clean.
8127 *
8128 * We have to keep multiple pointers in order to support cookie
8129 * removal at the beginning, middle or end of header without
8130 * corrupting the header (in case of set-cookie2). A special
8131 * pointer, <scav> points to the beginning of the set-cookie-av
8132 * fields after the first semi-colon. The <next> pointer points
8133 * either to the end of line (set-cookie) or next unquoted comma
8134 * (set-cookie2). All of these headers are valid :
8135 *
8136 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8137 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8138 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8139 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8140 * | | | | | | | | | |
8141 * | | | | | | | | +-> next hdr_end <--+
8142 * | | | | | | | +------------> scav
8143 * | | | | | | +--------------> val_end
8144 * | | | | | +--------------------> val_beg
8145 * | | | | +----------------------> equal
8146 * | | | +------------------------> att_end
8147 * | | +----------------------------> att_beg
8148 * | +------------------------------> prev
8149 * +-----------------------------------------> hdr_beg
8150 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 for (; prev < hdr_end; prev = next) {
8153 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154
Willy Tarreau24581ba2010-08-31 22:39:35 +02008155 /* find att_beg */
8156 att_beg = prev + 1;
8157 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8158 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159
Willy Tarreau24581ba2010-08-31 22:39:35 +02008160 /* find att_end : this is the first character after the last non
8161 * space before the equal. It may be equal to hdr_end.
8162 */
8163 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164
Willy Tarreau24581ba2010-08-31 22:39:35 +02008165 while (equal < hdr_end) {
8166 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8167 break;
8168 if (http_is_spht[(unsigned char)*equal++])
8169 continue;
8170 att_end = equal;
8171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172
Willy Tarreau24581ba2010-08-31 22:39:35 +02008173 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8174 * is between <att_beg> and <equal>, both may be identical.
8175 */
8176
8177 /* look for end of cookie if there is an equal sign */
8178 if (equal < hdr_end && *equal == '=') {
8179 /* look for the beginning of the value */
8180 val_beg = equal + 1;
8181 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8182 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183
Willy Tarreau24581ba2010-08-31 22:39:35 +02008184 /* find the end of the value, respecting quotes */
8185 next = find_cookie_value_end(val_beg, hdr_end);
8186
8187 /* make val_end point to the first white space or delimitor after the value */
8188 val_end = next;
8189 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8190 val_end--;
8191 } else {
8192 /* <equal> points to next comma, semi-colon or EOL */
8193 val_beg = val_end = next = equal;
8194 }
8195
8196 if (next < hdr_end) {
8197 /* Set-Cookie2 supports multiple cookies, and <next> points to
8198 * a colon or semi-colon before the end. So skip all attr-value
8199 * pairs and look for the next comma. For Set-Cookie, since
8200 * commas are permitted in values, skip to the end.
8201 */
8202 if (is_cookie2)
8203 next = find_hdr_value_end(next, hdr_end);
8204 else
8205 next = hdr_end;
8206 }
8207
8208 /* Now everything is as on the diagram above */
8209
8210 /* Ignore cookies with no equal sign */
8211 if (equal == val_end)
8212 continue;
8213
8214 /* If there are spaces around the equal sign, we need to
8215 * strip them otherwise we'll get trouble for cookie captures,
8216 * or even for rewrites. Since this happens extremely rarely,
8217 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8220 int stripped_before = 0;
8221 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008224 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008225 equal += stripped_before;
8226 val_beg += stripped_before;
8227 }
8228
8229 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008230 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 val_beg += stripped_after;
8232 stripped_before += stripped_after;
8233 }
8234
8235 val_end += stripped_before;
8236 next += stripped_before;
8237 hdr_end += stripped_before;
8238 hdr_next += stripped_before;
8239 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008240 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 }
8242
8243 /* First, let's see if we want to capture this cookie. We check
8244 * that we don't already have a server side cookie, because we
8245 * can only capture one. Also as an optimisation, we ignore
8246 * cookies shorter than the declared name.
8247 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008248 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008249 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008250 (val_end - att_beg >= s->fe->capture_namelen) &&
8251 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008252 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008253 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254 Alert("HTTP logging : out of memory.\n");
8255 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008256 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 if (log_len > s->fe->capture_len)
8258 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008259 memcpy(txn->srv_cookie, att_beg, log_len);
8260 txn->srv_cookie[log_len] = 0;
8261 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262 }
8263
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008264 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008266 if (!(s->flags & SN_IGNORE_PRST) &&
8267 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8268 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008269 /* assume passive cookie by default */
8270 txn->flags &= ~TX_SCK_MASK;
8271 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272
8273 /* If the cookie is in insert mode on a known server, we'll delete
8274 * this occurrence because we'll insert another one later.
8275 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 * a direct access.
8277 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008278 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008279 /* The "preserve" flag was set, we don't want to touch the
8280 * server's cookie.
8281 */
8282 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008283 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8284 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008285 /* this cookie must be deleted */
8286 if (*prev == ':' && next == hdr_end) {
8287 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008288 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008289 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8290 txn->hdr_idx.used--;
8291 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008292 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 hdr_next += delta;
8294 http_msg_move_end(&txn->rsp, delta);
8295 /* note: while both invalid now, <next> and <hdr_end>
8296 * are still equal, so the for() will stop as expected.
8297 */
8298 } else {
8299 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008300 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 next = prev;
8302 hdr_end += delta;
8303 hdr_next += delta;
8304 cur_hdr->len += delta;
8305 http_msg_move_end(&txn->rsp, delta);
8306 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008307 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008308 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008311 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313 * with this server since we know it.
8314 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008315 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008316 next += delta;
8317 hdr_end += delta;
8318 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008320 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008321
Willy Tarreauf1348312010-10-07 15:54:11 +02008322 txn->flags &= ~TX_SCK_MASK;
8323 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008326 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008329 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 next += delta;
8331 hdr_end += delta;
8332 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008334 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau827aee92011-03-10 16:55:02 +01008336 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008337 txn->flags &= ~TX_SCK_MASK;
8338 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008339 }
8340 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008341 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008342 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008343 int cmp_len, value_len;
8344 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008345
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008346 if (s->be->options2 & PR_O2_AS_PFX) {
8347 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8348 value_begin = att_beg + s->be->appsession_name_len;
8349 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008350 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008351 cmp_len = att_end - att_beg;
8352 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008353 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008354 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008355
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008356 if ((cmp_len == s->be->appsession_name_len) &&
8357 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008358 /* free a possibly previously allocated memory */
8359 pool_free2(apools.sessid, txn->sessid);
8360
Cyril Bontéb21570a2009-11-29 20:04:48 +01008361 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008362 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008363 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008364 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008365 return;
8366 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008367 memcpy(txn->sessid, value_begin, value_len);
8368 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008369 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008370 }
8371 /* that's done for this cookie, check the next one on the same
8372 * line when next != hdr_end (only if is_cookie2).
8373 */
8374 }
8375 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008376 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008378
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008379 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008380 appsess *asession = NULL;
8381 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008382 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008383 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008384 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008385 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8386 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008387 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008388 return;
8389 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008390 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8391
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008392 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8393 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8395 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008396 return;
8397 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008398 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8399 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008400
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008401 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008402 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008403 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008404 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8405 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008406 return;
8407 }
8408 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008409 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008410
8411 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008412 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008413 }
8414
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008415 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008416 asession->request_count++;
8417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418}
8419
8420
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008422 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008426 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008427 char *p1, *p2;
8428
8429 char *cur_ptr, *cur_end, *cur_next;
8430 int cur_idx;
8431
Willy Tarreau5df51872007-11-25 16:20:08 +01008432 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 return;
8434
8435 /* Iterate through the headers.
8436 * we start with the start line.
8437 */
8438 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008439 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440
8441 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8442 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008443 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008444
8445 cur_hdr = &txn->hdr_idx.v[cur_idx];
8446 cur_ptr = cur_next;
8447 cur_end = cur_ptr + cur_hdr->len;
8448 cur_next = cur_end + cur_hdr->cr + 1;
8449
8450 /* We have one full header between cur_ptr and cur_end, and the
8451 * next header starts at cur_next. We're only interested in
8452 * "Cookie:" headers.
8453 */
8454
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008455 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8456 if (val) {
8457 if ((cur_end - (cur_ptr + val) >= 8) &&
8458 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8459 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8460 return;
8461 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462 }
8463
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008464 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8465 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 continue;
8467
8468 /* OK, right now we know we have a cache-control header at cur_ptr */
8469
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008470 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008471
8472 if (p1 >= cur_end) /* no more info */
8473 continue;
8474
8475 /* p1 is at the beginning of the value */
8476 p2 = p1;
8477
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008478 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 p2++;
8480
8481 /* we have a complete value between p1 and p2 */
8482 if (p2 < cur_end && *p2 == '=') {
8483 /* we have something of the form no-cache="set-cookie" */
8484 if ((cur_end - p1 >= 21) &&
8485 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8486 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008487 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488 continue;
8489 }
8490
8491 /* OK, so we know that either p2 points to the end of string or to a comma */
8492 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008493 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8495 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8496 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008497 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 return;
8499 }
8500
8501 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008502 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 continue;
8504 }
8505 }
8506}
8507
8508
Willy Tarreau58f10d72006-12-04 02:26:12 +01008509/*
8510 * Try to retrieve a known appsession in the URI, then the associated server.
8511 * If the server is found, it's assigned to the session.
8512 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008514{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008515 char *end_params, *first_param, *cur_param, *next_param;
8516 char separator;
8517 int value_len;
8518
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008519 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008520
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008521 if (s->be->appsession_name == NULL ||
8522 (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 +01008523 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008525
Cyril Bontéb21570a2009-11-29 20:04:48 +01008526 first_param = NULL;
8527 switch (mode) {
8528 case PR_O2_AS_M_PP:
8529 first_param = memchr(begin, ';', len);
8530 break;
8531 case PR_O2_AS_M_QS:
8532 first_param = memchr(begin, '?', len);
8533 break;
8534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008535
Cyril Bontéb21570a2009-11-29 20:04:48 +01008536 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008537 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008538 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008539
Cyril Bontéb21570a2009-11-29 20:04:48 +01008540 switch (mode) {
8541 case PR_O2_AS_M_PP:
8542 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8543 end_params = (char *) begin + len;
8544 }
8545 separator = ';';
8546 break;
8547 case PR_O2_AS_M_QS:
8548 end_params = (char *) begin + len;
8549 separator = '&';
8550 break;
8551 default:
8552 /* unknown mode, shouldn't happen */
8553 return;
8554 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008555
Cyril Bontéb21570a2009-11-29 20:04:48 +01008556 cur_param = next_param = end_params;
8557 while (cur_param > first_param) {
8558 cur_param--;
8559 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8560 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008561 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8562 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8563 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008564 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008565 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8566 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008567 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008568 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008569 }
8570 break;
8571 }
8572 next_param = cur_param;
8573 }
8574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008575#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008576 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008577 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008579}
8580
Willy Tarreaub2513902006-12-17 14:52:38 +01008581/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008582 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008583 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008584 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008585 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008586 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008587 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008588 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008589 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008590int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008591{
8592 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008593 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008594 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008595
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008596 if (!uri_auth)
8597 return 0;
8598
Cyril Bonté70be45d2010-10-12 00:14:35 +02008599 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008600 return 0;
8601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008602 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008603 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008604 return 0;
8605
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008606 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 return 0;
8608
Willy Tarreaub2513902006-12-17 14:52:38 +01008609 return 1;
8610}
8611
Willy Tarreau4076a152009-04-02 15:18:36 +02008612/*
8613 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008614 * By default it tries to report the error position as msg->err_pos. However if
8615 * this one is not set, it will then report msg->next, which is the last known
8616 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008617 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008618 */
8619void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008620 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008621 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008622{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008623 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008624 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008625
Willy Tarreau9b28e032012-10-12 23:49:43 +02008626 es->len = MIN(chn->buf->i, sizeof(es->buf));
8627 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008628 len1 = MIN(len1, es->len);
8629 len2 = es->len - len1; /* remaining data if buffer wraps */
8630
Willy Tarreau9b28e032012-10-12 23:49:43 +02008631 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008632 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008633 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008634
Willy Tarreau4076a152009-04-02 15:18:36 +02008635 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008636 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008637 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008638 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008639
Willy Tarreau4076a152009-04-02 15:18:36 +02008640 es->when = date; // user-visible date
8641 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008642 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008643 es->oe = other_end;
Willy Tarreau350f4872014-11-28 14:42:25 +01008644 if (objt_conn(s->si[0].end))
8645 es->src = __objt_conn(s->si[0].end)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008646 else
8647 memset(&es->src, 0, sizeof(es->src));
8648
Willy Tarreau078272e2010-12-12 12:46:33 +01008649 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008650 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008651 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008652 es->s_flags = s->flags;
8653 es->t_flags = s->txn.flags;
8654 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008655 es->b_out = chn->buf->o;
8656 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008657 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008658 es->m_clen = msg->chunk_len;
8659 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008660}
Willy Tarreaub2513902006-12-17 14:52:38 +01008661
Willy Tarreau294c4732011-12-16 21:35:50 +01008662/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8663 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8664 * performed over the whole headers. Otherwise it must contain a valid header
8665 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8666 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8667 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8668 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008669 * -1. The value fetch stops at commas, so this function is suited for use with
8670 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008671 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008672 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008673unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008674 struct hdr_idx *idx, int occ,
8675 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008676{
Willy Tarreau294c4732011-12-16 21:35:50 +01008677 struct hdr_ctx local_ctx;
8678 char *ptr_hist[MAX_HDR_HISTORY];
8679 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008680 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008681 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008682
Willy Tarreau294c4732011-12-16 21:35:50 +01008683 if (!ctx) {
8684 local_ctx.idx = 0;
8685 ctx = &local_ctx;
8686 }
8687
Willy Tarreaubce70882009-09-07 11:51:47 +02008688 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008689 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008690 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008691 occ--;
8692 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 *vptr = ctx->line + ctx->val;
8694 *vlen = ctx->vlen;
8695 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 }
8697 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 }
8700
8701 /* negative occurrence, we scan all the list then walk back */
8702 if (-occ > MAX_HDR_HISTORY)
8703 return 0;
8704
Willy Tarreau294c4732011-12-16 21:35:50 +01008705 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008706 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8708 len_hist[hist_ptr] = ctx->vlen;
8709 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008710 hist_ptr = 0;
8711 found++;
8712 }
8713 if (-occ > found)
8714 return 0;
8715 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008716 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8717 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8718 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008719 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008720 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008721 if (hist_ptr >= MAX_HDR_HISTORY)
8722 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008723 *vptr = ptr_hist[hist_ptr];
8724 *vlen = len_hist[hist_ptr];
8725 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008726}
8727
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008728/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8729 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8730 * performed over the whole headers. Otherwise it must contain a valid header
8731 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8732 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8733 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8734 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8735 * -1. This function differs from http_get_hdr() in that it only returns full
8736 * line header values and does not stop at commas.
8737 * The return value is 0 if nothing was found, or non-zero otherwise.
8738 */
8739unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8740 struct hdr_idx *idx, int occ,
8741 struct hdr_ctx *ctx, char **vptr, int *vlen)
8742{
8743 struct hdr_ctx local_ctx;
8744 char *ptr_hist[MAX_HDR_HISTORY];
8745 int len_hist[MAX_HDR_HISTORY];
8746 unsigned int hist_ptr;
8747 int found;
8748
8749 if (!ctx) {
8750 local_ctx.idx = 0;
8751 ctx = &local_ctx;
8752 }
8753
8754 if (occ >= 0) {
8755 /* search from the beginning */
8756 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8757 occ--;
8758 if (occ <= 0) {
8759 *vptr = ctx->line + ctx->val;
8760 *vlen = ctx->vlen;
8761 return 1;
8762 }
8763 }
8764 return 0;
8765 }
8766
8767 /* negative occurrence, we scan all the list then walk back */
8768 if (-occ > MAX_HDR_HISTORY)
8769 return 0;
8770
8771 found = hist_ptr = 0;
8772 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8773 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8774 len_hist[hist_ptr] = ctx->vlen;
8775 if (++hist_ptr >= MAX_HDR_HISTORY)
8776 hist_ptr = 0;
8777 found++;
8778 }
8779 if (-occ > found)
8780 return 0;
8781 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8782 * find occurrence -occ, so we have to check [hist_ptr+occ].
8783 */
8784 hist_ptr += occ;
8785 if (hist_ptr >= MAX_HDR_HISTORY)
8786 hist_ptr -= MAX_HDR_HISTORY;
8787 *vptr = ptr_hist[hist_ptr];
8788 *vlen = len_hist[hist_ptr];
8789 return 1;
8790}
8791
Willy Tarreaubaaee002006-06-26 02:48:02 +02008792/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008793 * Print a debug line with a header. Always stop at the first CR or LF char,
8794 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8795 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008796 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008797void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008798{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008799 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008800 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008801 dir,
Willy Tarreau350f4872014-11-28 14:42:25 +01008802 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
8803 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008804
8805 for (max = 0; start + max < end; max++)
8806 if (start[max] == '\r' || start[max] == '\n')
8807 break;
8808
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008809 UBOUND(max, trash.size - trash.len - 3);
8810 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8811 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008812 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008813}
8814
Willy Tarreau0937bc42009-12-22 15:03:09 +01008815/*
8816 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8817 * the required fields are properly allocated and that we only need to (re)init
8818 * them. This should be used before processing any new request.
8819 */
8820void http_init_txn(struct session *s)
8821{
8822 struct http_txn *txn = &s->txn;
8823 struct proxy *fe = s->fe;
8824
8825 txn->flags = 0;
8826 txn->status = -1;
8827
Willy Tarreauf64d1412010-10-07 20:06:11 +02008828 txn->cookie_first_date = 0;
8829 txn->cookie_last_date = 0;
8830
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008831 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008832 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008833 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008834 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008835 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008836 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008837 txn->req.chunk_len = 0LL;
8838 txn->req.body_len = 0LL;
8839 txn->rsp.chunk_len = 0LL;
8840 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008841 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8842 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008843 txn->req.chn = &s->req;
8844 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008845
8846 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008847
8848 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8849 if (fe->options2 & PR_O2_REQBUG_OK)
8850 txn->req.err_pos = -1; /* let buggy requests pass */
8851
Willy Tarreau46023632010-01-07 22:51:47 +01008852 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008853 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8854
Willy Tarreau46023632010-01-07 22:51:47 +01008855 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008856 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8857
8858 if (txn->hdr_idx.v)
8859 hdr_idx_init(&txn->hdr_idx);
8860}
8861
8862/* to be used at the end of a transaction */
8863void http_end_txn(struct session *s)
8864{
8865 struct http_txn *txn = &s->txn;
8866
Willy Tarreau75195602014-03-11 15:48:55 +01008867 /* release any possible compression context */
8868 if (s->flags & SN_COMP_READY)
8869 s->comp_algo->end(&s->comp_ctx);
8870 s->comp_algo = NULL;
8871 s->flags &= ~SN_COMP_READY;
8872
Willy Tarreau0937bc42009-12-22 15:03:09 +01008873 /* these ones will have been dynamically allocated */
8874 pool_free2(pool2_requri, txn->uri);
8875 pool_free2(pool2_capture, txn->cli_cookie);
8876 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008877 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008878 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008879
William Lallemanda73203e2012-03-12 12:48:57 +01008880 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008881 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008882 txn->uri = NULL;
8883 txn->srv_cookie = NULL;
8884 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008885
8886 if (txn->req.cap) {
8887 struct cap_hdr *h;
8888 for (h = s->fe->req_cap; h; h = h->next)
8889 pool_free2(h->pool, txn->req.cap[h->index]);
8890 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8891 }
8892
8893 if (txn->rsp.cap) {
8894 struct cap_hdr *h;
8895 for (h = s->fe->rsp_cap; h; h = h->next)
8896 pool_free2(h->pool, txn->rsp.cap[h->index]);
8897 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8898 }
8899
Willy Tarreau0937bc42009-12-22 15:03:09 +01008900}
8901
8902/* to be used at the end of a transaction to prepare a new one */
8903void http_reset_txn(struct session *s)
8904{
8905 http_end_txn(s);
8906 http_init_txn(s);
8907
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008908 /* reinitialise the current rule list pointer to NULL. We are sure that
8909 * any rulelist match the NULL pointer.
8910 */
8911 s->current_rule_list = NULL;
8912
Willy Tarreau0937bc42009-12-22 15:03:09 +01008913 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008914 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008915 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008916 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008917 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008918 /* re-init store persistence */
8919 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008920 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008921
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008923
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008924 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925
Willy Tarreau739cfba2010-01-25 23:11:14 +01008926 /* We must trim any excess data from the response buffer, because we
8927 * may have blocked an invalid response from a server that we don't
8928 * want to accidentely forward once we disable the analysers, nor do
8929 * we want those data to come along with next response. A typical
8930 * example of such data would be from a buggy server responding to
8931 * a HEAD with some data, or sending more than the advertised
8932 * content-length.
8933 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008934 if (unlikely(s->res.buf->i))
8935 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008936
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008937 s->req.rto = s->fe->timeout.client;
8938 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008939
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008940 s->res.rto = TICK_ETERNITY;
8941 s->res.wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008942
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008943 s->req.rex = TICK_ETERNITY;
8944 s->req.wex = TICK_ETERNITY;
8945 s->req.analyse_exp = TICK_ETERNITY;
8946 s->res.rex = TICK_ETERNITY;
8947 s->res.wex = TICK_ETERNITY;
8948 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008949}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008950
Sasha Pachev218f0642014-06-16 12:05:59 -06008951void free_http_res_rules(struct list *r)
8952{
8953 struct http_res_rule *tr, *pr;
8954
8955 list_for_each_entry_safe(pr, tr, r, list) {
8956 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008957 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008958 free(pr);
8959 }
8960}
8961
8962void free_http_req_rules(struct list *r)
8963{
Willy Tarreauff011f22011-01-06 17:51:27 +01008964 struct http_req_rule *tr, *pr;
8965
8966 list_for_each_entry_safe(pr, tr, r, list) {
8967 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008968 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008969 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008970
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008971 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008972 free(pr);
8973 }
8974}
8975
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008976/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008977struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8978{
8979 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008980 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008981 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008982 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008983
8984 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8985 if (!rule) {
8986 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008987 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008988 }
8989
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008990 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008991 rule->action = HTTP_REQ_ACT_ALLOW;
8992 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008993 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008994 rule->action = HTTP_REQ_ACT_DENY;
8995 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008996 } else if (!strcmp(args[0], "tarpit")) {
8997 rule->action = HTTP_REQ_ACT_TARPIT;
8998 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008999 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009000 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009001 cur_arg = 1;
9002
9003 while(*args[cur_arg]) {
9004 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009005 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009006 cur_arg+=2;
9007 continue;
9008 } else
9009 break;
9010 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009011 } else if (!strcmp(args[0], "set-nice")) {
9012 rule->action = HTTP_REQ_ACT_SET_NICE;
9013 cur_arg = 1;
9014
9015 if (!*args[cur_arg] ||
9016 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9017 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9018 file, linenum, args[0]);
9019 goto out_err;
9020 }
9021 rule->arg.nice = atoi(args[cur_arg]);
9022 if (rule->arg.nice < -1024)
9023 rule->arg.nice = -1024;
9024 else if (rule->arg.nice > 1024)
9025 rule->arg.nice = 1024;
9026 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009027 } else if (!strcmp(args[0], "set-tos")) {
9028#ifdef IP_TOS
9029 char *err;
9030 rule->action = HTTP_REQ_ACT_SET_TOS;
9031 cur_arg = 1;
9032
9033 if (!*args[cur_arg] ||
9034 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9035 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9036 file, linenum, args[0]);
9037 goto out_err;
9038 }
9039
9040 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9041 if (err && *err != '\0') {
9042 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9043 file, linenum, err, args[0]);
9044 goto out_err;
9045 }
9046 cur_arg++;
9047#else
9048 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9049 goto out_err;
9050#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009051 } else if (!strcmp(args[0], "set-mark")) {
9052#ifdef SO_MARK
9053 char *err;
9054 rule->action = HTTP_REQ_ACT_SET_MARK;
9055 cur_arg = 1;
9056
9057 if (!*args[cur_arg] ||
9058 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9059 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9060 file, linenum, args[0]);
9061 goto out_err;
9062 }
9063
9064 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9065 if (err && *err != '\0') {
9066 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9067 file, linenum, err, args[0]);
9068 goto out_err;
9069 }
9070 cur_arg++;
9071 global.last_checks |= LSTCHK_NETADM;
9072#else
9073 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9074 goto out_err;
9075#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009076 } else if (!strcmp(args[0], "set-log-level")) {
9077 rule->action = HTTP_REQ_ACT_SET_LOGL;
9078 cur_arg = 1;
9079
9080 if (!*args[cur_arg] ||
9081 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9082 bad_log_level:
9083 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9084 file, linenum, args[0]);
9085 goto out_err;
9086 }
9087 if (strcmp(args[cur_arg], "silent") == 0)
9088 rule->arg.loglevel = -1;
9089 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9090 goto bad_log_level;
9091 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009092 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9093 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9094 cur_arg = 1;
9095
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009096 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9097 (*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 +01009098 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9099 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009100 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009101 }
9102
9103 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9104 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9105 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009106
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009107 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009108 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009109 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9110 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009111 free(proxy->conf.lfs_file);
9112 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9113 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009114 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009115 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9116 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009117 cur_arg = 1;
9118
9119 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009120 (*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 -06009121 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9122 file, linenum, args[0]);
9123 goto out_err;
9124 }
9125
9126 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9127 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9128 LIST_INIT(&rule->arg.hdr_add.fmt);
9129
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009130 error = NULL;
9131 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9132 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9133 args[cur_arg + 1], error);
9134 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009135 goto out_err;
9136 }
9137
9138 proxy->conf.args.ctx = ARGC_HRQ;
9139 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9140 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9141 file, linenum);
9142
9143 free(proxy->conf.lfs_file);
9144 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9145 proxy->conf.lfs_line = proxy->conf.args.line;
9146 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009147 } else if (strcmp(args[0], "del-header") == 0) {
9148 rule->action = HTTP_REQ_ACT_DEL_HDR;
9149 cur_arg = 1;
9150
9151 if (!*args[cur_arg] ||
9152 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9153 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9154 file, linenum, args[0]);
9155 goto out_err;
9156 }
9157
9158 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9159 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9160
9161 proxy->conf.args.ctx = ARGC_HRQ;
9162 free(proxy->conf.lfs_file);
9163 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9164 proxy->conf.lfs_line = proxy->conf.args.line;
9165 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009166 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9167 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009168 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009169 struct sample_expr *expr;
9170 unsigned int where;
9171 char *err = NULL;
9172
9173 cur_arg = 1;
9174 proxy->conf.args.ctx = ARGC_TRK;
9175
9176 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9177 if (!expr) {
9178 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9179 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9180 free(err);
9181 goto out_err;
9182 }
9183
9184 where = 0;
9185 if (proxy->cap & PR_CAP_FE)
9186 where |= SMP_VAL_FE_HRQ_HDR;
9187 if (proxy->cap & PR_CAP_BE)
9188 where |= SMP_VAL_BE_HRQ_HDR;
9189
9190 if (!(expr->fetch->val & where)) {
9191 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9192 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9193 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9194 args[cur_arg-1], sample_src_names(expr->fetch->use));
9195 free(expr);
9196 goto out_err;
9197 }
9198
9199 if (strcmp(args[cur_arg], "table") == 0) {
9200 cur_arg++;
9201 if (!args[cur_arg]) {
9202 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9203 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9204 free(expr);
9205 goto out_err;
9206 }
9207 /* we copy the table name for now, it will be resolved later */
9208 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9209 cur_arg++;
9210 }
9211 rule->act_prm.trk_ctr.expr = expr;
9212 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009213 } else if (strcmp(args[0], "redirect") == 0) {
9214 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009215 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009216
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009217 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009218 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9219 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9220 goto out_err;
9221 }
9222
9223 /* this redirect rule might already contain a parsed condition which
9224 * we'll pass to the http-request rule.
9225 */
9226 rule->action = HTTP_REQ_ACT_REDIR;
9227 rule->arg.redir = redir;
9228 rule->cond = redir->cond;
9229 redir->cond = NULL;
9230 cur_arg = 2;
9231 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009232 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9233 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9234 rule->action = HTTP_REQ_ACT_ADD_ACL;
9235 /*
9236 * '+ 8' for 'add-acl('
9237 * '- 9' for 'add-acl(' + trailing ')'
9238 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009239 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009240
9241 cur_arg = 1;
9242
9243 if (!*args[cur_arg] ||
9244 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9245 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9246 file, linenum, args[0]);
9247 goto out_err;
9248 }
9249
9250 LIST_INIT(&rule->arg.map.key);
9251 proxy->conf.args.ctx = ARGC_HRQ;
9252 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9253 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9254 file, linenum);
9255 free(proxy->conf.lfs_file);
9256 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9257 proxy->conf.lfs_line = proxy->conf.args.line;
9258 cur_arg += 1;
9259 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9260 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9261 rule->action = HTTP_REQ_ACT_DEL_ACL;
9262 /*
9263 * '+ 8' for 'del-acl('
9264 * '- 9' for 'del-acl(' + trailing ')'
9265 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009266 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009267
9268 cur_arg = 1;
9269
9270 if (!*args[cur_arg] ||
9271 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9272 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9273 file, linenum, args[0]);
9274 goto out_err;
9275 }
9276
9277 LIST_INIT(&rule->arg.map.key);
9278 proxy->conf.args.ctx = ARGC_HRQ;
9279 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9280 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9281 file, linenum);
9282 free(proxy->conf.lfs_file);
9283 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9284 proxy->conf.lfs_line = proxy->conf.args.line;
9285 cur_arg += 1;
9286 } else if (strncmp(args[0], "del-map", 7) == 0) {
9287 /* http-request del-map(<reference (map name)>) <key pattern> */
9288 rule->action = HTTP_REQ_ACT_DEL_MAP;
9289 /*
9290 * '+ 8' for 'del-map('
9291 * '- 9' for 'del-map(' + trailing ')'
9292 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009293 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009294
9295 cur_arg = 1;
9296
9297 if (!*args[cur_arg] ||
9298 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9299 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9300 file, linenum, args[0]);
9301 goto out_err;
9302 }
9303
9304 LIST_INIT(&rule->arg.map.key);
9305 proxy->conf.args.ctx = ARGC_HRQ;
9306 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9307 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9308 file, linenum);
9309 free(proxy->conf.lfs_file);
9310 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9311 proxy->conf.lfs_line = proxy->conf.args.line;
9312 cur_arg += 1;
9313 } else if (strncmp(args[0], "set-map", 7) == 0) {
9314 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9315 rule->action = HTTP_REQ_ACT_SET_MAP;
9316 /*
9317 * '+ 8' for 'set-map('
9318 * '- 9' for 'set-map(' + trailing ')'
9319 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009320 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009321
9322 cur_arg = 1;
9323
9324 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9325 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9326 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9327 file, linenum, args[0]);
9328 goto out_err;
9329 }
9330
9331 LIST_INIT(&rule->arg.map.key);
9332 LIST_INIT(&rule->arg.map.value);
9333 proxy->conf.args.ctx = ARGC_HRQ;
9334
9335 /* key pattern */
9336 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9337 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9338 file, linenum);
9339
9340 /* value pattern */
9341 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9342 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9343 file, linenum);
9344 free(proxy->conf.lfs_file);
9345 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9346 proxy->conf.lfs_line = proxy->conf.args.line;
9347
9348 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009349 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9350 char *errmsg = NULL;
9351 cur_arg = 1;
9352 /* try in the module list */
9353 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9354 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9355 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9356 free(errmsg);
9357 goto out_err;
9358 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009359 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009360 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 +01009361 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009362 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009363 }
9364
9365 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9366 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009367 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009368
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009369 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9370 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9371 file, linenum, args[0], errmsg);
9372 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009373 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009374 }
9375 rule->cond = cond;
9376 }
9377 else if (*args[cur_arg]) {
9378 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9379 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9380 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009381 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009382 }
9383
9384 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009385 out_err:
9386 free(rule);
9387 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009388}
9389
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009390/* parse an "http-respose" rule */
9391struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9392{
9393 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009394 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009395 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009396 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009397
9398 rule = calloc(1, sizeof(*rule));
9399 if (!rule) {
9400 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9401 goto out_err;
9402 }
9403
9404 if (!strcmp(args[0], "allow")) {
9405 rule->action = HTTP_RES_ACT_ALLOW;
9406 cur_arg = 1;
9407 } else if (!strcmp(args[0], "deny")) {
9408 rule->action = HTTP_RES_ACT_DENY;
9409 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009410 } else if (!strcmp(args[0], "set-nice")) {
9411 rule->action = HTTP_RES_ACT_SET_NICE;
9412 cur_arg = 1;
9413
9414 if (!*args[cur_arg] ||
9415 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9416 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9417 file, linenum, args[0]);
9418 goto out_err;
9419 }
9420 rule->arg.nice = atoi(args[cur_arg]);
9421 if (rule->arg.nice < -1024)
9422 rule->arg.nice = -1024;
9423 else if (rule->arg.nice > 1024)
9424 rule->arg.nice = 1024;
9425 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009426 } else if (!strcmp(args[0], "set-tos")) {
9427#ifdef IP_TOS
9428 char *err;
9429 rule->action = HTTP_RES_ACT_SET_TOS;
9430 cur_arg = 1;
9431
9432 if (!*args[cur_arg] ||
9433 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9434 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9435 file, linenum, args[0]);
9436 goto out_err;
9437 }
9438
9439 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9440 if (err && *err != '\0') {
9441 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9442 file, linenum, err, args[0]);
9443 goto out_err;
9444 }
9445 cur_arg++;
9446#else
9447 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9448 goto out_err;
9449#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009450 } else if (!strcmp(args[0], "set-mark")) {
9451#ifdef SO_MARK
9452 char *err;
9453 rule->action = HTTP_RES_ACT_SET_MARK;
9454 cur_arg = 1;
9455
9456 if (!*args[cur_arg] ||
9457 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9458 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9459 file, linenum, args[0]);
9460 goto out_err;
9461 }
9462
9463 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9464 if (err && *err != '\0') {
9465 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9466 file, linenum, err, args[0]);
9467 goto out_err;
9468 }
9469 cur_arg++;
9470 global.last_checks |= LSTCHK_NETADM;
9471#else
9472 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9473 goto out_err;
9474#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009475 } else if (!strcmp(args[0], "set-log-level")) {
9476 rule->action = HTTP_RES_ACT_SET_LOGL;
9477 cur_arg = 1;
9478
9479 if (!*args[cur_arg] ||
9480 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9481 bad_log_level:
9482 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9483 file, linenum, args[0]);
9484 goto out_err;
9485 }
9486 if (strcmp(args[cur_arg], "silent") == 0)
9487 rule->arg.loglevel = -1;
9488 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9489 goto bad_log_level;
9490 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009491 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9492 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9493 cur_arg = 1;
9494
9495 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9496 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9497 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9498 file, linenum, args[0]);
9499 goto out_err;
9500 }
9501
9502 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9503 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9504 LIST_INIT(&rule->arg.hdr_add.fmt);
9505
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009506 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009507 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009508 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9509 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009510 free(proxy->conf.lfs_file);
9511 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9512 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009513 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009514 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009515 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009516 cur_arg = 1;
9517
9518 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009519 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9520 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009521 file, linenum, args[0]);
9522 goto out_err;
9523 }
9524
9525 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9526 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9527 LIST_INIT(&rule->arg.hdr_add.fmt);
9528
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009529 error = NULL;
9530 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9531 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9532 args[cur_arg + 1], error);
9533 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009534 goto out_err;
9535 }
9536
9537 proxy->conf.args.ctx = ARGC_HRQ;
9538 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9539 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9540 file, linenum);
9541
9542 free(proxy->conf.lfs_file);
9543 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9544 proxy->conf.lfs_line = proxy->conf.args.line;
9545 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009546 } else if (strcmp(args[0], "del-header") == 0) {
9547 rule->action = HTTP_RES_ACT_DEL_HDR;
9548 cur_arg = 1;
9549
9550 if (!*args[cur_arg] ||
9551 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9552 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9553 file, linenum, args[0]);
9554 goto out_err;
9555 }
9556
9557 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9558 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9559
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009560 proxy->conf.args.ctx = ARGC_HRS;
9561 free(proxy->conf.lfs_file);
9562 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9563 proxy->conf.lfs_line = proxy->conf.args.line;
9564 cur_arg += 1;
9565 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9566 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9567 rule->action = HTTP_RES_ACT_ADD_ACL;
9568 /*
9569 * '+ 8' for 'add-acl('
9570 * '- 9' for 'add-acl(' + trailing ')'
9571 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009572 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009573
9574 cur_arg = 1;
9575
9576 if (!*args[cur_arg] ||
9577 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9578 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9579 file, linenum, args[0]);
9580 goto out_err;
9581 }
9582
9583 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009584 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009585 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9586 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9587 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009588 free(proxy->conf.lfs_file);
9589 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9590 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009591
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009592 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009593 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9594 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9595 rule->action = HTTP_RES_ACT_DEL_ACL;
9596 /*
9597 * '+ 8' for 'del-acl('
9598 * '- 9' for 'del-acl(' + trailing ')'
9599 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009600 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009601
9602 cur_arg = 1;
9603
9604 if (!*args[cur_arg] ||
9605 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9606 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9607 file, linenum, args[0]);
9608 goto out_err;
9609 }
9610
9611 LIST_INIT(&rule->arg.map.key);
9612 proxy->conf.args.ctx = ARGC_HRS;
9613 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9614 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9615 file, linenum);
9616 free(proxy->conf.lfs_file);
9617 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9618 proxy->conf.lfs_line = proxy->conf.args.line;
9619 cur_arg += 1;
9620 } else if (strncmp(args[0], "del-map", 7) == 0) {
9621 /* http-response del-map(<reference (map name)>) <key pattern> */
9622 rule->action = HTTP_RES_ACT_DEL_MAP;
9623 /*
9624 * '+ 8' for 'del-map('
9625 * '- 9' for 'del-map(' + trailing ')'
9626 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009627 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009628
9629 cur_arg = 1;
9630
9631 if (!*args[cur_arg] ||
9632 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9633 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9634 file, linenum, args[0]);
9635 goto out_err;
9636 }
9637
9638 LIST_INIT(&rule->arg.map.key);
9639 proxy->conf.args.ctx = ARGC_HRS;
9640 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9641 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9642 file, linenum);
9643 free(proxy->conf.lfs_file);
9644 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9645 proxy->conf.lfs_line = proxy->conf.args.line;
9646 cur_arg += 1;
9647 } else if (strncmp(args[0], "set-map", 7) == 0) {
9648 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9649 rule->action = HTTP_RES_ACT_SET_MAP;
9650 /*
9651 * '+ 8' for 'set-map('
9652 * '- 9' for 'set-map(' + trailing ')'
9653 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009654 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009655
9656 cur_arg = 1;
9657
9658 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9659 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9660 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9661 file, linenum, args[0]);
9662 goto out_err;
9663 }
9664
9665 LIST_INIT(&rule->arg.map.key);
9666 LIST_INIT(&rule->arg.map.value);
9667
9668 proxy->conf.args.ctx = ARGC_HRS;
9669
9670 /* key pattern */
9671 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9672 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9673 file, linenum);
9674
9675 /* value pattern */
9676 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9677 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9678 file, linenum);
9679
9680 free(proxy->conf.lfs_file);
9681 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9682 proxy->conf.lfs_line = proxy->conf.args.line;
9683
9684 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009685 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9686 char *errmsg = NULL;
9687 cur_arg = 1;
9688 /* try in the module list */
9689 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9690 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9691 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9692 free(errmsg);
9693 goto out_err;
9694 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009695 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009696 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 +02009697 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9698 goto out_err;
9699 }
9700
9701 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9702 struct acl_cond *cond;
9703 char *errmsg = NULL;
9704
9705 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9706 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9707 file, linenum, args[0], errmsg);
9708 free(errmsg);
9709 goto out_err;
9710 }
9711 rule->cond = cond;
9712 }
9713 else if (*args[cur_arg]) {
9714 Alert("parsing [%s:%d]: 'http-response %s' expects"
9715 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9716 file, linenum, args[0], args[cur_arg]);
9717 goto out_err;
9718 }
9719
9720 return rule;
9721 out_err:
9722 free(rule);
9723 return NULL;
9724}
9725
Willy Tarreau4baae242012-12-27 12:00:31 +01009726/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009727 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9728 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009729 */
9730struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009731 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009732{
9733 struct redirect_rule *rule;
9734 int cur_arg;
9735 int type = REDIRECT_TYPE_NONE;
9736 int code = 302;
9737 const char *destination = NULL;
9738 const char *cookie = NULL;
9739 int cookie_set = 0;
9740 unsigned int flags = REDIRECT_FLAG_NONE;
9741 struct acl_cond *cond = NULL;
9742
9743 cur_arg = 0;
9744 while (*(args[cur_arg])) {
9745 if (strcmp(args[cur_arg], "location") == 0) {
9746 if (!*args[cur_arg + 1])
9747 goto missing_arg;
9748
9749 type = REDIRECT_TYPE_LOCATION;
9750 cur_arg++;
9751 destination = args[cur_arg];
9752 }
9753 else if (strcmp(args[cur_arg], "prefix") == 0) {
9754 if (!*args[cur_arg + 1])
9755 goto missing_arg;
9756
9757 type = REDIRECT_TYPE_PREFIX;
9758 cur_arg++;
9759 destination = args[cur_arg];
9760 }
9761 else if (strcmp(args[cur_arg], "scheme") == 0) {
9762 if (!*args[cur_arg + 1])
9763 goto missing_arg;
9764
9765 type = REDIRECT_TYPE_SCHEME;
9766 cur_arg++;
9767 destination = args[cur_arg];
9768 }
9769 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9770 if (!*args[cur_arg + 1])
9771 goto missing_arg;
9772
9773 cur_arg++;
9774 cookie = args[cur_arg];
9775 cookie_set = 1;
9776 }
9777 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9778 if (!*args[cur_arg + 1])
9779 goto missing_arg;
9780
9781 cur_arg++;
9782 cookie = args[cur_arg];
9783 cookie_set = 0;
9784 }
9785 else if (strcmp(args[cur_arg], "code") == 0) {
9786 if (!*args[cur_arg + 1])
9787 goto missing_arg;
9788
9789 cur_arg++;
9790 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009791 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009792 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009793 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009794 args[cur_arg - 1], args[cur_arg]);
9795 return NULL;
9796 }
9797 }
9798 else if (!strcmp(args[cur_arg],"drop-query")) {
9799 flags |= REDIRECT_FLAG_DROP_QS;
9800 }
9801 else if (!strcmp(args[cur_arg],"append-slash")) {
9802 flags |= REDIRECT_FLAG_APPEND_SLASH;
9803 }
9804 else if (strcmp(args[cur_arg], "if") == 0 ||
9805 strcmp(args[cur_arg], "unless") == 0) {
9806 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9807 if (!cond) {
9808 memprintf(errmsg, "error in condition: %s", *errmsg);
9809 return NULL;
9810 }
9811 break;
9812 }
9813 else {
9814 memprintf(errmsg,
9815 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9816 args[cur_arg]);
9817 return NULL;
9818 }
9819 cur_arg++;
9820 }
9821
9822 if (type == REDIRECT_TYPE_NONE) {
9823 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9824 return NULL;
9825 }
9826
9827 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9828 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009829 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009830
9831 if (!use_fmt) {
9832 /* old-style static redirect rule */
9833 rule->rdr_str = strdup(destination);
9834 rule->rdr_len = strlen(destination);
9835 }
9836 else {
9837 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009838
9839 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9840 * if prefix == "/", we don't want to add anything, otherwise it
9841 * makes it hard for the user to configure a self-redirection.
9842 */
Godbachd9722032014-12-18 15:44:58 +08009843 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009844 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009845 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009846 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9847 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009848 free(curproxy->conf.lfs_file);
9849 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9850 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009851 }
9852 }
9853
Willy Tarreau4baae242012-12-27 12:00:31 +01009854 if (cookie) {
9855 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9856 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9857 */
9858 rule->cookie_len = strlen(cookie);
9859 if (cookie_set) {
9860 rule->cookie_str = malloc(rule->cookie_len + 10);
9861 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9862 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9863 rule->cookie_len += 9;
9864 } else {
9865 rule->cookie_str = malloc(rule->cookie_len + 21);
9866 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9867 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9868 rule->cookie_len += 20;
9869 }
9870 }
9871 rule->type = type;
9872 rule->code = code;
9873 rule->flags = flags;
9874 LIST_INIT(&rule->list);
9875 return rule;
9876
9877 missing_arg:
9878 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9879 return NULL;
9880}
9881
Willy Tarreau8797c062007-05-07 00:55:35 +02009882/************************************************************************/
9883/* The code below is dedicated to ACL parsing and matching */
9884/************************************************************************/
9885
9886
Willy Tarreau14174bc2012-04-16 14:34:04 +02009887/* This function ensures that the prerequisites for an L7 fetch are ready,
9888 * which means that a request or response is ready. If some data is missing,
9889 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009890 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9891 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009892 *
9893 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009894 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9895 * decide whether or not an HTTP message is present ;
9896 * 0 if the requested data cannot be fetched or if it is certain that
9897 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009898 * 1 if an HTTP message is ready
9899 */
9900static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009901smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009902 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009903{
9904 struct http_txn *txn = l7;
9905 struct http_msg *msg = &txn->req;
9906
9907 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9908 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9909 */
9910
9911 if (unlikely(!s || !txn))
9912 return 0;
9913
9914 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009915 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009916
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009917 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009918 /* If the buffer does not leave enough free space at the end,
9919 * we must first realign it.
9920 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009921 if (s->req.buf->p > s->req.buf->data &&
9922 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9923 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009924
Willy Tarreau14174bc2012-04-16 14:34:04 +02009925 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009926 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009927 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009928
9929 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009930 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009931 http_msg_analyzer(msg, &txn->hdr_idx);
9932
9933 /* Still no valid request ? */
9934 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009935 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009936 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009937 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009938 }
9939 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009940 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009941 return 0;
9942 }
9943
9944 /* OK we just got a valid HTTP request. We have some minor
9945 * preparation to perform so that further checks can rely
9946 * on HTTP tests.
9947 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009948
9949 /* If the request was parsed but was too large, we must absolutely
9950 * return an error so that it is not processed. At the moment this
9951 * cannot happen, but if the parsers are to change in the future,
9952 * we want this check to be maintained.
9953 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009954 if (unlikely(s->req.buf->i + s->req.buf->p >
9955 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009956 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009957 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009958 return 1;
9959 }
9960
Willy Tarreau9b28e032012-10-12 23:49:43 +02009961 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009962 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9963 s->flags |= SN_REDIRECTABLE;
9964
Willy Tarreau506d0502013-07-06 13:29:24 +02009965 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9966 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009967 }
9968
Willy Tarreau506d0502013-07-06 13:29:24 +02009969 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009970 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009971 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009972
9973 /* otherwise everything's ready for the request */
9974 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009975 else {
9976 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009977 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9978 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009979 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009980 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009981 }
9982
9983 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009984 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009985 return 1;
9986}
Willy Tarreau8797c062007-05-07 00:55:35 +02009987
Willy Tarreau6c616e02014-06-25 16:56:41 +02009988/* Note: these functinos *do* modify the sample. Even in case of success, at
9989 * least the type and uint value are modified.
9990 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009991#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009992 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 +02009993
Willy Tarreau24e32d82012-04-23 23:55:44 +02009994#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009995 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 +02009996
Willy Tarreau8797c062007-05-07 00:55:35 +02009997
9998/* 1. Check on METHOD
9999 * We use the pre-parsed method if it is known, and store its number as an
10000 * integer. If it is unknown, we use the pointer and the length.
10001 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010002static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010003{
10004 int len, meth;
10005
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010006 len = strlen(text);
10007 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010008
10009 pattern->val.i = meth;
10010 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010011 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010012 pattern->len = len;
10013 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010014 else {
10015 pattern->ptr.str = NULL;
10016 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010017 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010018 return 1;
10019}
10020
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010021/* This function fetches the method of current HTTP request and stores
10022 * it in the global pattern struct as a chunk. There are two possibilities :
10023 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10024 * in <len> and <ptr> is NULL ;
10025 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10026 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010027 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010028 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010029static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010030smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010031 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010032{
10033 int meth;
10034 struct http_txn *txn = l7;
10035
Willy Tarreau24e32d82012-04-23 23:55:44 +020010036 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010037
Willy Tarreau8797c062007-05-07 00:55:35 +020010038 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010039 smp->type = SMP_T_METH;
10040 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010041 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010042 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10043 /* ensure the indexes are not affected */
10044 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010045 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010046 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10047 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010048 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010049 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010050 return 1;
10051}
10052
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010053/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010054static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010055{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010056 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010057 struct pattern_list *lst;
10058 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010059
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010060 list_for_each_entry(lst, &expr->patterns, list) {
10061 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010062
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010063 /* well-known method */
10064 if (pattern->val.i != HTTP_METH_OTHER) {
10065 if (smp->data.meth.meth == pattern->val.i)
10066 return pattern;
10067 else
10068 continue;
10069 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010070
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010071 /* Other method, we must compare the strings */
10072 if (pattern->len != smp->data.meth.str.len)
10073 continue;
10074
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010075 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010076 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10077 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010078 return pattern;
10079 }
10080 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010081}
10082
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010083static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010084smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010085 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010086{
10087 struct http_txn *txn = l7;
10088 char *ptr;
10089 int len;
10090
Willy Tarreauc0239e02012-04-16 14:42:55 +020010091 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010092
Willy Tarreau8797c062007-05-07 00:55:35 +020010093 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010094 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010095
10096 while ((len-- > 0) && (*ptr++ != '/'));
10097 if (len <= 0)
10098 return 0;
10099
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010100 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010101 smp->data.str.str = ptr;
10102 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010103
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010104 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010105 return 1;
10106}
10107
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010108static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010109smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010110 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010111{
10112 struct http_txn *txn = l7;
10113 char *ptr;
10114 int len;
10115
Willy Tarreauc0239e02012-04-16 14:42:55 +020010116 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010117
Willy Tarreauf26b2522012-12-14 08:33:14 +010010118 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10119 return 0;
10120
Willy Tarreau8797c062007-05-07 00:55:35 +020010121 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010122 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010123
10124 while ((len-- > 0) && (*ptr++ != '/'));
10125 if (len <= 0)
10126 return 0;
10127
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010128 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010129 smp->data.str.str = ptr;
10130 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010131
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010132 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010133 return 1;
10134}
10135
10136/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010137static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010138smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010139 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010140{
10141 struct http_txn *txn = l7;
10142 char *ptr;
10143 int len;
10144
Willy Tarreauc0239e02012-04-16 14:42:55 +020010145 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010146
Willy Tarreauf26b2522012-12-14 08:33:14 +010010147 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10148 return 0;
10149
Willy Tarreau8797c062007-05-07 00:55:35 +020010150 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010151 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010152
Willy Tarreauf853c462012-04-23 18:53:56 +020010153 smp->type = SMP_T_UINT;
10154 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010155 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010156 return 1;
10157}
10158
10159/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010160static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010161smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010162 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010163{
10164 struct http_txn *txn = l7;
10165
Willy Tarreauc0239e02012-04-16 14:42:55 +020010166 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010167
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010168 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010169 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010170 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010171 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010172 return 1;
10173}
10174
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010175static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010176smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010177 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010178{
10179 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010180 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010181
Willy Tarreauc0239e02012-04-16 14:42:55 +020010182 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010183
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010184 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 +020010185 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010186 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010187
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010188 smp->type = SMP_T_IPV4;
10189 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010190 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010191 return 1;
10192}
10193
10194static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010195smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010196 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010197{
10198 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010199 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010200
Willy Tarreauc0239e02012-04-16 14:42:55 +020010201 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010202
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010203 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 +020010204 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10205 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010206
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010207 smp->type = SMP_T_UINT;
10208 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010209 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010210 return 1;
10211}
10212
Willy Tarreau185b5c42012-04-26 15:11:51 +020010213/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10214 * Accepts an optional argument of type string containing the header field name,
10215 * and an optional argument of type signed or unsigned integer to request an
10216 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010217 * headers are considered from the first one. It does not stop on commas and
10218 * returns full lines instead (useful for User-Agent or Date for example).
10219 */
10220static int
10221smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010222 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010223{
10224 struct http_txn *txn = l7;
10225 struct hdr_idx *idx = &txn->hdr_idx;
10226 struct hdr_ctx *ctx = smp->ctx.a[0];
10227 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10228 int occ = 0;
10229 const char *name_str = NULL;
10230 int name_len = 0;
10231
10232 if (!ctx) {
10233 /* first call */
10234 ctx = &static_hdr_ctx;
10235 ctx->idx = 0;
10236 smp->ctx.a[0] = ctx;
10237 }
10238
10239 if (args) {
10240 if (args[0].type != ARGT_STR)
10241 return 0;
10242 name_str = args[0].data.str.str;
10243 name_len = args[0].data.str.len;
10244
10245 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10246 occ = args[1].data.uint;
10247 }
10248
10249 CHECK_HTTP_MESSAGE_FIRST();
10250
10251 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10252 /* search for header from the beginning */
10253 ctx->idx = 0;
10254
10255 if (!occ && !(opt & SMP_OPT_ITERATE))
10256 /* no explicit occurrence and single fetch => last header by default */
10257 occ = -1;
10258
10259 if (!occ)
10260 /* prepare to report multiple occurrences for ACL fetches */
10261 smp->flags |= SMP_F_NOT_LAST;
10262
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010263 smp->type = SMP_T_STR;
10264 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010265 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10266 return 1;
10267
10268 smp->flags &= ~SMP_F_NOT_LAST;
10269 return 0;
10270}
10271
10272/* 6. Check on HTTP header count. The number of occurrences is returned.
10273 * Accepts exactly 1 argument of type string. It does not stop on commas and
10274 * returns full lines instead (useful for User-Agent or Date for example).
10275 */
10276static int
10277smp_fetch_fhdr_cnt(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 Tarreau04ff9f12013-06-10 18:39:42 +020010279{
10280 struct http_txn *txn = l7;
10281 struct hdr_idx *idx = &txn->hdr_idx;
10282 struct hdr_ctx ctx;
10283 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10284 int cnt;
10285
10286 if (!args || args->type != ARGT_STR)
10287 return 0;
10288
10289 CHECK_HTTP_MESSAGE_FIRST();
10290
10291 ctx.idx = 0;
10292 cnt = 0;
10293 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10294 cnt++;
10295
10296 smp->type = SMP_T_UINT;
10297 smp->data.uint = cnt;
10298 smp->flags = SMP_F_VOL_HDR;
10299 return 1;
10300}
10301
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010302static int
10303smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010304 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010305{
10306 struct http_txn *txn = l7;
10307 struct hdr_idx *idx = &txn->hdr_idx;
10308 struct hdr_ctx ctx;
10309 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10310 struct chunk *temp;
10311 char del = ',';
10312
10313 if (args && args->type == ARGT_STR)
10314 del = *args[0].data.str.str;
10315
10316 CHECK_HTTP_MESSAGE_FIRST();
10317
10318 temp = get_trash_chunk();
10319
10320 ctx.idx = 0;
10321 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10322 if (temp->len)
10323 temp->str[temp->len++] = del;
10324 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10325 temp->len += ctx.del;
10326 }
10327
10328 smp->type = SMP_T_STR;
10329 smp->data.str.str = temp->str;
10330 smp->data.str.len = temp->len;
10331 smp->flags = SMP_F_VOL_HDR;
10332 return 1;
10333}
10334
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010335/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10336 * Accepts an optional argument of type string containing the header field name,
10337 * and an optional argument of type signed or unsigned integer to request an
10338 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010339 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010340 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010341static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010342smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010343 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010344{
10345 struct http_txn *txn = l7;
10346 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010347 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010348 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010349 int occ = 0;
10350 const char *name_str = NULL;
10351 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010352
Willy Tarreaua890d072013-04-02 12:01:06 +020010353 if (!ctx) {
10354 /* first call */
10355 ctx = &static_hdr_ctx;
10356 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010357 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010358 }
10359
Willy Tarreau185b5c42012-04-26 15:11:51 +020010360 if (args) {
10361 if (args[0].type != ARGT_STR)
10362 return 0;
10363 name_str = args[0].data.str.str;
10364 name_len = args[0].data.str.len;
10365
10366 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10367 occ = args[1].data.uint;
10368 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010369
Willy Tarreaue333ec92012-04-16 16:26:40 +020010370 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010371
Willy Tarreau185b5c42012-04-26 15:11:51 +020010372 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010373 /* search for header from the beginning */
10374 ctx->idx = 0;
10375
Willy Tarreau185b5c42012-04-26 15:11:51 +020010376 if (!occ && !(opt & SMP_OPT_ITERATE))
10377 /* no explicit occurrence and single fetch => last header by default */
10378 occ = -1;
10379
10380 if (!occ)
10381 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010382 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010383
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010384 smp->type = SMP_T_STR;
10385 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010386 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 +020010387 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010388
Willy Tarreau37406352012-04-23 16:16:37 +020010389 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010390 return 0;
10391}
10392
Willy Tarreauc11416f2007-06-17 16:58:38 +020010393/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010394 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010395 */
10396static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010397smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010398 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010399{
10400 struct http_txn *txn = l7;
10401 struct hdr_idx *idx = &txn->hdr_idx;
10402 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010403 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010404 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010405
Willy Tarreau24e32d82012-04-23 23:55:44 +020010406 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010407 return 0;
10408
Willy Tarreaue333ec92012-04-16 16:26:40 +020010409 CHECK_HTTP_MESSAGE_FIRST();
10410
Willy Tarreau33a7e692007-06-10 19:45:56 +020010411 ctx.idx = 0;
10412 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010413 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 +020010414 cnt++;
10415
Willy Tarreauf853c462012-04-23 18:53:56 +020010416 smp->type = SMP_T_UINT;
10417 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010418 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010419 return 1;
10420}
10421
Willy Tarreau185b5c42012-04-26 15:11:51 +020010422/* Fetch an HTTP header's integer value. The integer value is returned. It
10423 * takes a mandatory argument of type string and an optional one of type int
10424 * to designate a specific occurrence. It returns an unsigned integer, which
10425 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010426 */
10427static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010428smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010429 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010430{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010431 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010432
Willy Tarreauf853c462012-04-23 18:53:56 +020010433 if (ret > 0) {
10434 smp->type = SMP_T_UINT;
10435 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10436 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010437
Willy Tarreaud53e2422012-04-16 17:21:11 +020010438 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010439}
10440
Cyril Bonté69fa9922012-10-25 00:01:06 +020010441/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10442 * and an optional one of type int to designate a specific occurrence.
10443 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010444 */
10445static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010446smp_fetch_hdr_ip(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 Tarreau106f9792009-09-19 07:54:16 +020010448{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010449 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010450
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010451 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010452 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10453 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010454 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010455 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010456 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010457 if (smp->data.str.len < temp->size - 1) {
10458 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10459 temp->str[smp->data.str.len] = '\0';
10460 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10461 smp->type = SMP_T_IPV6;
10462 break;
10463 }
10464 }
10465 }
10466
Willy Tarreaud53e2422012-04-16 17:21:11 +020010467 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010468 if (!(smp->flags & SMP_F_NOT_LAST))
10469 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010470 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010471 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010472}
10473
Willy Tarreau737b0c12007-06-10 21:28:46 +020010474/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10475 * the first '/' after the possible hostname, and ends before the possible '?'.
10476 */
10477static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010478smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010479 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010480{
10481 struct http_txn *txn = l7;
10482 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010483
Willy Tarreauc0239e02012-04-16 14:42:55 +020010484 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010485
Willy Tarreau9b28e032012-10-12 23:49:43 +020010486 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010487 ptr = http_get_path(txn);
10488 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010489 return 0;
10490
10491 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010492 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010493 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010494
10495 while (ptr < end && *ptr != '?')
10496 ptr++;
10497
Willy Tarreauf853c462012-04-23 18:53:56 +020010498 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010499 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010500 return 1;
10501}
10502
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010503/* This produces a concatenation of the first occurrence of the Host header
10504 * followed by the path component if it begins with a slash ('/'). This means
10505 * that '*' will not be added, resulting in exactly the first Host entry.
10506 * If no Host header is found, then the path is returned as-is. The returned
10507 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010508 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010509 */
10510static int
10511smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010512 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010513{
10514 struct http_txn *txn = l7;
10515 char *ptr, *end, *beg;
10516 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010517 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010518
10519 CHECK_HTTP_MESSAGE_FIRST();
10520
10521 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010522 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010523 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010524
10525 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010526 temp = get_trash_chunk();
10527 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010528 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010529 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010530 smp->data.str.len = ctx.vlen;
10531
10532 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010533 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010534 beg = http_get_path(txn);
10535 if (!beg)
10536 beg = end;
10537
10538 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10539
10540 if (beg < ptr && *beg == '/') {
10541 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10542 smp->data.str.len += ptr - beg;
10543 }
10544
10545 smp->flags = SMP_F_VOL_1ST;
10546 return 1;
10547}
10548
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010549/* This produces a 32-bit hash of the concatenation of the first occurrence of
10550 * the Host header followed by the path component if it begins with a slash ('/').
10551 * This means that '*' will not be added, resulting in exactly the first Host
10552 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010553 * is hashed using the path hash followed by a full avalanche hash and provides a
10554 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010555 * high-traffic sites without having to store whole paths.
10556 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010557int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010558smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010559 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010560{
10561 struct http_txn *txn = l7;
10562 struct hdr_ctx ctx;
10563 unsigned int hash = 0;
10564 char *ptr, *beg, *end;
10565 int len;
10566
10567 CHECK_HTTP_MESSAGE_FIRST();
10568
10569 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010570 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010571 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10572 ptr = ctx.line + ctx.val;
10573 len = ctx.vlen;
10574 while (len--)
10575 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10576 }
10577
10578 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010579 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010580 beg = http_get_path(txn);
10581 if (!beg)
10582 beg = end;
10583
10584 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10585
10586 if (beg < ptr && *beg == '/') {
10587 while (beg < ptr)
10588 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10589 }
10590 hash = full_hash(hash);
10591
10592 smp->type = SMP_T_UINT;
10593 smp->data.uint = hash;
10594 smp->flags = SMP_F_VOL_1ST;
10595 return 1;
10596}
10597
Willy Tarreau4a550602012-12-09 14:53:32 +010010598/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010599 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10600 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10601 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010602 * that in environments where IPv6 is insignificant, truncating the output to
10603 * 8 bytes would still work.
10604 */
10605static int
10606smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010607 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010608{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010609 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010610 struct connection *cli_conn = objt_conn(l4->si[0].end);
10611
10612 if (!cli_conn)
10613 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010614
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010615 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010616 return 0;
10617
Willy Tarreau47ca5452012-12-23 20:22:19 +010010618 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010619 *(unsigned int *)temp->str = htonl(smp->data.uint);
10620 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010621
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010622 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010623 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010624 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010625 temp->len += 4;
10626 break;
10627 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010628 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010629 temp->len += 16;
10630 break;
10631 default:
10632 return 0;
10633 }
10634
10635 smp->data.str = *temp;
10636 smp->type = SMP_T_BIN;
10637 return 1;
10638}
10639
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010640/* Extracts the query string, which comes after the question mark '?'. If no
10641 * question mark is found, nothing is returned. Otherwise it returns a sample
10642 * of type string carrying the whole query string.
10643 */
10644static int
10645smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010646 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010647{
10648 struct http_txn *txn = l7;
10649 char *ptr, *end;
10650
10651 CHECK_HTTP_MESSAGE_FIRST();
10652
10653 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10654 end = ptr + txn->req.sl.rq.u_l;
10655
10656 /* look up the '?' */
10657 do {
10658 if (ptr == end)
10659 return 0;
10660 } while (*ptr++ != '?');
10661
10662 smp->type = SMP_T_STR;
10663 smp->data.str.str = ptr;
10664 smp->data.str.len = end - ptr;
10665 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10666 return 1;
10667}
10668
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010669static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010670smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010671 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010672{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010673 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10674 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10675 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010676
Willy Tarreau24e32d82012-04-23 23:55:44 +020010677 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010678
Willy Tarreauf853c462012-04-23 18:53:56 +020010679 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010680 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010681 return 1;
10682}
10683
Willy Tarreau7f18e522010-10-22 20:04:13 +020010684/* return a valid test if the current request is the first one on the connection */
10685static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010686smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010687 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010688{
10689 if (!s)
10690 return 0;
10691
Willy Tarreauf853c462012-04-23 18:53:56 +020010692 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010693 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010694 return 1;
10695}
10696
Willy Tarreau34db1082012-04-19 17:16:54 +020010697/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010698static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010699smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010700 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010701{
10702
Willy Tarreau24e32d82012-04-23 23:55:44 +020010703 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010704 return 0;
10705
Willy Tarreauc0239e02012-04-16 14:42:55 +020010706 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010707
Willy Tarreauc0239e02012-04-16 14:42:55 +020010708 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010709 return 0;
10710
Willy Tarreauf853c462012-04-23 18:53:56 +020010711 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010712 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010713 return 1;
10714}
Willy Tarreau8797c062007-05-07 00:55:35 +020010715
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010716/* Accepts exactly 1 argument of type userlist */
10717static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010718smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010719 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010720{
10721
10722 if (!args || args->type != ARGT_USR)
10723 return 0;
10724
10725 CHECK_HTTP_MESSAGE_FIRST();
10726
10727 if (!get_http_auth(l4))
10728 return 0;
10729
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010730 /* if the user does not belong to the userlist or has a wrong password,
10731 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010732 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010733 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010734 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10735 return 0;
10736
10737 /* pat_match_auth() will need the user list */
10738 smp->ctx.a[0] = args->data.usr;
10739
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010740 smp->type = SMP_T_STR;
10741 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010742 smp->data.str.str = l4->txn.auth.user;
10743 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010744
10745 return 1;
10746}
10747
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010748/* Try to find the next occurrence of a cookie name in a cookie header value.
10749 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10750 * the cookie value is returned into *value and *value_l, and the function
10751 * returns a pointer to the next pointer to search from if the value was found.
10752 * Otherwise if the cookie was not found, NULL is returned and neither value
10753 * nor value_l are touched. The input <hdr> string should first point to the
10754 * header's value, and the <hdr_end> pointer must point to the first character
10755 * not part of the value. <list> must be non-zero if value may represent a list
10756 * of values (cookie headers). This makes it faster to abort parsing when no
10757 * list is expected.
10758 */
10759static char *
10760extract_cookie_value(char *hdr, const char *hdr_end,
10761 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010762 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010763{
10764 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10765 char *next;
10766
10767 /* we search at least a cookie name followed by an equal, and more
10768 * generally something like this :
10769 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10770 */
10771 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10772 /* Iterate through all cookies on this line */
10773
10774 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10775 att_beg++;
10776
10777 /* find att_end : this is the first character after the last non
10778 * space before the equal. It may be equal to hdr_end.
10779 */
10780 equal = att_end = att_beg;
10781
10782 while (equal < hdr_end) {
10783 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10784 break;
10785 if (http_is_spht[(unsigned char)*equal++])
10786 continue;
10787 att_end = equal;
10788 }
10789
10790 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10791 * is between <att_beg> and <equal>, both may be identical.
10792 */
10793
10794 /* look for end of cookie if there is an equal sign */
10795 if (equal < hdr_end && *equal == '=') {
10796 /* look for the beginning of the value */
10797 val_beg = equal + 1;
10798 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10799 val_beg++;
10800
10801 /* find the end of the value, respecting quotes */
10802 next = find_cookie_value_end(val_beg, hdr_end);
10803
10804 /* make val_end point to the first white space or delimitor after the value */
10805 val_end = next;
10806 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10807 val_end--;
10808 } else {
10809 val_beg = val_end = next = equal;
10810 }
10811
10812 /* We have nothing to do with attributes beginning with '$'. However,
10813 * they will automatically be removed if a header before them is removed,
10814 * since they're supposed to be linked together.
10815 */
10816 if (*att_beg == '$')
10817 continue;
10818
10819 /* Ignore cookies with no equal sign */
10820 if (equal == next)
10821 continue;
10822
10823 /* Now we have the cookie name between att_beg and att_end, and
10824 * its value between val_beg and val_end.
10825 */
10826
10827 if (att_end - att_beg == cookie_name_l &&
10828 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10829 /* let's return this value and indicate where to go on from */
10830 *value = val_beg;
10831 *value_l = val_end - val_beg;
10832 return next + 1;
10833 }
10834
10835 /* Set-Cookie headers only have the name in the first attr=value part */
10836 if (!list)
10837 break;
10838 }
10839
10840 return NULL;
10841}
10842
William Lallemanda43ba4e2014-01-28 18:14:25 +010010843/* Fetch a captured HTTP request header. The index is the position of
10844 * the "capture" option in the configuration file
10845 */
10846static int
10847smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010848 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010849{
10850 struct proxy *fe = l4->fe;
10851 struct http_txn *txn = l7;
10852 int idx;
10853
10854 if (!args || args->type != ARGT_UINT)
10855 return 0;
10856
10857 idx = args->data.uint;
10858
10859 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10860 return 0;
10861
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010862 smp->type = SMP_T_STR;
10863 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010864 smp->data.str.str = txn->req.cap[idx];
10865 smp->data.str.len = strlen(txn->req.cap[idx]);
10866
10867 return 1;
10868}
10869
10870/* Fetch a captured HTTP response header. The index is the position of
10871 * the "capture" option in the configuration file
10872 */
10873static int
10874smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010875 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010876{
10877 struct proxy *fe = l4->fe;
10878 struct http_txn *txn = l7;
10879 int idx;
10880
10881 if (!args || args->type != ARGT_UINT)
10882 return 0;
10883
10884 idx = args->data.uint;
10885
10886 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10887 return 0;
10888
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010889 smp->type = SMP_T_STR;
10890 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010891 smp->data.str.str = txn->rsp.cap[idx];
10892 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10893
10894 return 1;
10895}
10896
William Lallemand65ad6e12014-01-31 15:08:02 +010010897/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10898static int
10899smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010900 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010901{
10902 struct chunk *temp;
10903 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010904 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010905
10906 if (!txn->uri)
10907 return 0;
10908
William Lallemand96a77852014-02-05 00:30:02 +010010909 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010910
William Lallemand96a77852014-02-05 00:30:02 +010010911 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10912 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010913
William Lallemand96a77852014-02-05 00:30:02 +010010914 temp = get_trash_chunk();
10915 temp->str = txn->uri;
10916 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010917 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010918 smp->type = SMP_T_STR;
10919 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010920
10921 return 1;
10922
10923}
10924
10925/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10926static int
10927smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010928 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010929{
10930 struct chunk *temp;
10931 struct http_txn *txn = l7;
10932 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010933
10934 if (!txn->uri)
10935 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010936
William Lallemand65ad6e12014-01-31 15:08:02 +010010937 ptr = txn->uri;
10938
10939 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10940 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010941
William Lallemand65ad6e12014-01-31 15:08:02 +010010942 if (!*ptr)
10943 return 0;
10944
10945 ptr++; /* skip the space */
10946
10947 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010948 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010949 if (!ptr)
10950 return 0;
10951 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10952 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010953
10954 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010955 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010956 smp->type = SMP_T_STR;
10957 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010958
10959 return 1;
10960}
10961
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010962/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10963 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10964 */
10965static int
10966smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010967 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010968{
10969 struct http_txn *txn = l7;
10970
10971 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10972 return 0;
10973
10974 if (txn->req.flags & HTTP_MSGF_VER_11)
10975 smp->data.str.str = "HTTP/1.1";
10976 else
10977 smp->data.str.str = "HTTP/1.0";
10978
10979 smp->data.str.len = 8;
10980 smp->type = SMP_T_STR;
10981 smp->flags = SMP_F_CONST;
10982 return 1;
10983
10984}
10985
10986/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10987 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10988 */
10989static int
10990smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010991 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010992{
10993 struct http_txn *txn = l7;
10994
10995 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10996 return 0;
10997
10998 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10999 smp->data.str.str = "HTTP/1.1";
11000 else
11001 smp->data.str.str = "HTTP/1.0";
11002
11003 smp->data.str.len = 8;
11004 smp->type = SMP_T_STR;
11005 smp->flags = SMP_F_CONST;
11006 return 1;
11007
11008}
11009
William Lallemand65ad6e12014-01-31 15:08:02 +010011010
Willy Tarreaue333ec92012-04-16 16:26:40 +020011011/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011012 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011013 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011014 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011015 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011016 * Accepts exactly 1 argument of type string. If the input options indicate
11017 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011018 * The returned sample is of type CSTR. Can be used to parse cookies in other
11019 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011020 */
William Lallemand07c8b242014-05-02 17:11:07 +020011021int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011022 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011023{
11024 struct http_txn *txn = l7;
11025 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011026 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011027 const struct http_msg *msg;
11028 const char *hdr_name;
11029 int hdr_name_len;
11030 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011031 int occ = 0;
11032 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011033
Willy Tarreau24e32d82012-04-23 23:55:44 +020011034 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011035 return 0;
11036
Willy Tarreaua890d072013-04-02 12:01:06 +020011037 if (!ctx) {
11038 /* first call */
11039 ctx = &static_hdr_ctx;
11040 ctx->idx = 0;
11041 smp->ctx.a[2] = ctx;
11042 }
11043
Willy Tarreaue333ec92012-04-16 16:26:40 +020011044 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011045
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011046 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011047 msg = &txn->req;
11048 hdr_name = "Cookie";
11049 hdr_name_len = 6;
11050 } else {
11051 msg = &txn->rsp;
11052 hdr_name = "Set-Cookie";
11053 hdr_name_len = 10;
11054 }
11055
Willy Tarreau28376d62012-04-26 21:26:10 +020011056 if (!occ && !(opt & SMP_OPT_ITERATE))
11057 /* no explicit occurrence and single fetch => last cookie by default */
11058 occ = -1;
11059
11060 /* OK so basically here, either we want only one value and it's the
11061 * last one, or we want to iterate over all of them and we fetch the
11062 * next one.
11063 */
11064
Willy Tarreau9b28e032012-10-12 23:49:43 +020011065 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011066 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011067 /* search for the header from the beginning, we must first initialize
11068 * the search parameters.
11069 */
Willy Tarreau37406352012-04-23 16:16:37 +020011070 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011071 ctx->idx = 0;
11072 }
11073
Willy Tarreau28376d62012-04-26 21:26:10 +020011074 smp->flags |= SMP_F_VOL_HDR;
11075
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011076 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011077 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11078 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011079 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11080 goto out;
11081
Willy Tarreau24e32d82012-04-23 23:55:44 +020011082 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011083 continue;
11084
Willy Tarreau37406352012-04-23 16:16:37 +020011085 smp->ctx.a[0] = ctx->line + ctx->val;
11086 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011087 }
11088
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011089 smp->type = SMP_T_STR;
11090 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011091 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011092 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011093 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011094 &smp->data.str.str,
11095 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011096 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011097 found = 1;
11098 if (occ >= 0) {
11099 /* one value was returned into smp->data.str.{str,len} */
11100 smp->flags |= SMP_F_NOT_LAST;
11101 return 1;
11102 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011103 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011104 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011105 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011106 /* all cookie headers and values were scanned. If we're looking for the
11107 * last occurrence, we may return it now.
11108 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011109 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011110 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011111 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011112}
11113
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011114/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011115 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011116 * multiple cookies may be parsed on the same line. The returned sample is of
11117 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011118 */
11119static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010011120smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011121 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011122{
11123 struct http_txn *txn = l7;
11124 struct hdr_idx *idx = &txn->hdr_idx;
11125 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011126 const struct http_msg *msg;
11127 const char *hdr_name;
11128 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011129 int cnt;
11130 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011131 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011132
Willy Tarreau24e32d82012-04-23 23:55:44 +020011133 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011134 return 0;
11135
Willy Tarreaue333ec92012-04-16 16:26:40 +020011136 CHECK_HTTP_MESSAGE_FIRST();
11137
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011138 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011139 msg = &txn->req;
11140 hdr_name = "Cookie";
11141 hdr_name_len = 6;
11142 } else {
11143 msg = &txn->rsp;
11144 hdr_name = "Set-Cookie";
11145 hdr_name_len = 10;
11146 }
11147
Willy Tarreau9b28e032012-10-12 23:49:43 +020011148 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011149 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011150 ctx.idx = 0;
11151 cnt = 0;
11152
11153 while (1) {
11154 /* Note: val_beg == NULL every time we need to fetch a new header */
11155 if (!val_beg) {
11156 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11157 break;
11158
Willy Tarreau24e32d82012-04-23 23:55:44 +020011159 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011160 continue;
11161
11162 val_beg = ctx.line + ctx.val;
11163 val_end = val_beg + ctx.vlen;
11164 }
11165
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011166 smp->type = SMP_T_STR;
11167 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011168 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011169 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011170 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011171 &smp->data.str.str,
11172 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011173 cnt++;
11174 }
11175 }
11176
Willy Tarreaub169eba2013-12-16 15:14:43 +010011177 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011178 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011179 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011180 return 1;
11181}
11182
Willy Tarreau51539362012-05-08 12:46:28 +020011183/* Fetch an cookie's integer value. The integer value is returned. It
11184 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11185 */
11186static int
11187smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011188 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011189{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011190 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011191
11192 if (ret > 0) {
11193 smp->type = SMP_T_UINT;
11194 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11195 }
11196
11197 return ret;
11198}
11199
Willy Tarreau8797c062007-05-07 00:55:35 +020011200/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011201/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011202/************************************************************************/
11203
David Cournapeau16023ee2010-12-23 20:55:41 +090011204/*
11205 * Given a path string and its length, find the position of beginning of the
11206 * query string. Returns NULL if no query string is found in the path.
11207 *
11208 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11209 *
11210 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11211 */
bedis4c75cca2012-10-05 08:38:24 +020011212static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011213{
11214 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011215
bedis4c75cca2012-10-05 08:38:24 +020011216 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011217 return p ? p + 1 : NULL;
11218}
11219
bedis4c75cca2012-10-05 08:38:24 +020011220static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011221{
bedis4c75cca2012-10-05 08:38:24 +020011222 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011223}
11224
11225/*
11226 * Given a url parameter, find the starting position of the first occurence,
11227 * or NULL if the parameter is not found.
11228 *
11229 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11230 * the function will return query_string+8.
11231 */
11232static char*
11233find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011234 char* url_param_name, size_t url_param_name_l,
11235 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011236{
11237 char *pos, *last;
11238
11239 pos = query_string;
11240 last = query_string + query_string_l - url_param_name_l - 1;
11241
11242 while (pos <= last) {
11243 if (pos[url_param_name_l] == '=') {
11244 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11245 return pos;
11246 pos += url_param_name_l + 1;
11247 }
bedis4c75cca2012-10-05 08:38:24 +020011248 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011249 pos++;
11250 pos++;
11251 }
11252 return NULL;
11253}
11254
11255/*
11256 * Given a url parameter name, returns its value and size into *value and
11257 * *value_l respectively, and returns non-zero. If the parameter is not found,
11258 * zero is returned and value/value_l are not touched.
11259 */
11260static int
11261find_url_param_value(char* path, size_t path_l,
11262 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011263 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011264{
11265 char *query_string, *qs_end;
11266 char *arg_start;
11267 char *value_start, *value_end;
11268
bedis4c75cca2012-10-05 08:38:24 +020011269 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011270 if (!query_string)
11271 return 0;
11272
11273 qs_end = path + path_l;
11274 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011275 url_param_name, url_param_name_l,
11276 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011277 if (!arg_start)
11278 return 0;
11279
11280 value_start = arg_start + url_param_name_l + 1;
11281 value_end = value_start;
11282
bedis4c75cca2012-10-05 08:38:24 +020011283 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011284 value_end++;
11285
11286 *value = value_start;
11287 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011288 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011289}
11290
11291static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011292smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011293 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011294{
bedis4c75cca2012-10-05 08:38:24 +020011295 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011296 struct http_txn *txn = l7;
11297 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011298
bedis4c75cca2012-10-05 08:38:24 +020011299 if (!args || args[0].type != ARGT_STR ||
11300 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011301 return 0;
11302
11303 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011304
bedis4c75cca2012-10-05 08:38:24 +020011305 if (args[1].type)
11306 delim = *args[1].data.str.str;
11307
Willy Tarreau9b28e032012-10-12 23:49:43 +020011308 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011309 args->data.str.str, args->data.str.len,
11310 &smp->data.str.str, &smp->data.str.len,
11311 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011312 return 0;
11313
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011314 smp->type = SMP_T_STR;
11315 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011316 return 1;
11317}
11318
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011319/* Return the signed integer value for the specified url parameter (see url_param
11320 * above).
11321 */
11322static int
11323smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011324 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011325{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011326 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011327
11328 if (ret > 0) {
11329 smp->type = SMP_T_UINT;
11330 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11331 }
11332
11333 return ret;
11334}
11335
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011336/* This produces a 32-bit hash of the concatenation of the first occurrence of
11337 * the Host header followed by the path component if it begins with a slash ('/').
11338 * This means that '*' will not be added, resulting in exactly the first Host
11339 * entry. If no Host header is found, then the path is used. The resulting value
11340 * is hashed using the url hash followed by a full avalanche hash and provides a
11341 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11342 * high-traffic sites without having to store whole paths.
11343 * this differs from the base32 functions in that it includes the url parameters
11344 * as well as the path
11345 */
11346static int
11347smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011348 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011349{
11350 struct http_txn *txn = l7;
11351 struct hdr_ctx ctx;
11352 unsigned int hash = 0;
11353 char *ptr, *beg, *end;
11354 int len;
11355
11356 CHECK_HTTP_MESSAGE_FIRST();
11357
11358 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011359 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011360 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11361 ptr = ctx.line + ctx.val;
11362 len = ctx.vlen;
11363 while (len--)
11364 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11365 }
11366
11367 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011368 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 +000011369 beg = http_get_path(txn);
11370 if (!beg)
11371 beg = end;
11372
11373 for (ptr = beg; ptr < end ; ptr++);
11374
11375 if (beg < ptr && *beg == '/') {
11376 while (beg < ptr)
11377 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11378 }
11379 hash = full_hash(hash);
11380
11381 smp->type = SMP_T_UINT;
11382 smp->data.uint = hash;
11383 smp->flags = SMP_F_VOL_1ST;
11384 return 1;
11385}
11386
11387/* This concatenates the source address with the 32-bit hash of the Host and
11388 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11389 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11390 * on the source address length. The URL hash is stored before the address so
11391 * that in environments where IPv6 is insignificant, truncating the output to
11392 * 8 bytes would still work.
11393 */
11394static int
11395smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011396 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011397{
11398 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011399 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011400
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011401 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011402 return 0;
11403
11404 temp = get_trash_chunk();
11405 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11406 temp->len += sizeof(smp->data.uint);
11407
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011408 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011409 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011410 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011411 temp->len += 4;
11412 break;
11413 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011414 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011415 temp->len += 16;
11416 break;
11417 default:
11418 return 0;
11419 }
11420
11421 smp->data.str = *temp;
11422 smp->type = SMP_T_BIN;
11423 return 1;
11424}
11425
Willy Tarreau185b5c42012-04-26 15:11:51 +020011426/* This function is used to validate the arguments passed to any "hdr" fetch
11427 * keyword. These keywords support an optional positive or negative occurrence
11428 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11429 * is assumed that the types are already the correct ones. Returns 0 on error,
11430 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11431 * error message in case of error, that the caller is responsible for freeing.
11432 * The initial location must either be freeable or NULL.
11433 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011434int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011435{
11436 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011437 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011438 return 0;
11439 }
11440 return 1;
11441}
11442
Willy Tarreau276fae92013-07-25 14:36:01 +020011443/* takes an UINT value on input supposed to represent the time since EPOCH,
11444 * adds an optional offset found in args[0] and emits a string representing
11445 * the date in RFC-1123/5322 format.
11446 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011447static int sample_conv_http_date(struct session *session, const struct arg *args,
11448 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011449{
11450 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11451 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11452 struct chunk *temp;
11453 struct tm *tm;
11454 time_t curr_date = smp->data.uint;
11455
11456 /* add offset */
11457 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11458 curr_date += args[0].data.sint;
11459
11460 tm = gmtime(&curr_date);
11461
11462 temp = get_trash_chunk();
11463 temp->len = snprintf(temp->str, temp->size - temp->len,
11464 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11465 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11466 tm->tm_hour, tm->tm_min, tm->tm_sec);
11467
11468 smp->data.str = *temp;
11469 smp->type = SMP_T_STR;
11470 return 1;
11471}
11472
Thierry FOURNIERad903512014-04-11 17:51:01 +020011473/* Match language range with language tag. RFC2616 14.4:
11474 *
11475 * A language-range matches a language-tag if it exactly equals
11476 * the tag, or if it exactly equals a prefix of the tag such
11477 * that the first tag character following the prefix is "-".
11478 *
11479 * Return 1 if the strings match, else return 0.
11480 */
11481static inline int language_range_match(const char *range, int range_len,
11482 const char *tag, int tag_len)
11483{
11484 const char *end = range + range_len;
11485 const char *tend = tag + tag_len;
11486 while (range < end) {
11487 if (*range == '-' && tag == tend)
11488 return 1;
11489 if (*range != *tag || tag == tend)
11490 return 0;
11491 range++;
11492 tag++;
11493 }
11494 /* Return true only if the last char of the tag is matched. */
11495 return tag == tend;
11496}
11497
11498/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011499static int sample_conv_q_prefered(struct session *session, const struct arg *args,
11500 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011501{
11502 const char *al = smp->data.str.str;
11503 const char *end = al + smp->data.str.len;
11504 const char *token;
11505 int toklen;
11506 int qvalue;
11507 const char *str;
11508 const char *w;
11509 int best_q = 0;
11510
11511 /* Set the constant to the sample, because the output of the
11512 * function will be peek in the constant configuration string.
11513 */
11514 smp->flags |= SMP_F_CONST;
11515 smp->data.str.size = 0;
11516 smp->data.str.str = "";
11517 smp->data.str.len = 0;
11518
11519 /* Parse the accept language */
11520 while (1) {
11521
11522 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011523 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011524 al++;
11525 if (al >= end)
11526 break;
11527
11528 /* Start of the fisrt word. */
11529 token = al;
11530
11531 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011532 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011533 al++;
11534 if (al == token)
11535 goto expect_comma;
11536
11537 /* Length of the token. */
11538 toklen = al - token;
11539 qvalue = 1000;
11540
11541 /* Check if the token exists in the list. If the token not exists,
11542 * jump to the next token.
11543 */
11544 str = args[0].data.str.str;
11545 w = str;
11546 while (1) {
11547 if (*str == ';' || *str == '\0') {
11548 if (language_range_match(token, toklen, w, str-w))
11549 goto look_for_q;
11550 if (*str == '\0')
11551 goto expect_comma;
11552 w = str + 1;
11553 }
11554 str++;
11555 }
11556 goto expect_comma;
11557
11558look_for_q:
11559
11560 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011561 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011562 al++;
11563 if (al >= end)
11564 goto process_value;
11565
11566 /* If ',' is found, process the result */
11567 if (*al == ',')
11568 goto process_value;
11569
11570 /* If the character is different from ';', look
11571 * for the end of the header part in best effort.
11572 */
11573 if (*al != ';')
11574 goto expect_comma;
11575
11576 /* Assumes that the char is ';', now expect "q=". */
11577 al++;
11578
11579 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011580 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011581 al++;
11582 if (al >= end)
11583 goto process_value;
11584
11585 /* Expect 'q'. If no 'q', continue in best effort */
11586 if (*al != 'q')
11587 goto process_value;
11588 al++;
11589
11590 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011591 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011592 al++;
11593 if (al >= end)
11594 goto process_value;
11595
11596 /* Expect '='. If no '=', continue in best effort */
11597 if (*al != '=')
11598 goto process_value;
11599 al++;
11600
11601 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011602 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011603 al++;
11604 if (al >= end)
11605 goto process_value;
11606
11607 /* Parse the q value. */
11608 qvalue = parse_qvalue(al, &al);
11609
11610process_value:
11611
11612 /* If the new q value is the best q value, then store the associated
11613 * language in the response. If qvalue is the biggest value (1000),
11614 * break the process.
11615 */
11616 if (qvalue > best_q) {
11617 smp->data.str.str = (char *)w;
11618 smp->data.str.len = str - w;
11619 if (qvalue >= 1000)
11620 break;
11621 best_q = qvalue;
11622 }
11623
11624expect_comma:
11625
11626 /* Expect comma or end. If the end is detected, quit the loop. */
11627 while (al < end && *al != ',')
11628 al++;
11629 if (al >= end)
11630 break;
11631
11632 /* Comma is found, jump it and restart the analyzer. */
11633 al++;
11634 }
11635
11636 /* Set default value if required. */
11637 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11638 smp->data.str.str = args[1].data.str.str;
11639 smp->data.str.len = args[1].data.str.len;
11640 }
11641
11642 /* Return true only if a matching language was found. */
11643 return smp->data.str.len != 0;
11644}
11645
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011646/* This function executes one of the set-{method,path,query,uri} actions. It
11647 * builds a string in the trash from the specified format string, then modifies
11648 * the relevant part of the request line accordingly. Then it updates various
11649 * pointers to the next elements which were moved, and the total buffer length.
11650 * It finds the action to be performed in p[2], previously filled by function
11651 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11652 * error, though this can be revisited when this code is finally exploited.
11653 */
11654int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn)
11655{
11656 char *cur_ptr, *cur_end;
11657 int offset;
11658 int delta;
11659
11660 chunk_reset(&trash);
11661
11662 /* prepare a '?' just in case we have to create a query string */
11663 trash.str[trash.len++] = '?';
11664 offset = 1;
11665 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11666
11667 switch (*(int *)&rule->arg.act.p[2]) {
11668 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011669 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011670 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11671
11672 /* adjust req line offsets and lengths */
11673 delta = trash.len - offset - (cur_end - cur_ptr);
11674 txn->req.sl.rq.m_l += delta;
11675 txn->req.sl.rq.u += delta;
11676 txn->req.sl.rq.v += delta;
11677 break;
11678
11679 case 1: // path
11680 cur_ptr = http_get_path(txn);
11681 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011682 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011683
11684 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011685 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011686 cur_end++;
11687
11688 /* adjust req line offsets and lengths */
11689 delta = trash.len - offset - (cur_end - cur_ptr);
11690 txn->req.sl.rq.u_l += delta;
11691 txn->req.sl.rq.v += delta;
11692 break;
11693
11694 case 2: // query
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011695 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011696 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11697 while (cur_ptr < cur_end && *cur_ptr != '?')
11698 cur_ptr++;
11699
11700 /* skip the question mark or indicate that we must insert it
11701 * (but only if the format string is not empty then).
11702 */
11703 if (cur_ptr < cur_end)
11704 cur_ptr++;
11705 else if (trash.len > 1)
11706 offset = 0;
11707
11708 /* adjust req line offsets and lengths */
11709 delta = trash.len - offset - (cur_end - cur_ptr);
11710 txn->req.sl.rq.u_l += delta;
11711 txn->req.sl.rq.v += delta;
11712 break;
11713
11714 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011715 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011716 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11717
11718 /* adjust req line offsets and lengths */
11719 delta = trash.len - offset - (cur_end - cur_ptr);
11720 txn->req.sl.rq.u_l += delta;
11721 txn->req.sl.rq.v += delta;
11722 break;
11723
11724 default:
11725 return -1;
11726 }
11727
11728 /* commit changes and adjust end of message */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011729 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011730 http_msg_move_end(&txn->req, delta);
11731 return 0;
11732}
11733
11734/* parse an http-request action among :
11735 * set-method
11736 * set-path
11737 * set-query
11738 * set-uri
11739 *
11740 * All of them accept a single argument of type string representing a log-format.
11741 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11742 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11743 * It returns 0 on success, < 0 on error.
11744 */
11745int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11746{
11747 int cur_arg = *orig_arg;
11748
11749 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11750
11751 switch (args[0][4]) {
11752 case 'm' :
11753 *(int *)&rule->arg.act.p[2] = 0;
11754 rule->action_ptr = http_action_set_req_line;
11755 break;
11756 case 'p' :
11757 *(int *)&rule->arg.act.p[2] = 1;
11758 rule->action_ptr = http_action_set_req_line;
11759 break;
11760 case 'q' :
11761 *(int *)&rule->arg.act.p[2] = 2;
11762 rule->action_ptr = http_action_set_req_line;
11763 break;
11764 case 'u' :
11765 *(int *)&rule->arg.act.p[2] = 3;
11766 rule->action_ptr = http_action_set_req_line;
11767 break;
11768 default:
11769 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11770 return -1;
11771 }
11772
11773 if (!*args[cur_arg] ||
11774 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11775 memprintf(err, "expects exactly 1 argument <format>");
11776 return -1;
11777 }
11778
11779 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11780 proxy->conf.args.ctx = ARGC_HRQ;
11781 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11782 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11783 proxy->conf.args.file, proxy->conf.args.line);
11784
11785 (*orig_arg)++;
11786 return 0;
11787}
11788
William Lallemand73025dd2014-04-24 14:38:37 +020011789/*
11790 * Return the struct http_req_action_kw associated to a keyword.
11791 */
11792struct http_req_action_kw *action_http_req_custom(const char *kw)
11793{
11794 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11795 struct http_req_action_kw_list *kw_list;
11796 int i;
11797
11798 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11799 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11800 if (!strcmp(kw, kw_list->kw[i].kw))
11801 return &kw_list->kw[i];
11802 }
11803 }
11804 }
11805 return NULL;
11806}
11807
11808/*
11809 * Return the struct http_res_action_kw associated to a keyword.
11810 */
11811struct http_res_action_kw *action_http_res_custom(const char *kw)
11812{
11813 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11814 struct http_res_action_kw_list *kw_list;
11815 int i;
11816
11817 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11818 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11819 if (!strcmp(kw, kw_list->kw[i].kw))
11820 return &kw_list->kw[i];
11821 }
11822 }
11823 }
11824 return NULL;
11825}
11826
Willy Tarreau4a568972010-05-12 08:08:50 +020011827/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011828/* All supported ACL keywords must be declared here. */
11829/************************************************************************/
11830
11831/* Note: must not be declared <const> as its list will be overwritten.
11832 * Please take care of keeping this list alphabetically sorted.
11833 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011834static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011835 { "base", "base", PAT_MATCH_STR },
11836 { "base_beg", "base", PAT_MATCH_BEG },
11837 { "base_dir", "base", PAT_MATCH_DIR },
11838 { "base_dom", "base", PAT_MATCH_DOM },
11839 { "base_end", "base", PAT_MATCH_END },
11840 { "base_len", "base", PAT_MATCH_LEN },
11841 { "base_reg", "base", PAT_MATCH_REG },
11842 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011843
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011844 { "cook", "req.cook", PAT_MATCH_STR },
11845 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11846 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11847 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11848 { "cook_end", "req.cook", PAT_MATCH_END },
11849 { "cook_len", "req.cook", PAT_MATCH_LEN },
11850 { "cook_reg", "req.cook", PAT_MATCH_REG },
11851 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011852
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011853 { "hdr", "req.hdr", PAT_MATCH_STR },
11854 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11855 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11856 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11857 { "hdr_end", "req.hdr", PAT_MATCH_END },
11858 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11859 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11860 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011861
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011862 /* these two declarations uses strings with list storage (in place
11863 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11864 * and delete functions are relative to the list management. The parse
11865 * and match method are related to the corresponding fetch methods. This
11866 * is very particular ACL declaration mode.
11867 */
11868 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11869 { "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 +020011870
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011871 { "path", "path", PAT_MATCH_STR },
11872 { "path_beg", "path", PAT_MATCH_BEG },
11873 { "path_dir", "path", PAT_MATCH_DIR },
11874 { "path_dom", "path", PAT_MATCH_DOM },
11875 { "path_end", "path", PAT_MATCH_END },
11876 { "path_len", "path", PAT_MATCH_LEN },
11877 { "path_reg", "path", PAT_MATCH_REG },
11878 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011880 { "req_ver", "req.ver", PAT_MATCH_STR },
11881 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011882
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011883 { "scook", "res.cook", PAT_MATCH_STR },
11884 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11885 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11886 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11887 { "scook_end", "res.cook", PAT_MATCH_END },
11888 { "scook_len", "res.cook", PAT_MATCH_LEN },
11889 { "scook_reg", "res.cook", PAT_MATCH_REG },
11890 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011891
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011892 { "shdr", "res.hdr", PAT_MATCH_STR },
11893 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11894 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11895 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11896 { "shdr_end", "res.hdr", PAT_MATCH_END },
11897 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11898 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11899 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011900
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011901 { "url", "url", PAT_MATCH_STR },
11902 { "url_beg", "url", PAT_MATCH_BEG },
11903 { "url_dir", "url", PAT_MATCH_DIR },
11904 { "url_dom", "url", PAT_MATCH_DOM },
11905 { "url_end", "url", PAT_MATCH_END },
11906 { "url_len", "url", PAT_MATCH_LEN },
11907 { "url_reg", "url", PAT_MATCH_REG },
11908 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011909
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011910 { "urlp", "urlp", PAT_MATCH_STR },
11911 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11912 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11913 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11914 { "urlp_end", "urlp", PAT_MATCH_END },
11915 { "urlp_len", "urlp", PAT_MATCH_LEN },
11916 { "urlp_reg", "urlp", PAT_MATCH_REG },
11917 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011918
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011919 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011920}};
11921
11922/************************************************************************/
11923/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011924/************************************************************************/
11925/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011926static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011927 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011928 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11929 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11930
William Lallemanda43ba4e2014-01-28 18:14:25 +010011931 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011932 { "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 +020011933
11934 /* retrieve these captures from the HTTP logs */
11935 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11936 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11937 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11938
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011939 { "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 +020011940 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011941
Willy Tarreau409bcde2013-01-08 00:31:00 +010011942 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11943 * are only here to match the ACL's name, are request-only and are used
11944 * for ACL compatibility only.
11945 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011946 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11947 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011948 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11949 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11950
11951 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11952 * only here to match the ACL's name, are request-only and are used for
11953 * ACL compatibility only.
11954 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011955 { "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 +010011956 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11957 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11958 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11959
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011960 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011961 { "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 +010011962 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011963 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011964 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011965 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011966
11967 /* HTTP protocol on the request path */
11968 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011969 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011970
11971 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011972 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11973 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011974
11975 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011976 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11977 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011978
Willy Tarreau18ed2562013-01-14 15:56:36 +010011979 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011980 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011981 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11982 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11983
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011984 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011985 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011986 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011987 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11988 { "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 +010011989 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011990 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11991
11992 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011993 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011994 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11995 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11996
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011997 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011998 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011999 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012000 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12001 { "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 +010012002 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012003 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12004
Willy Tarreau409bcde2013-01-08 00:31:00 +010012005 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012006 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012007 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12008 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012009 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012010
12011 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012012 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012013 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12014 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12015 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12016
12017 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012018 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012019 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12020 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012021 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12022 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012023 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12024 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012025 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12026 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012027}};
12028
Willy Tarreau8797c062007-05-07 00:55:35 +020012029
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012030/************************************************************************/
12031/* All supported converter keywords must be declared here. */
12032/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012033/* Note: must not be declared <const> as its list will be overwritten */
12034static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012035 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12036 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012037 { NULL, NULL, 0, 0, 0 },
12038}};
12039
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012040/************************************************************************/
12041/* All supported http-request action keywords must be declared here. */
12042/************************************************************************/
12043struct http_req_action_kw_list http_req_actions = {
12044 .scope = "http",
12045 .kw = {
12046 { "set-method", parse_set_req_line },
12047 { "set-path", parse_set_req_line },
12048 { "set-query", parse_set_req_line },
12049 { "set-uri", parse_set_req_line },
12050 }
12051};
12052
Willy Tarreau8797c062007-05-07 00:55:35 +020012053__attribute__((constructor))
12054static void __http_protocol_init(void)
12055{
12056 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012057 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012058 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012059 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012060}
12061
12062
Willy Tarreau58f10d72006-12-04 02:26:12 +010012063/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012064 * Local variables:
12065 * c-indent-level: 8
12066 * c-basic-offset: 8
12067 * End:
12068 */