blob: 2df2ce89649fb6eec6b2b4c02febf93166ce076f [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 Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.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 Tarreau87b09662015-04-03 00:22:06 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200256
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,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100452 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100453};
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 Tarreau87b09662015-04-03 00:22:06 +0200826static void http_server_error(struct stream *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 Tarreau87b09662015-04-03 00:22:06 +0200845/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100846 * and message.
847 */
848
Willy Tarreau87b09662015-04-03 00:22:06 +0200849struct chunk *http_error_message(struct stream *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 Tarreau87b09662015-04-03 00:22:06 +0200977void http_perform_server_redirect(struct stream *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 Tarreau87b09662015-04-03 00:22:06 +02001053void http_return_srv_error(struct stream *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
Willy Tarreau87b09662015-04-03 00:22:06 +02001426 * have the credentials overwritten by another stream 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
Willy Tarreau87b09662015-04-03 00:22:06 +02001435get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001436{
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
Willy Tarreau87b09662015-04-03 00:22:06 +02001445 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001446#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 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002239int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002240{
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) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002312 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 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) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002336 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002337 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 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002350int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002351{
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 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002448 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002449 trash.len = 18;
2450 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002451 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2452 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002453 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 Tarreau87b09662015-04-03 00:22:06 +02002463void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002464{
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 Tarreau87b09662015-04-03 00:22:06 +02002524int http_wait_for_request(struct stream *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 Tarreau87b09662015-04-03 00:22:06 +02002553 DPRINTF(stderr,"[%u] %s: stream=%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
Willy Tarreau87b09662015-04-03 00:22:06 +02002645 * on a keep-alive stream, if we encounter and error, close, t/o,
2646 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002647 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002648 * process_stream() 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 Tarreau87b09662015-04-03 00:22:06 +02002660 stream_inc_http_req_ctr(s);
2661 stream_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
Willy Tarreau87b09662015-04-03 00:22:06 +02002668 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 * 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 Tarreau87b09662015-04-03 00:22:06 +02002675 stream_inc_http_req_ctr(s);
2676 stream_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 Tarreau87b09662015-04-03 00:22:06 +02002694 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002695 }
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 Tarreau87b09662015-04-03 00:22:06 +02002702 stream_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 Tarreau87b09662015-04-03 00:22:06 +02002724 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002725 }
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 Tarreau87b09662015-04-03 00:22:06 +02002731 stream_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 Tarreau87b09662015-04-03 00:22:06 +02002757 stream_inc_http_err_ctr(s);
2758 stream_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 Tarreau87b09662015-04-03 00:22:06 +02002829 stream_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
Willy Tarreau87b09662015-04-03 00:22:06 +02003095 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003096 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003097int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003098{
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 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003232void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003233{
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
Willy Tarreau87b09662015-04-03 00:22:06 +02003249int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003250 const char* name, unsigned int name_len,
3251 const char *str, struct my_regex *re,
3252 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003253{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003254 struct hdr_ctx ctx;
3255 char *buf = msg->chn->buf->p;
3256 struct hdr_idx *idx = &s->txn.hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003257 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3258 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003259 struct chunk *output = get_trash_chunk();
3260
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003261 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003262
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003263 /* Choose the header browsing function. */
3264 switch (action) {
3265 case HTTP_REQ_ACT_REPLACE_VAL:
3266 case HTTP_RES_ACT_REPLACE_VAL:
3267 http_find_hdr_func = http_find_header2;
3268 break;
3269 case HTTP_REQ_ACT_REPLACE_HDR:
3270 case HTTP_RES_ACT_REPLACE_HDR:
3271 http_find_hdr_func = http_find_full_header2;
3272 break;
3273 default: /* impossible */
3274 return -1;
3275 }
3276
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003277 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3278 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003279 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003280 char *val = ctx.line + ctx.val;
3281 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003282
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003283 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3284 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003286 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003287 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003288 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003289
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003290 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003291
3292 hdr->len += delta;
3293 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003294
3295 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003296 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003297 }
3298
3299 return 0;
3300}
3301
Willy Tarreau87b09662015-04-03 00:22:06 +02003302static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003303 const char* name, unsigned int name_len,
3304 struct list *fmt, struct my_regex *re,
3305 int action)
3306{
3307 struct chunk *replace = get_trash_chunk();
3308
3309 replace->len = build_logline(s, replace->str, replace->size, fmt);
3310 if (replace->len >= replace->size - 1)
3311 return -1;
3312
3313 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3314}
3315
Willy Tarreau87b09662015-04-03 00:22:06 +02003316/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003317 * transaction <txn>. Returns the verdict of the first rule that prevents
3318 * further processing of the request (auth, deny, ...), and defaults to
3319 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3320 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3321 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003322 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003323enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003324http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003325{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003326 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003327 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003328 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003329 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003330
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003331 /* If "the current_rule_list" match the executed rule list, we are in
3332 * resume condition. If a resume is needed it is always in the action
3333 * and never in the ACL or converters. In this case, we initialise the
3334 * current rule, and go to the action execution point.
3335 */
3336 if (s->current_rule_list == rules) {
3337 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3338 goto resume_execution;
3339 }
3340 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003341 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003342 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003343 continue;
3344
Willy Tarreau96257ec2012-12-27 10:46:37 +01003345 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003346 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003347 int ret;
3348
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003349 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003350 ret = acl_pass(ret);
3351
Willy Tarreauff011f22011-01-06 17:51:27 +01003352 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003353 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003354
3355 if (!ret) /* condition not matched */
3356 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003357 }
3358
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003359resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003360
Willy Tarreau96257ec2012-12-27 10:46:37 +01003361 switch (rule->action) {
3362 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003363 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003364
3365 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003366 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003367
Willy Tarreauccbcc372012-12-27 12:37:57 +01003368 case HTTP_REQ_ACT_TARPIT:
3369 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003370 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003371
Willy Tarreau96257ec2012-12-27 10:46:37 +01003372 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003373 /* Auth might be performed on regular http-req rules as well as on stats */
3374 auth_realm = rule->arg.auth.realm;
3375 if (!auth_realm) {
3376 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3377 auth_realm = STATS_DEFAULT_REALM;
3378 else
3379 auth_realm = px->id;
3380 }
3381 /* send 401/407 depending on whether we use a proxy or not. We still
3382 * count one error, because normal browsing won't significantly
3383 * increase the counter but brute force attempts will.
3384 */
3385 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3386 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3387 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003388 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003389 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003390
Willy Tarreau81499eb2012-12-27 12:19:02 +01003391 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003392 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3393 return HTTP_RULE_RES_BADREQ;
3394 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003395
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003396 case HTTP_REQ_ACT_SET_NICE:
3397 s->task->nice = rule->arg.nice;
3398 break;
3399
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003400 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003401 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003402 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003403 break;
3404
Willy Tarreau51347ed2013-06-11 19:34:13 +02003405 case HTTP_REQ_ACT_SET_MARK:
3406#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003407 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003408 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003409#endif
3410 break;
3411
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003412 case HTTP_REQ_ACT_SET_LOGL:
3413 s->logs.level = rule->arg.loglevel;
3414 break;
3415
Sasha Pachev218f0642014-06-16 12:05:59 -06003416 case HTTP_REQ_ACT_REPLACE_HDR:
3417 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003418 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3419 rule->arg.hdr_add.name_len,
3420 &rule->arg.hdr_add.fmt,
3421 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003422 return HTTP_RULE_RES_BADREQ;
3423 break;
3424
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003425 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003426 ctx.idx = 0;
3427 /* remove all occurrences of the header */
3428 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3429 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3430 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003431 }
Willy Tarreau85603282015-01-21 20:39:27 +01003432 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003433
Willy Tarreau85603282015-01-21 20:39:27 +01003434 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003435 case HTTP_REQ_ACT_ADD_HDR:
3436 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3437 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3438 trash.len = rule->arg.hdr_add.name_len;
3439 trash.str[trash.len++] = ':';
3440 trash.str[trash.len++] = ' ';
3441 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 +01003442
3443 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3444 /* remove all occurrences of the header */
3445 ctx.idx = 0;
3446 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3447 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3448 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3449 }
3450 }
3451
Willy Tarreau96257ec2012-12-27 10:46:37 +01003452 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3453 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003454
3455 case HTTP_REQ_ACT_DEL_ACL:
3456 case HTTP_REQ_ACT_DEL_MAP: {
3457 struct pat_ref *ref;
3458 char *key;
3459 int len;
3460
3461 /* collect reference */
3462 ref = pat_ref_lookup(rule->arg.map.ref);
3463 if (!ref)
3464 continue;
3465
3466 /* collect key */
3467 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3468 key = trash.str;
3469 key[len] = '\0';
3470
3471 /* perform update */
3472 /* returned code: 1=ok, 0=ko */
3473 pat_ref_delete(ref, key);
3474
3475 break;
3476 }
3477
3478 case HTTP_REQ_ACT_ADD_ACL: {
3479 struct pat_ref *ref;
3480 char *key;
3481 struct chunk *trash_key;
3482 int len;
3483
3484 trash_key = get_trash_chunk();
3485
3486 /* collect reference */
3487 ref = pat_ref_lookup(rule->arg.map.ref);
3488 if (!ref)
3489 continue;
3490
3491 /* collect key */
3492 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3493 key = trash_key->str;
3494 key[len] = '\0';
3495
3496 /* perform update */
3497 /* add entry only if it does not already exist */
3498 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003499 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003500
3501 break;
3502 }
3503
3504 case HTTP_REQ_ACT_SET_MAP: {
3505 struct pat_ref *ref;
3506 char *key, *value;
3507 struct chunk *trash_key, *trash_value;
3508 int len;
3509
3510 trash_key = get_trash_chunk();
3511 trash_value = get_trash_chunk();
3512
3513 /* collect reference */
3514 ref = pat_ref_lookup(rule->arg.map.ref);
3515 if (!ref)
3516 continue;
3517
3518 /* collect key */
3519 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3520 key = trash_key->str;
3521 key[len] = '\0';
3522
3523 /* collect value */
3524 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3525 value = trash_value->str;
3526 value[len] = '\0';
3527
3528 /* perform update */
3529 if (pat_ref_find_elt(ref, key) != NULL)
3530 /* update entry if it exists */
3531 pat_ref_set(ref, key, value, NULL);
3532 else
3533 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003534 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003535
3536 break;
3537 }
William Lallemand73025dd2014-04-24 14:38:37 +02003538
3539 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003540 if (!rule->action_ptr(rule, px, s, txn)) {
3541 s->current_rule = &rule->list;
3542 return HTTP_RULE_RES_YIELD;
3543 }
William Lallemand73025dd2014-04-24 14:38:37 +02003544 break;
3545
3546 case HTTP_REQ_ACT_CUSTOM_STOP:
3547 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003548 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003549
3550 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3551 /* Note: only the first valid tracking parameter of each
3552 * applies.
3553 */
3554
3555 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3556 struct stktable *t;
3557 struct stksess *ts;
3558 struct stktable_key *key;
3559 void *ptr;
3560
3561 t = rule->act_prm.trk_ctr.table.t;
3562 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3563
3564 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003565 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003566
3567 /* let's count a new HTTP request as it's the first time we do it */
3568 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3569 if (ptr)
3570 stktable_data_cast(ptr, http_req_cnt)++;
3571
3572 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3573 if (ptr)
3574 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3575 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3576
3577 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3578 if (s->fe != s->be)
3579 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3580 }
3581 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003582 }
3583 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003584
3585 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003586 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003587}
3588
Willy Tarreau71241ab2012-12-27 11:30:54 +01003589
Willy Tarreau87b09662015-04-03 00:22:06 +02003590/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003591 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3592 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3593 * evaluation of next rule list. If *STOP is returned, the process must stop
3594 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3595 * rule. If *YIELD is returned, the czller must call again the function with
3596 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003597 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003598static enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003599http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003600{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003601 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003602 struct http_res_rule *rule;
3603 struct hdr_ctx ctx;
3604
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003605 /* If "the current_rule_list" match the executed rule list, we are in
3606 * resume condition. If a resume is needed it is always in the action
3607 * and never in the ACL or converters. In this case, we initialise the
3608 * current rule, and go to the action execution point.
3609 */
3610 if (s->current_rule_list == rules) {
3611 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3612 goto resume_execution;
3613 }
3614 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003615 list_for_each_entry(rule, rules, list) {
3616 if (rule->action >= HTTP_RES_ACT_MAX)
3617 continue;
3618
3619 /* check optional condition */
3620 if (rule->cond) {
3621 int ret;
3622
3623 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3624 ret = acl_pass(ret);
3625
3626 if (rule->cond->pol == ACL_COND_UNLESS)
3627 ret = !ret;
3628
3629 if (!ret) /* condition not matched */
3630 continue;
3631 }
3632
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003633resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003634
3635 switch (rule->action) {
3636 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003637 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003638
3639 case HTTP_RES_ACT_DENY:
3640 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003641 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003642
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003643 case HTTP_RES_ACT_SET_NICE:
3644 s->task->nice = rule->arg.nice;
3645 break;
3646
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003647 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau350f4872014-11-28 14:42:25 +01003648 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003649 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003650 break;
3651
Willy Tarreau51347ed2013-06-11 19:34:13 +02003652 case HTTP_RES_ACT_SET_MARK:
3653#ifdef SO_MARK
Willy Tarreau350f4872014-11-28 14:42:25 +01003654 if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003655 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003656#endif
3657 break;
3658
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003659 case HTTP_RES_ACT_SET_LOGL:
3660 s->logs.level = rule->arg.loglevel;
3661 break;
3662
Sasha Pachev218f0642014-06-16 12:05:59 -06003663 case HTTP_RES_ACT_REPLACE_HDR:
3664 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003665 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3666 rule->arg.hdr_add.name_len,
3667 &rule->arg.hdr_add.fmt,
3668 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003669 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003670 break;
3671
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003672 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003673 ctx.idx = 0;
3674 /* remove all occurrences of the header */
3675 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3676 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3677 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3678 }
Willy Tarreau85603282015-01-21 20:39:27 +01003679 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003680
Willy Tarreau85603282015-01-21 20:39:27 +01003681 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003682 case HTTP_RES_ACT_ADD_HDR:
3683 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3684 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3685 trash.len = rule->arg.hdr_add.name_len;
3686 trash.str[trash.len++] = ':';
3687 trash.str[trash.len++] = ' ';
3688 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 +01003689
3690 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3691 /* remove all occurrences of the header */
3692 ctx.idx = 0;
3693 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3694 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3695 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3696 }
3697 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003698 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3699 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003700
3701 case HTTP_RES_ACT_DEL_ACL:
3702 case HTTP_RES_ACT_DEL_MAP: {
3703 struct pat_ref *ref;
3704 char *key;
3705 int len;
3706
3707 /* collect reference */
3708 ref = pat_ref_lookup(rule->arg.map.ref);
3709 if (!ref)
3710 continue;
3711
3712 /* collect key */
3713 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3714 key = trash.str;
3715 key[len] = '\0';
3716
3717 /* perform update */
3718 /* returned code: 1=ok, 0=ko */
3719 pat_ref_delete(ref, key);
3720
3721 break;
3722 }
3723
3724 case HTTP_RES_ACT_ADD_ACL: {
3725 struct pat_ref *ref;
3726 char *key;
3727 struct chunk *trash_key;
3728 int len;
3729
3730 trash_key = get_trash_chunk();
3731
3732 /* collect reference */
3733 ref = pat_ref_lookup(rule->arg.map.ref);
3734 if (!ref)
3735 continue;
3736
3737 /* collect key */
3738 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3739 key = trash_key->str;
3740 key[len] = '\0';
3741
3742 /* perform update */
3743 /* check if the entry already exists */
3744 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003745 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003746
3747 break;
3748 }
3749
3750 case HTTP_RES_ACT_SET_MAP: {
3751 struct pat_ref *ref;
3752 char *key, *value;
3753 struct chunk *trash_key, *trash_value;
3754 int len;
3755
3756 trash_key = get_trash_chunk();
3757 trash_value = get_trash_chunk();
3758
3759 /* collect reference */
3760 ref = pat_ref_lookup(rule->arg.map.ref);
3761 if (!ref)
3762 continue;
3763
3764 /* collect key */
3765 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3766 key = trash_key->str;
3767 key[len] = '\0';
3768
3769 /* collect value */
3770 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3771 value = trash_value->str;
3772 value[len] = '\0';
3773
3774 /* perform update */
3775 if (pat_ref_find_elt(ref, key) != NULL)
3776 /* update entry if it exists */
3777 pat_ref_set(ref, key, value, NULL);
3778 else
3779 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003780 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003781
3782 break;
3783 }
William Lallemand73025dd2014-04-24 14:38:37 +02003784
3785 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003786 if (!rule->action_ptr(rule, px, s, txn)) {
3787 s->current_rule = &rule->list;
3788 return HTTP_RULE_RES_YIELD;
3789 }
William Lallemand73025dd2014-04-24 14:38:37 +02003790 break;
3791
3792 case HTTP_RES_ACT_CUSTOM_STOP:
3793 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003794 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 }
3796 }
3797
3798 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003799 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003800}
3801
3802
Willy Tarreau71241ab2012-12-27 11:30:54 +01003803/* Perform an HTTP redirect based on the information in <rule>. The function
3804 * returns non-zero on success, or zero in case of a, irrecoverable error such
3805 * as too large a request to build a valid response.
3806 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003807static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003808{
3809 struct http_msg *msg = &txn->req;
3810 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003811 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003812
3813 /* build redirect message */
3814 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003815 case 308:
3816 msg_fmt = HTTP_308;
3817 break;
3818 case 307:
3819 msg_fmt = HTTP_307;
3820 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003821 case 303:
3822 msg_fmt = HTTP_303;
3823 break;
3824 case 301:
3825 msg_fmt = HTTP_301;
3826 break;
3827 case 302:
3828 default:
3829 msg_fmt = HTTP_302;
3830 break;
3831 }
3832
3833 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3834 return 0;
3835
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003836 location = trash.str + trash.len;
3837
Willy Tarreau71241ab2012-12-27 11:30:54 +01003838 switch(rule->type) {
3839 case REDIRECT_TYPE_SCHEME: {
3840 const char *path;
3841 const char *host;
3842 struct hdr_ctx ctx;
3843 int pathlen;
3844 int hostlen;
3845
3846 host = "";
3847 hostlen = 0;
3848 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003849 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003850 host = ctx.line + ctx.val;
3851 hostlen = ctx.vlen;
3852 }
3853
3854 path = http_get_path(txn);
3855 /* build message using path */
3856 if (path) {
3857 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3858 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3859 int qs = 0;
3860 while (qs < pathlen) {
3861 if (path[qs] == '?') {
3862 pathlen = qs;
3863 break;
3864 }
3865 qs++;
3866 }
3867 }
3868 } else {
3869 path = "/";
3870 pathlen = 1;
3871 }
3872
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003873 if (rule->rdr_str) { /* this is an old "redirect" rule */
3874 /* check if we can add scheme + "://" + host + path */
3875 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3876 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003877
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003878 /* add scheme */
3879 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3880 trash.len += rule->rdr_len;
3881 }
3882 else {
3883 /* add scheme with executing log format */
3884 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003885
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003886 /* check if we can add scheme + "://" + host + path */
3887 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3888 return 0;
3889 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003890 /* add "://" */
3891 memcpy(trash.str + trash.len, "://", 3);
3892 trash.len += 3;
3893
3894 /* add host */
3895 memcpy(trash.str + trash.len, host, hostlen);
3896 trash.len += hostlen;
3897
3898 /* add path */
3899 memcpy(trash.str + trash.len, path, pathlen);
3900 trash.len += pathlen;
3901
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003902 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003903 if (trash.len && trash.str[trash.len - 1] != '/' &&
3904 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3905 if (trash.len > trash.size - 5)
3906 return 0;
3907 trash.str[trash.len] = '/';
3908 trash.len++;
3909 }
3910
3911 break;
3912 }
3913 case REDIRECT_TYPE_PREFIX: {
3914 const char *path;
3915 int pathlen;
3916
3917 path = http_get_path(txn);
3918 /* build message using path */
3919 if (path) {
3920 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3921 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3922 int qs = 0;
3923 while (qs < pathlen) {
3924 if (path[qs] == '?') {
3925 pathlen = qs;
3926 break;
3927 }
3928 qs++;
3929 }
3930 }
3931 } else {
3932 path = "/";
3933 pathlen = 1;
3934 }
3935
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003936 if (rule->rdr_str) { /* this is an old "redirect" rule */
3937 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3938 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003939
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003940 /* add prefix. Note that if prefix == "/", we don't want to
3941 * add anything, otherwise it makes it hard for the user to
3942 * configure a self-redirection.
3943 */
3944 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3945 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3946 trash.len += rule->rdr_len;
3947 }
3948 }
3949 else {
3950 /* add prefix with executing log format */
3951 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3952
3953 /* Check length */
3954 if (trash.len + pathlen > trash.size - 4)
3955 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003956 }
3957
3958 /* add path */
3959 memcpy(trash.str + trash.len, path, pathlen);
3960 trash.len += pathlen;
3961
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003962 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003963 if (trash.len && trash.str[trash.len - 1] != '/' &&
3964 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3965 if (trash.len > trash.size - 5)
3966 return 0;
3967 trash.str[trash.len] = '/';
3968 trash.len++;
3969 }
3970
3971 break;
3972 }
3973 case REDIRECT_TYPE_LOCATION:
3974 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003975 if (rule->rdr_str) { /* this is an old "redirect" rule */
3976 if (trash.len + rule->rdr_len > trash.size - 4)
3977 return 0;
3978
3979 /* add location */
3980 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3981 trash.len += rule->rdr_len;
3982 }
3983 else {
3984 /* add location with executing log format */
3985 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003986
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003987 /* Check left length */
3988 if (trash.len > trash.size - 4)
3989 return 0;
3990 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003991 break;
3992 }
3993
3994 if (rule->cookie_len) {
3995 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3996 trash.len += 14;
3997 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3998 trash.len += rule->cookie_len;
3999 memcpy(trash.str + trash.len, "\r\n", 2);
4000 trash.len += 2;
4001 }
4002
4003 /* add end of headers and the keep-alive/close status.
4004 * We may choose to set keep-alive if the Location begins
4005 * with a slash, because the client will come back to the
4006 * same server.
4007 */
4008 txn->status = rule->code;
4009 /* let's log the request time */
4010 s->logs.tv_request = now;
4011
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004012 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004013 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4014 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4015 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4016 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4017 /* keep-alive possible */
4018 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4019 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4020 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4021 trash.len += 30;
4022 } else {
4023 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4024 trash.len += 24;
4025 }
4026 }
4027 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4028 trash.len += 4;
4029 bo_inject(txn->rsp.chn, trash.str, trash.len);
4030 /* "eat" the request */
4031 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004032 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004033 msg->sov = 0;
4034 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004035 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036 txn->req.msg_state = HTTP_MSG_CLOSED;
4037 txn->rsp.msg_state = HTTP_MSG_DONE;
4038 } else {
4039 /* keep-alive not possible */
4040 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4041 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4042 trash.len += 29;
4043 } else {
4044 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4045 trash.len += 23;
4046 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004047 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004048 txn->req.chn->analysers = 0;
4049 }
4050
4051 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004052 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 if (!(s->flags & SN_FINST_MASK))
4054 s->flags |= SN_FINST_R;
4055
4056 return 1;
4057}
4058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004059/* This stream analyser runs all HTTP request processing which is common to
4060 * frontends and backends, which means blocking ACLs, filters, connection-close,
4061 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004062 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004063 * either needs more data or wants to immediately abort the request (eg: deny,
4064 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004065 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004066int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004067{
Willy Tarreaud787e662009-07-07 10:14:51 +02004068 struct http_txn *txn = &s->txn;
4069 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004071 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004072 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004073
Willy Tarreau655dce92009-11-08 13:10:58 +01004074 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004075 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004076 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004077 }
4078
Willy Tarreau87b09662015-04-03 00:22:06 +02004079 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004080 now_ms, __FUNCTION__,
4081 s,
4082 req,
4083 req->rex, req->wex,
4084 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004085 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004086 req->analysers);
4087
Willy Tarreau65410832014-04-28 21:25:43 +02004088 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004089 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004090
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004091 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004092 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4093 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004094
Willy Tarreau0b748332014-04-29 00:13:29 +02004095 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004096 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4097 goto return_prx_yield;
4098
Willy Tarreau0b748332014-04-29 00:13:29 +02004099 case HTTP_RULE_RES_CONT:
4100 case HTTP_RULE_RES_STOP: /* nothing to do */
4101 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004102
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4104 if (txn->flags & TX_CLTARPIT)
4105 goto tarpit;
4106 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004107
Willy Tarreau0b748332014-04-29 00:13:29 +02004108 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4109 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004110
Willy Tarreau0b748332014-04-29 00:13:29 +02004111 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004112 goto done;
4113
Willy Tarreau0b748332014-04-29 00:13:29 +02004114 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4115 goto return_bad_req;
4116 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004117 }
4118
Willy Tarreau52542592014-04-28 18:33:26 +02004119 /* OK at this stage, we know that the request was accepted according to
4120 * the http-request rules, we can check for the stats. Note that the
4121 * URI is detected *before* the req* rules in order not to be affected
4122 * by a possible reqrep, while they are processed *after* so that a
4123 * reqdeny can still block them. This clearly needs to change in 1.6!
4124 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004125 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004126 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004127 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004128 txn->status = 500;
4129 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004130 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004131
Willy Tarreau52542592014-04-28 18:33:26 +02004132 if (!(s->flags & SN_ERR_MASK))
4133 s->flags |= SN_ERR_RESOURCE;
4134 goto return_prx_cond;
4135 }
4136
4137 /* parse the whole stats request and extract the relevant information */
4138 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004139 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4140 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004141
Willy Tarreau0b748332014-04-29 00:13:29 +02004142 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4143 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004144
Willy Tarreau0b748332014-04-29 00:13:29 +02004145 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4146 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004147 }
4148
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004149 /* evaluate the req* rules except reqadd */
4150 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004151 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004152 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004153
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004154 if (txn->flags & TX_CLDENY)
4155 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004156
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 if (txn->flags & TX_CLTARPIT)
4158 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004159 }
Willy Tarreau06619262006-12-17 08:37:22 +01004160
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004161 /* add request headers from the rule sets in the same order */
4162 list_for_each_entry(wl, &px->req_add, list) {
4163 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004164 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004165 ret = acl_pass(ret);
4166 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4167 ret = !ret;
4168 if (!ret)
4169 continue;
4170 }
4171
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004172 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004173 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004174 }
4175
Willy Tarreau52542592014-04-28 18:33:26 +02004176
4177 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004178 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004179 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004180 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4181 s->fe->fe_counters.intercepted_req++;
4182
4183 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4184 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4185 if (!(s->flags & SN_FINST_MASK))
4186 s->flags |= SN_FINST_R;
4187
Willy Tarreau70730dd2014-04-24 18:06:27 +02004188 /* we may want to compress the stats page */
4189 if (s->fe->comp || s->be->comp)
4190 select_compression_request_header(s, req->buf);
4191
4192 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004193 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004194 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004195 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004196
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004197 /* check whether we have some ACLs set to redirect this request */
4198 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004199 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004200 int ret;
4201
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004202 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004203 ret = acl_pass(ret);
4204 if (rule->cond->pol == ACL_COND_UNLESS)
4205 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 if (!ret)
4207 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004208 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004209 if (!http_apply_redirect_rule(rule, s, txn))
4210 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004211 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004212 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004213
Willy Tarreau2be39392010-01-03 17:24:51 +01004214 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4215 * If this happens, then the data will not come immediately, so we must
4216 * send all what we have without waiting. Note that due to the small gain
4217 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004218 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004219 * itself once used.
4220 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004221 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004222
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004223 done: /* done with this analyser, continue with next ones that the calling
4224 * points will have set, if any.
4225 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004226 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004227 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4228 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004229 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004230
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004231 tarpit:
4232 /* When a connection is tarpitted, we use the tarpit timeout,
4233 * which may be the same as the connect timeout if unspecified.
4234 * If unset, then set it to zero because we really want it to
4235 * eventually expire. We build the tarpit as an analyser.
4236 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004237 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004238
4239 /* wipe the request out so that we can drop the connection early
4240 * if the client closes first.
4241 */
4242 channel_dont_connect(req);
4243 req->analysers = 0; /* remove switching rules etc... */
4244 req->analysers |= AN_REQ_HTTP_TARPIT;
4245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4246 if (!req->analyse_exp)
4247 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004248 stream_inc_http_err_ctr(s);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004249 s->fe->fe_counters.denied_req++;
4250 if (s->fe != s->be)
4251 s->be->be_counters.denied_req++;
4252 if (s->listener->counters)
4253 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004254 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004255
4256 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004257 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004258 txn->status = 403;
4259 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004260 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreau87b09662015-04-03 00:22:06 +02004261 stream_inc_http_err_ctr(s);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004262 s->fe->fe_counters.denied_req++;
4263 if (s->fe != s->be)
4264 s->be->be_counters.denied_req++;
4265 if (s->listener->counters)
4266 s->listener->counters->denied_req++;
4267 goto return_prx_cond;
4268
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004269 return_bad_req:
4270 /* We centralize bad requests processing here */
4271 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4272 /* we detected a parsing error. We want to archive this request
4273 * in the dedicated proxy area for later troubleshooting.
4274 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004275 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004276 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004277
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004278 txn->req.msg_state = HTTP_MSG_ERROR;
4279 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004280 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004281
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004282 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004283 if (s->listener->counters)
4284 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004285
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004286 return_prx_cond:
4287 if (!(s->flags & SN_ERR_MASK))
4288 s->flags |= SN_ERR_PRXCOND;
4289 if (!(s->flags & SN_FINST_MASK))
4290 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 req->analysers = 0;
4293 req->analyse_exp = TICK_ETERNITY;
4294 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004295
4296 return_prx_yield:
4297 channel_dont_connect(req);
4298 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004299}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004300
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004301/* This function performs all the processing enabled for the current request.
4302 * It returns 1 if the processing can continue on next analysers, or zero if it
4303 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004304 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004305 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004306int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307{
4308 struct http_txn *txn = &s->txn;
4309 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004310 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004311
Willy Tarreau655dce92009-11-08 13:10:58 +01004312 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004313 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004314 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004315 return 0;
4316 }
4317
Willy Tarreau87b09662015-04-03 00:22:06 +02004318 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004319 now_ms, __FUNCTION__,
4320 s,
4321 req,
4322 req->rex, req->wex,
4323 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004324 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004325 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004326
William Lallemand82fe75c2012-10-23 10:25:10 +02004327 if (s->fe->comp || s->be->comp)
4328 select_compression_request_header(s, req->buf);
4329
Willy Tarreau59234e92008-11-30 23:51:27 +01004330 /*
4331 * Right now, we know that we have processed the entire headers
4332 * and that unwanted requests have been filtered out. We can do
4333 * whatever we want with the remaining request. Also, now we
4334 * may have separate values for ->fe, ->be.
4335 */
Willy Tarreau06619262006-12-17 08:37:22 +01004336
Willy Tarreau59234e92008-11-30 23:51:27 +01004337 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004338 * If HTTP PROXY is set we simply get remote server address parsing
4339 * incoming request. Note that this requires that a connection is
4340 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 */
4342 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004343 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004344 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004345
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004346 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004347 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004348 txn->req.msg_state = HTTP_MSG_ERROR;
4349 txn->status = 500;
4350 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004351 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004352
4353 if (!(s->flags & SN_ERR_MASK))
4354 s->flags |= SN_ERR_RESOURCE;
4355 if (!(s->flags & SN_FINST_MASK))
4356 s->flags |= SN_FINST_R;
4357
4358 return 0;
4359 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004360
4361 path = http_get_path(txn);
4362 url2sa(req->buf->p + msg->sl.rq.u,
4363 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004364 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004365 /* if the path was found, we have to remove everything between
4366 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4367 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4368 * u_l characters by a single "/".
4369 */
4370 if (path) {
4371 char *cur_ptr = req->buf->p;
4372 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4373 int delta;
4374
4375 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4376 http_msg_move_end(&txn->req, delta);
4377 cur_end += delta;
4378 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4379 goto return_bad_req;
4380 }
4381 else {
4382 char *cur_ptr = req->buf->p;
4383 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4384 int delta;
4385
4386 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4387 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4388 http_msg_move_end(&txn->req, delta);
4389 cur_end += delta;
4390 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4391 goto return_bad_req;
4392 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004393 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004394
Willy Tarreau59234e92008-11-30 23:51:27 +01004395 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004396 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004397 * Note that doing so might move headers in the request, but
4398 * the fields will stay coherent and the URI will not move.
4399 * This should only be performed in the backend.
4400 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004401 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4403 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004404
Willy Tarreau59234e92008-11-30 23:51:27 +01004405 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004406 * 8: the appsession cookie was looked up very early in 1.2,
4407 * so let's do the same now.
4408 */
4409
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004410 /* It needs to look into the URI unless persistence must be ignored */
4411 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004412 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 +01004413 }
4414
William Lallemanda73203e2012-03-12 12:48:57 +01004415 /* add unique-id if "header-unique-id" is specified */
4416
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004417 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4418 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4419 goto return_bad_req;
4420 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004421 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004422 }
William Lallemanda73203e2012-03-12 12:48:57 +01004423
4424 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004425 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4426 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004427 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004428 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004429 goto return_bad_req;
4430 }
4431
Cyril Bontéb21570a2009-11-29 20:04:48 +01004432 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004433 * 9: add X-Forwarded-For if either the frontend or the backend
4434 * asks for it.
4435 */
4436 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004437 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004438 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004439 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4440 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004441 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004442 /* The header is set to be added only if none is present
4443 * and we found it, so don't do anything.
4444 */
4445 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004446 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004447 /* Add an X-Forwarded-For header unless the source IP is
4448 * in the 'except' network range.
4449 */
4450 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004451 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004452 != s->fe->except_net.s_addr) &&
4453 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004454 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004455 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004456 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004457 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004458 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004459
4460 /* Note: we rely on the backend to get the header name to be used for
4461 * x-forwarded-for, because the header is really meant for the backends.
4462 * However, if the backend did not specify any option, we have to rely
4463 * on the frontend's header name.
4464 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004465 if (s->be->fwdfor_hdr_len) {
4466 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004468 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004470 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004471 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004472 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 +01004473
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004474 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004475 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004476 }
4477 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004478 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004479 /* FIXME: for the sake of completeness, we should also support
4480 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004481 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004482 int len;
4483 char pn[INET6_ADDRSTRLEN];
4484 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004485 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004486 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004487
Willy Tarreau59234e92008-11-30 23:51:27 +01004488 /* Note: we rely on the backend to get the header name to be used for
4489 * x-forwarded-for, because the header is really meant for the backends.
4490 * However, if the backend did not specify any option, we have to rely
4491 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004492 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 if (s->be->fwdfor_hdr_len) {
4494 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004495 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004496 } else {
4497 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004498 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004499 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004500 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004501
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004502 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004503 goto return_bad_req;
4504 }
4505 }
4506
4507 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004508 * 10: add X-Original-To if either the frontend or the backend
4509 * asks for it.
4510 */
4511 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4512
4513 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004514 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004515 /* Add an X-Original-To header unless the destination IP is
4516 * in the 'except' network range.
4517 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004518 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004519
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004520 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004521 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004522 (((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 +02004523 != s->fe->except_to.s_addr) &&
4524 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004525 (((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 +02004526 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004527 int len;
4528 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004529 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004530
4531 /* Note: we rely on the backend to get the header name to be used for
4532 * x-original-to, 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.
4535 */
4536 if (s->be->orgto_hdr_len) {
4537 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004538 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004539 } else {
4540 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004541 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004542 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004543 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 +02004544
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004546 goto return_bad_req;
4547 }
4548 }
4549 }
4550
Willy Tarreau50fc7772012-11-11 22:19:57 +01004551 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4552 * If an "Upgrade" token is found, the header is left untouched in order not to have
4553 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4554 * "Upgrade" is present in the Connection header.
4555 */
4556 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4557 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004558 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4559 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004560 unsigned int want_flags = 0;
4561
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004562 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004563 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004564 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4565 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004566 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004567 want_flags |= TX_CON_CLO_SET;
4568 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004569 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004570 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4571 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004572 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004573 want_flags |= TX_CON_KAL_SET;
4574 }
4575
4576 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004577 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004578 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004579
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004580
Willy Tarreau522d6c02009-12-06 18:49:18 +01004581 /* If we have no server assigned yet and we're balancing on url_param
4582 * with a POST request, we may be interested in checking the body for
4583 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004584 */
4585 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4586 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004587 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004588 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004589 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004590 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004591
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004592 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004593 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004594#ifdef TCP_QUICKACK
4595 /* We expect some data from the client. Unless we know for sure
4596 * we already have a full request, we have to re-enable quick-ack
4597 * in case we previously disabled it, otherwise we might cause
4598 * the client to delay further data.
4599 */
4600 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004601 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004602 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004603 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004604 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004605#endif
4606 }
Willy Tarreau03945942009-12-22 16:50:27 +01004607
Willy Tarreau59234e92008-11-30 23:51:27 +01004608 /*************************************************************
4609 * OK, that's finished for the headers. We have done what we *
4610 * could. Let's switch to the DATA state. *
4611 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004612 req->analyse_exp = TICK_ETERNITY;
4613 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004614
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004615 /* if the server closes the connection, we want to immediately react
4616 * and close the socket to save packets and syscalls.
4617 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004618 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004619 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004620
Willy Tarreau59234e92008-11-30 23:51:27 +01004621 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004622 /* OK let's go on with the BODY now */
4623 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004624
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004626 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004627 /* we detected a parsing error. We want to archive this request
4628 * in the dedicated proxy area for later troubleshooting.
4629 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004630 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004631 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004632
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 txn->req.msg_state = HTTP_MSG_ERROR;
4634 txn->status = 400;
4635 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004636 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004637
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004638 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004639 if (s->listener->counters)
4640 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004641
Willy Tarreau59234e92008-11-30 23:51:27 +01004642 if (!(s->flags & SN_ERR_MASK))
4643 s->flags |= SN_ERR_PRXCOND;
4644 if (!(s->flags & SN_FINST_MASK))
4645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004646 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004647}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004648
Willy Tarreau60b85b02008-11-30 23:28:40 +01004649/* This function is an analyser which processes the HTTP tarpit. It always
4650 * returns zero, at the beginning because it prevents any other processing
4651 * from occurring, and at the end because it terminates the request.
4652 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004653int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004654{
4655 struct http_txn *txn = &s->txn;
4656
4657 /* This connection is being tarpitted. The CLIENT side has
4658 * already set the connect expiration date to the right
4659 * timeout. We just have to check that the client is still
4660 * there and that the timeout has not expired.
4661 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004662 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004663 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004664 !tick_is_expired(req->analyse_exp, now_ms))
4665 return 0;
4666
4667 /* We will set the queue timer to the time spent, just for
4668 * logging purposes. We fake a 500 server error, so that the
4669 * attacker will not suspect his connection has been tarpitted.
4670 * It will not cause trouble to the logs because we can exclude
4671 * the tarpitted connections by filtering on the 'PT' status flags.
4672 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004673 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4674
4675 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004676 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004677 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004678
4679 req->analysers = 0;
4680 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004681
Willy Tarreau60b85b02008-11-30 23:28:40 +01004682 if (!(s->flags & SN_ERR_MASK))
4683 s->flags |= SN_ERR_PRXCOND;
4684 if (!(s->flags & SN_FINST_MASK))
4685 s->flags |= SN_FINST_T;
4686 return 0;
4687}
4688
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004689/* This function is an analyser which waits for the HTTP request body. It waits
4690 * for either the buffer to be full, or the full advertised contents to have
4691 * reached the buffer. It must only be called after the standard HTTP request
4692 * processing has occurred, because it expects the request to be parsed and will
4693 * look for the Expect header. It may send a 100-Continue interim response. It
4694 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4695 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4696 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004697 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004698int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004699{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004700 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004701 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004702
4703 /* We have to parse the HTTP request body to find any required data.
4704 * "balance url_param check_post" should have been the only way to get
4705 * into this. We were brought here after HTTP header analysis, so all
4706 * related structures are ready.
4707 */
4708
Willy Tarreau890988f2014-04-10 11:59:33 +02004709 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4710 /* This is the first call */
4711 if (msg->msg_state < HTTP_MSG_BODY)
4712 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004713
Willy Tarreau890988f2014-04-10 11:59:33 +02004714 if (msg->msg_state < HTTP_MSG_100_SENT) {
4715 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4716 * send an HTTP/1.1 100 Continue intermediate response.
4717 */
4718 if (msg->flags & HTTP_MSGF_VER_11) {
4719 struct hdr_ctx ctx;
4720 ctx.idx = 0;
4721 /* Expect is allowed in 1.1, look for it */
4722 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4723 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004724 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004725 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004726 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004727 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004728 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004729
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004730 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004731 * req->buf->p still points to the beginning of the message. We
4732 * must save the body in msg->next because it survives buffer
4733 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004734 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004735 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004736
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004737 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004738 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4739 else
4740 msg->msg_state = HTTP_MSG_DATA;
4741 }
4742
Willy Tarreau890988f2014-04-10 11:59:33 +02004743 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4744 /* We're in content-length mode, we just have to wait for enough data. */
4745 if (req->buf->i - msg->sov < msg->body_len)
4746 goto missing_data;
4747
4748 /* OK we have everything we need now */
4749 goto http_end;
4750 }
4751
4752 /* OK here we're parsing a chunked-encoded message */
4753
Willy Tarreau522d6c02009-12-06 18:49:18 +01004754 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004755 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004756 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004757 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004758 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004759 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004760
Willy Tarreau115acb92009-12-26 13:56:06 +01004761 if (!ret)
4762 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004763 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004764 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004765 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004766 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004767 }
4768
Willy Tarreaud98cf932009-12-27 22:54:55 +01004769 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004770 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004771 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004772 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004773 if (msg->msg_state == HTTP_MSG_TRAILERS)
4774 goto http_end;
4775
Willy Tarreau226071e2014-04-10 11:55:45 +02004776 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004777 goto http_end;
4778
4779 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004780 /* we get here if we need to wait for more data. If the buffer is full,
4781 * we have the maximum we can expect.
4782 */
4783 if (buffer_full(req->buf, global.tune.maxrewrite))
4784 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004785
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004786 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004787 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004788 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004789
4790 if (!(s->flags & SN_ERR_MASK))
4791 s->flags |= SN_ERR_CLITO;
4792 if (!(s->flags & SN_FINST_MASK))
4793 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004794 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004795 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004796
4797 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004798 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004799 /* Not enough data. We'll re-use the http-request
4800 * timeout here. Ideally, we should set the timeout
4801 * relative to the accept() date. We just set the
4802 * request timeout once at the beginning of the
4803 * request.
4804 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004805 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004806 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004807 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004808 return 0;
4809 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004810
4811 http_end:
4812 /* The situation will not evolve, so let's give up on the analysis. */
4813 s->logs.tv_request = now; /* update the request timer to reflect full request */
4814 req->analysers &= ~an_bit;
4815 req->analyse_exp = TICK_ETERNITY;
4816 return 1;
4817
4818 return_bad_req: /* let's centralize all bad requests */
4819 txn->req.msg_state = HTTP_MSG_ERROR;
4820 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004821 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004822
Willy Tarreau79ebac62010-06-07 13:47:49 +02004823 if (!(s->flags & SN_ERR_MASK))
4824 s->flags |= SN_ERR_PRXCOND;
4825 if (!(s->flags & SN_FINST_MASK))
4826 s->flags |= SN_FINST_R;
4827
Willy Tarreau522d6c02009-12-06 18:49:18 +01004828 return_err_msg:
4829 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004830 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004831 if (s->listener->counters)
4832 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004833 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004834}
4835
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004836/* send a server's name with an outgoing request over an established connection.
4837 * Note: this function is designed to be called once the request has been scheduled
4838 * for being forwarded. This is the reason why it rewinds the buffer before
4839 * proceeding.
4840 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004841int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004842
4843 struct hdr_ctx ctx;
4844
Mark Lamourinec2247f02012-01-04 13:02:01 -05004845 char *hdr_name = be->server_id_hdr_name;
4846 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004847 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004848 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004849 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004850
William Lallemandd9e90662012-01-30 17:27:17 +01004851 ctx.idx = 0;
4852
Willy Tarreau211cdec2014-04-17 20:18:08 +02004853 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004854 if (old_o) {
4855 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004856 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004857 txn->req.next += old_o;
4858 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004859 }
4860
Willy Tarreau9b28e032012-10-12 23:49:43 +02004861 old_i = chn->buf->i;
4862 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 -05004863 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004864 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004865 }
4866
4867 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004868 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004869 memcpy(hdr_val, hdr_name, hdr_name_len);
4870 hdr_val += hdr_name_len;
4871 *hdr_val++ = ':';
4872 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004873 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4874 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004875
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004876 if (old_o) {
4877 /* If this was a forwarded request, we must readjust the amount of
4878 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004879 * variations. Note that the current state is >= HTTP_MSG_BODY,
4880 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004881 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004882 old_o += chn->buf->i - old_i;
4883 b_adv(chn->buf, old_o);
4884 txn->req.next -= old_o;
4885 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004886 }
4887
Mark Lamourinec2247f02012-01-04 13:02:01 -05004888 return 0;
4889}
4890
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891/* Terminate current transaction and prepare a new one. This is very tricky
4892 * right now but it works.
4893 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004894void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895{
Willy Tarreau068621e2013-12-23 15:11:25 +01004896 int prev_status = s->txn.status;
4897
Willy Tarreau610ecce2010-01-04 21:15:02 +01004898 /* FIXME: We need a more portable way of releasing a backend's and a
4899 * server's connections. We need a safer way to reinitialize buffer
4900 * flags. We also need a more accurate method for computing per-request
4901 * data.
4902 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903
Willy Tarreau4213a112013-12-15 10:25:42 +01004904 /* unless we're doing keep-alive, we want to quickly close the connection
4905 * to the server.
4906 */
4907 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004908 !si_conn_ready(&s->si[1])) {
4909 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4910 si_shutr(&s->si[1]);
4911 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004912 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004913
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004914 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004915 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004916 if (unlikely(s->srv_conn))
4917 sess_change_server(s, NULL);
4918 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919
4920 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004921 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922
4923 if (s->txn.status) {
4924 int n;
4925
4926 n = s->txn.status / 100;
4927 if (n < 1 || n > 5)
4928 n = 0;
4929
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004930 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004931 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004932 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004933 s->fe->fe_counters.p.http.comp_rsp++;
4934 }
Willy Tarreau24657792010-02-26 10:30:28 +01004935 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004936 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004937 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004938 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004939 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004940 s->be->be_counters.p.http.comp_rsp++;
4941 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004942 }
4943
4944 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004945 s->logs.bytes_in -= s->req.buf->i;
4946 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004947
4948 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004949 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 !(s->flags & SN_MONITOR) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004951 (!(s->fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004952 s->do_log(s);
4953 }
4954
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004955 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004956 stream_stop_content_counters(s);
4957 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004958
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 s->logs.accept_date = date; /* user-visible date for logging */
4960 s->logs.tv_accept = now; /* corrected date for internal use */
4961 tv_zero(&s->logs.tv_request);
4962 s->logs.t_queue = -1;
4963 s->logs.t_connect = -1;
4964 s->logs.t_data = -1;
4965 s->logs.t_close = 0;
4966 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4967 s->logs.srv_queue_size = 0; /* we will get this number soon */
4968
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004969 s->logs.bytes_in = s->req.total = s->req.buf->i;
4970 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004971
4972 if (s->pend_pos)
4973 pendconn_free(s->pend_pos);
4974
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004975 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004976 if (s->flags & SN_CURR_SESS) {
4977 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004978 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004979 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004980 if (may_dequeue_tasks(objt_server(s->target), s->be))
4981 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982 }
4983
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004984 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985
Willy Tarreau4213a112013-12-15 10:25:42 +01004986 /* only release our endpoint if we don't intend to reuse the
4987 * connection.
4988 */
4989 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004990 !si_conn_ready(&s->si[1])) {
4991 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004992 }
4993
Willy Tarreau350f4872014-11-28 14:42:25 +01004994 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4995 s->si[1].err_type = SI_ET_NONE;
4996 s->si[1].conn_retries = 0; /* used for logging too */
4997 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004998 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004999 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);
5000 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 +02005001 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 +01005002 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02005003 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005004
Willy Tarreau610ecce2010-01-04 21:15:02 +01005005 s->txn.meth = 0;
5006 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005007 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005008
5009 if (prev_status == 401 || prev_status == 407) {
5010 /* In HTTP keep-alive mode, if we receive a 401, we still have
5011 * a chance of being able to send the visitor again to the same
5012 * server over the same connection. This is required by some
5013 * broken protocols such as NTLM, and anyway whenever there is
5014 * an opportunity for sending the challenge to the proper place,
5015 * it's better to do it (at least it helps with debugging).
5016 */
5017 s->txn.flags |= TX_PREFER_LAST;
5018 }
5019
Willy Tarreauee55dc02010-06-01 10:56:34 +02005020 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005021 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022
Willy Tarreau96e31212011-05-30 18:10:30 +02005023 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005024 s->req.flags |= CF_NEVER_WAIT;
5025 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005026 }
5027
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 /* if the request buffer is not empty, it means we're
5029 * about to process another request, so send pending
5030 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005031 * Just don't do this if the buffer is close to be full,
5032 * because the request will wait for it to flush a little
5033 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005034 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005035 if (s->req.buf->i) {
5036 if (s->res.buf->o &&
5037 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5038 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5039 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005040 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005041
5042 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005043 channel_auto_read(&s->req);
5044 channel_auto_close(&s->req);
5045 channel_auto_read(&s->res);
5046 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005047
Willy Tarreau27375622013-12-17 00:00:28 +01005048 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005049 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005050
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005051 s->req.analysers = s->listener->analysers;
5052 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053}
5054
5055
5056/* This function updates the request state machine according to the response
5057 * state machine and buffer flags. It returns 1 if it changes anything (flag
5058 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5059 * it is only used to find when a request/response couple is complete. Both
5060 * this function and its equivalent should loop until both return zero. It
5061 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5062 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005063int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005065 struct channel *chn = &s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005066 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005067 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 unsigned int old_state = txn->req.msg_state;
5069
Willy Tarreau610ecce2010-01-04 21:15:02 +01005070 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5071 return 0;
5072
5073 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005074 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005075 * We can shut the read side unless we want to abort_on_close,
5076 * or we have a POST request. The issue with POST requests is
5077 * that some browsers still send a CRLF after the request, and
5078 * this CRLF must be read so that it does not remain in the kernel
5079 * buffers, otherwise a close could cause an RST on some systems
5080 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005081 * Note that if we're using keep-alive on the client side, we'd
5082 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005083 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005084 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005085 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005086 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5087 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5088 !(s->be->options & PR_O_ABRT_CLOSE) &&
5089 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005090 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005091
Willy Tarreau40f151a2012-12-20 12:10:09 +01005092 /* if the server closes the connection, we want to immediately react
5093 * and close the socket to save packets and syscalls.
5094 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005095 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005096
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5098 goto wait_other_side;
5099
5100 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5101 /* The server has not finished to respond, so we
5102 * don't want to move in order not to upset it.
5103 */
5104 goto wait_other_side;
5105 }
5106
5107 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5108 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005109 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005111 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005112 goto wait_other_side;
5113 }
5114
5115 /* When we get here, it means that both the request and the
5116 * response have finished receiving. Depending on the connection
5117 * mode, we'll have to wait for the last bytes to leave in either
5118 * direction, and sometimes for a close to be effective.
5119 */
5120
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005121 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5122 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005123 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5124 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005125 }
5126 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5127 /* Option forceclose is set, or either side wants to close,
5128 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005129 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005130 * once both states are CLOSED.
5131 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005132 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5133 channel_shutr_now(chn);
5134 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005136 }
5137 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005138 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5139 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005140 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005141 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5142 channel_auto_read(chn);
5143 txn->req.msg_state = HTTP_MSG_TUNNEL;
5144 chn->flags |= CF_NEVER_WAIT;
5145 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146 }
5147
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005148 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005150 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005151
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005152 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 txn->req.msg_state = HTTP_MSG_CLOSING;
5154 goto http_msg_closing;
5155 }
5156 else {
5157 txn->req.msg_state = HTTP_MSG_CLOSED;
5158 goto http_msg_closed;
5159 }
5160 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005161 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005162 }
5163
5164 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5165 http_msg_closing:
5166 /* nothing else to forward, just waiting for the output buffer
5167 * to be empty and for the shutw_now to take effect.
5168 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005169 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005170 txn->req.msg_state = HTTP_MSG_CLOSED;
5171 goto http_msg_closed;
5172 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005173 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 txn->req.msg_state = HTTP_MSG_ERROR;
5175 goto wait_other_side;
5176 }
5177 }
5178
5179 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5180 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005181 /* see above in MSG_DONE why we only do this in these states */
5182 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5183 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5184 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005185 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 goto wait_other_side;
5187 }
5188
5189 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005190 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191}
5192
5193
5194/* This function updates the response state machine according to the request
5195 * state machine and buffer flags. It returns 1 if it changes anything (flag
5196 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5197 * it is only used to find when a request/response couple is complete. Both
5198 * this function and its equivalent should loop until both return zero. It
5199 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005201int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005203 struct channel *chn = &s->res;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005205 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 unsigned int old_state = txn->rsp.msg_state;
5207
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5209 return 0;
5210
5211 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5212 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005213 * still monitor the server connection for a possible close
5214 * while the request is being uploaded, so we don't disable
5215 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005217 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005218
5219 if (txn->req.msg_state == HTTP_MSG_ERROR)
5220 goto wait_other_side;
5221
5222 if (txn->req.msg_state < HTTP_MSG_DONE) {
5223 /* The client seems to still be sending data, probably
5224 * because we got an error response during an upload.
5225 * We have the choice of either breaking the connection
5226 * or letting it pass through. Let's do the later.
5227 */
5228 goto wait_other_side;
5229 }
5230
5231 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5232 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005233 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005235 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236 goto wait_other_side;
5237 }
5238
5239 /* When we get here, it means that both the request and the
5240 * response have finished receiving. Depending on the connection
5241 * mode, we'll have to wait for the last bytes to leave in either
5242 * direction, and sometimes for a close to be effective.
5243 */
5244
5245 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5246 /* Server-close mode : shut read and wait for the request
5247 * side to close its output buffer. The caller will detect
5248 * when we're in DONE and the other is in CLOSED and will
5249 * catch that for the final cleanup.
5250 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005251 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5252 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005254 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5255 /* Option forceclose is set, or either side wants to close,
5256 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005257 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005258 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005260 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5261 channel_shutr_now(chn);
5262 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005263 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005264 }
5265 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005266 /* The last possible modes are keep-alive and tunnel. Tunnel will
5267 * need to forward remaining data. Keep-alive will need to monitor
5268 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005269 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005270 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005271 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005272 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5273 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 }
5275
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005278 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5280 goto http_msg_closing;
5281 }
5282 else {
5283 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5284 goto http_msg_closed;
5285 }
5286 }
5287 goto wait_other_side;
5288 }
5289
5290 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5291 http_msg_closing:
5292 /* nothing else to forward, just waiting for the output buffer
5293 * to be empty and for the shutw_now to take effect.
5294 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005295 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5297 goto http_msg_closed;
5298 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005299 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005301 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005302 if (objt_server(s->target))
5303 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 goto wait_other_side;
5305 }
5306 }
5307
5308 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5309 http_msg_closed:
5310 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005311 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005312 channel_auto_close(chn);
5313 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 goto wait_other_side;
5315 }
5316
5317 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005318 /* We force the response to leave immediately if we're waiting for the
5319 * other side, since there is no pending shutdown to push it out.
5320 */
5321 if (!channel_is_empty(chn))
5322 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005323 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324}
5325
5326
5327/* Resync the request and response state machines. Return 1 if either state
5328 * changes.
5329 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005330int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331{
5332 struct http_txn *txn = &s->txn;
5333 int old_req_state = txn->req.msg_state;
5334 int old_res_state = txn->rsp.msg_state;
5335
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 http_sync_req_state(s);
5337 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 if (!http_sync_res_state(s))
5339 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 if (!http_sync_req_state(s))
5341 break;
5342 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005343
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 /* OK, both state machines agree on a compatible state.
5345 * There are a few cases we're interested in :
5346 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5347 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5348 * directions, so let's simply disable both analysers.
5349 * - HTTP_MSG_CLOSED on the response only means we must abort the
5350 * request.
5351 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5352 * with server-close mode means we've completed one request and we
5353 * must re-initialize the server connection.
5354 */
5355
5356 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5357 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5358 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5359 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005360 s->req.analysers = 0;
5361 channel_auto_close(&s->req);
5362 channel_auto_read(&s->req);
5363 s->res.analysers = 0;
5364 channel_auto_close(&s->res);
5365 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005367 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005368 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005369 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005370 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005371 s->res.analysers = 0;
5372 channel_auto_close(&s->res);
5373 channel_auto_read(&s->res);
5374 s->req.analysers = 0;
5375 channel_abort(&s->req);
5376 channel_auto_close(&s->req);
5377 channel_auto_read(&s->req);
5378 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005380 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5381 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005383 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5384 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5385 /* server-close/keep-alive: terminate this transaction,
5386 * possibly killing the server connection and reinitialize
5387 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 */
5389 http_end_txn_clean_session(s);
5390 }
5391
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 return txn->req.msg_state != old_req_state ||
5393 txn->rsp.msg_state != old_res_state;
5394}
5395
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396/* This function is an analyser which forwards request body (including chunk
5397 * sizes if any). It is called as soon as we must forward, even if we forward
5398 * zero byte. The only situation where it must not be called is when we're in
5399 * tunnel mode and we want to forward till the close. It's used both to forward
5400 * remaining data and to resync after end of body. It expects the msg_state to
5401 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005402 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005403 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005404 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005406int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407{
5408 struct http_txn *txn = &s->txn;
5409 struct http_msg *msg = &s->txn.req;
5410
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005411 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5412 return 0;
5413
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005414 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005415 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005416 /* Output closed while we were sending data. We must abort and
5417 * wake the other side up.
5418 */
5419 msg->msg_state = HTTP_MSG_ERROR;
5420 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005421 return 1;
5422 }
5423
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424 /* Note that we don't have to send 100-continue back because we don't
5425 * need the data to complete our job, and it's up to the server to
5426 * decide whether to return 100, 417 or anything else in return of
5427 * an "Expect: 100-continue" header.
5428 */
5429
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005430 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005431 /* we have msg->sov which points to the first byte of message
5432 * body, and req->buf.p still points to the beginning of the
5433 * message. We forward the headers now, as we don't need them
5434 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005435 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005436 b_adv(req->buf, msg->sov);
5437 msg->next -= msg->sov;
5438 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005439
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005440 /* The previous analysers guarantee that the state is somewhere
5441 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5442 * msg->next are always correct.
5443 */
5444 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5445 if (msg->flags & HTTP_MSGF_TE_CHNK)
5446 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5447 else
5448 msg->msg_state = HTTP_MSG_DATA;
5449 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 }
5451
Willy Tarreau7ba23542014-04-17 21:50:00 +02005452 /* Some post-connect processing might want us to refrain from starting to
5453 * forward data. Currently, the only reason for this is "balance url_param"
5454 * whichs need to parse/process the request after we've enabled forwarding.
5455 */
5456 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005457 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005458 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005459 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005460 goto missing_data;
5461 }
5462 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5463 }
5464
Willy Tarreau80a92c02014-03-12 10:41:13 +01005465 /* in most states, we should abort in case of early close */
5466 channel_auto_close(req);
5467
Willy Tarreauefdf0942014-04-24 20:08:57 +02005468 if (req->to_forward) {
5469 /* We can't process the buffer's contents yet */
5470 req->flags |= CF_WAKE_WRITE;
5471 goto missing_data;
5472 }
5473
Willy Tarreaud98cf932009-12-27 22:54:55 +01005474 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005475 if (msg->msg_state == HTTP_MSG_DATA) {
5476 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005477 /* we may have some pending data starting at req->buf->p */
5478 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005479 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005480 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005481 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005482 msg->next += msg->chunk_len;
5483 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005484
5485 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005486 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005487 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005489 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005490 }
5491 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005492 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005493 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 * TRAILERS state.
5495 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005496 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497
Willy Tarreau54d23df2012-10-25 19:04:45 +02005498 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005499 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005500 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005501 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005502 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005503 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005505 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005506 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005508 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005510 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511
5512 if (ret == 0)
5513 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005514 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005515 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005516 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005517 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005519 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520 /* we're in MSG_CHUNK_SIZE now */
5521 }
5522 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005523 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524
5525 if (ret == 0)
5526 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005527 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005528 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005529 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005530 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005532 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 /* we're in HTTP_MSG_DONE now */
5534 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005536 int old_state = msg->msg_state;
5537
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005539
5540 /* we may have some pending data starting at req->buf->p
5541 * such as last chunk of data or trailers.
5542 */
5543 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005544 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005545 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005546 msg->next = 0;
5547
Willy Tarreau4fe41902010-06-07 22:27:41 +02005548 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005549 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5550 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005551 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 if (http_resync_states(s)) {
5553 /* some state changes occurred, maybe the analyser
5554 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005555 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005556 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005557 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005558 /* request errors are most likely due to
5559 * the server aborting the transfer.
5560 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005561 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005562 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005563 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005564 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005565 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005566 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005567 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005568 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005569
5570 /* If "option abortonclose" is set on the backend, we
5571 * want to monitor the client's connection and forward
5572 * any shutdown notification to the server, which will
5573 * decide whether to close or to go on processing the
5574 * request.
5575 */
5576 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005577 channel_auto_read(req);
5578 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005579 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005580 else if (s->txn.meth == HTTP_METH_POST) {
5581 /* POST requests may require to read extra CRLF
5582 * sent by broken browsers and which could cause
5583 * an RST to be sent upon close on some systems
5584 * (eg: Linux).
5585 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005586 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005587 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005588
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005590 }
5591 }
5592
Willy Tarreaud98cf932009-12-27 22:54:55 +01005593 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005594 /* we may have some pending data starting at req->buf->p */
5595 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005597 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5598
Willy Tarreaubed410e2014-04-22 08:19:34 +02005599 msg->next = 0;
5600 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5601
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005602 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005603 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005604 if (!(s->flags & SN_ERR_MASK))
5605 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005606 if (!(s->flags & SN_FINST_MASK)) {
5607 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5608 s->flags |= SN_FINST_H;
5609 else
5610 s->flags |= SN_FINST_D;
5611 }
5612
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005613 s->fe->fe_counters.cli_aborts++;
5614 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005615 if (objt_server(s->target))
5616 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005617
5618 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005619 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005620
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005621 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005622 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005624
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005625 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005626 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005627 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005628 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005629 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005630
Willy Tarreau5c620922011-05-11 19:56:11 +02005631 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005632 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005633 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005634 * modes are already handled by the stream sock layer. We must not do
5635 * this in content-length mode because it could present the MSG_MORE
5636 * flag with the last block of forwarded data, which would cause an
5637 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005638 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005639 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005640 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005641
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005642 return 0;
5643
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005645 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646 if (s->listener->counters)
5647 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005648
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005649 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005650 /* we may have some pending data starting at req->buf->p */
5651 b_adv(req->buf, msg->next);
5652 msg->next = 0;
5653
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005654 txn->req.msg_state = HTTP_MSG_ERROR;
5655 if (txn->status) {
5656 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005657 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005658 } else {
5659 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005660 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005661 }
5662 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005663 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005664
5665 if (!(s->flags & SN_ERR_MASK))
5666 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005667 if (!(s->flags & SN_FINST_MASK)) {
5668 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5669 s->flags |= SN_FINST_H;
5670 else
5671 s->flags |= SN_FINST_D;
5672 }
5673 return 0;
5674
5675 aborted_xfer:
5676 txn->req.msg_state = HTTP_MSG_ERROR;
5677 if (txn->status) {
5678 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005679 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005680 } else {
5681 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005682 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005683 }
5684 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005685 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005686
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005687 s->fe->fe_counters.srv_aborts++;
5688 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005689 if (objt_server(s->target))
5690 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005691
5692 if (!(s->flags & SN_ERR_MASK))
5693 s->flags |= SN_ERR_SRVCL;
5694 if (!(s->flags & SN_FINST_MASK)) {
5695 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5696 s->flags |= SN_FINST_H;
5697 else
5698 s->flags |= SN_FINST_D;
5699 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005700 return 0;
5701}
5702
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005703/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5704 * processing can continue on next analysers, or zero if it either needs more
5705 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005706 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707 * when it has nothing left to do, and may remove any analyser when it wants to
5708 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005709 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005710int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005711{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005712 struct http_txn *txn = &s->txn;
5713 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005714 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005715 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005716 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005717 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005718
Willy Tarreau87b09662015-04-03 00:22:06 +02005719 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005720 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005721 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005722 rep,
5723 rep->rex, rep->wex,
5724 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005725 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005726 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005728 /*
5729 * Now parse the partial (or complete) lines.
5730 * We will check the response syntax, and also join multi-line
5731 * headers. An index of all the lines will be elaborated while
5732 * parsing.
5733 *
5734 * For the parsing, we use a 28 states FSM.
5735 *
5736 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005737 * rep->buf->p = beginning of response
5738 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5739 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005740 * msg->eol = end of current header or line (LF or CRLF)
5741 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742 */
5743
Willy Tarreau628c40c2014-04-24 19:11:26 +02005744 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005745 /* There's a protected area at the end of the buffer for rewriting
5746 * purposes. We don't want to start to parse the request if the
5747 * protected area is affected, because we may have to move processed
5748 * data later, which is much more complicated.
5749 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005750 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005751 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005752 /* some data has still not left the buffer, wake us once that's done */
5753 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5754 goto abort_response;
5755 channel_dont_close(rep);
5756 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005757 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005758 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005759 }
5760
Willy Tarreau379357a2013-06-08 12:55:46 +02005761 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5762 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5763 buffer_slow_realign(rep->buf);
5764
Willy Tarreau9b28e032012-10-12 23:49:43 +02005765 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005766 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005767 }
5768
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005769 /* 1: we might have to print this header in debug mode */
5770 if (unlikely((global.mode & MODE_DEBUG) &&
5771 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005772 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005774
Willy Tarreau9b28e032012-10-12 23:49:43 +02005775 sol = rep->buf->p;
5776 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005777 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779 sol += hdr_idx_first_pos(&txn->hdr_idx);
5780 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 while (cur_idx) {
5783 eol = sol + txn->hdr_idx.v[cur_idx].len;
5784 debug_hdr("srvhdr", s, sol, eol);
5785 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5786 cur_idx = txn->hdr_idx.v[cur_idx].next;
5787 }
5788 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 /*
5791 * Now we quickly check if we have found a full valid response.
5792 * If not so, we check the FD and buffer states before leaving.
5793 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005794 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 * responses are checked first.
5796 *
5797 * Depending on whether the client is still there or not, we
5798 * may send an error response back or not. Note that normally
5799 * we should only check for HTTP status there, and check I/O
5800 * errors somewhere else.
5801 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005802
Willy Tarreau655dce92009-11-08 13:10:58 +01005803 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005804 /* Invalid response */
5805 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5806 /* we detected a parsing error. We want to archive this response
5807 * in the dedicated proxy area for later troubleshooting.
5808 */
5809 hdr_response_bad:
5810 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005811 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005812
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005813 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005814 if (objt_server(s->target)) {
5815 objt_server(s->target)->counters.failed_resp++;
5816 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005817 }
Willy Tarreau64648412010-03-05 10:41:54 +01005818 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005819 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 rep->analysers = 0;
5821 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005822 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005823 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005824 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005825
5826 if (!(s->flags & SN_ERR_MASK))
5827 s->flags |= SN_ERR_PRXCOND;
5828 if (!(s->flags & SN_FINST_MASK))
5829 s->flags |= SN_FINST_H;
5830
5831 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005832 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005833
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005834 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005835 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005836 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005837 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005838 goto hdr_response_bad;
5839 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005840
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005841 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005842 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005843 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005844 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005845 else if (txn->flags & TX_NOT_FIRST)
5846 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005847
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005848 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005849 if (objt_server(s->target)) {
5850 objt_server(s->target)->counters.failed_resp++;
5851 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005852 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005853
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005854 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005855 rep->analysers = 0;
5856 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005857 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005858 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005859 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005860
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861 if (!(s->flags & SN_ERR_MASK))
5862 s->flags |= SN_ERR_SRVCL;
5863 if (!(s->flags & SN_FINST_MASK))
5864 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005865 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005867
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005868 /* read timeout : return a 504 to the client. */
5869 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005871 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005872 else if (txn->flags & TX_NOT_FIRST)
5873 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005874
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005875 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005876 if (objt_server(s->target)) {
5877 objt_server(s->target)->counters.failed_resp++;
5878 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005879 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005880
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005881 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005882 rep->analysers = 0;
5883 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005884 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005885 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005886 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005887
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 if (!(s->flags & SN_ERR_MASK))
5889 s->flags |= SN_ERR_SRVTO;
5890 if (!(s->flags & SN_FINST_MASK))
5891 s->flags |= SN_FINST_H;
5892 return 0;
5893 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005894
Willy Tarreauf003d372012-11-26 13:35:37 +01005895 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005896 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005897 s->fe->fe_counters.cli_aborts++;
5898 s->be->be_counters.cli_aborts++;
5899 if (objt_server(s->target))
5900 objt_server(s->target)->counters.cli_aborts++;
5901
5902 rep->analysers = 0;
5903 channel_auto_close(rep);
5904
5905 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005906 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005907 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005908
5909 if (!(s->flags & SN_ERR_MASK))
5910 s->flags |= SN_ERR_CLICL;
5911 if (!(s->flags & SN_FINST_MASK))
5912 s->flags |= SN_FINST_H;
5913
Willy Tarreau87b09662015-04-03 00:22:06 +02005914 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005915 return 0;
5916 }
5917
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005918 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005919 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005920 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005921 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005922 else if (txn->flags & TX_NOT_FIRST)
5923 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005924
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005925 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005926 if (objt_server(s->target)) {
5927 objt_server(s->target)->counters.failed_resp++;
5928 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005929 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005930
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005931 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932 rep->analysers = 0;
5933 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005934 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005935 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005936 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 if (!(s->flags & SN_ERR_MASK))
5939 s->flags |= SN_ERR_SRVCL;
5940 if (!(s->flags & SN_FINST_MASK))
5941 s->flags |= SN_FINST_H;
5942 return 0;
5943 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005946 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005947 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005948 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005949 else if (txn->flags & TX_NOT_FIRST)
5950 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005951
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005952 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005954 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955
5956 if (!(s->flags & SN_ERR_MASK))
5957 s->flags |= SN_ERR_CLICL;
5958 if (!(s->flags & SN_FINST_MASK))
5959 s->flags |= SN_FINST_H;
5960
Willy Tarreau87b09662015-04-03 00:22:06 +02005961 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005962 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005963 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005964
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005965 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005966 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005967 return 0;
5968 }
5969
5970 /* More interesting part now : we know that we have a complete
5971 * response which at least looks like HTTP. We have an indicator
5972 * of each header's length, so we can parse them quickly.
5973 */
5974
5975 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005976 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 /*
5979 * 1: get the status code
5980 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005981 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 if (n < 1 || n > 5)
5983 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005984 /* when the client triggers a 4xx from the server, it's most often due
5985 * to a missing object or permission. These events should be tracked
5986 * because if they happen often, it may indicate a brute force or a
5987 * vulnerability scan.
5988 */
5989 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005990 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005991
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005992 if (objt_server(s->target))
5993 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994
Willy Tarreau5b154472009-12-21 20:11:07 +01005995 /* check if the response is HTTP/1.1 or above */
5996 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005997 ((rep->buf->p[5] > '1') ||
5998 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005999 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006000
6001 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006002 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 +01006003
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006004 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006005 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006006
Willy Tarreau9b28e032012-10-12 23:49:43 +02006007 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006008
Willy Tarreau39650402010-03-15 19:44:39 +01006009 /* Adjust server's health based on status code. Note: status codes 501
6010 * and 505 are triggered on demand by client request, so we must not
6011 * count them as server failures.
6012 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006013 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006014 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006015 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006016 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006017 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006018 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006019
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 /*
6021 * 2: check for cacheability.
6022 */
6023
6024 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006025 case 100:
6026 /*
6027 * We may be facing a 100-continue response, in which case this
6028 * is not the right response, and we're waiting for the next one.
6029 * Let's allow this response to go to the client and wait for the
6030 * next one.
6031 */
6032 hdr_idx_init(&txn->hdr_idx);
6033 msg->next -= channel_forward(rep, msg->next);
6034 msg->msg_state = HTTP_MSG_RPBEFORE;
6035 txn->status = 0;
6036 s->logs.t_data = -1; /* was not a response yet */
6037 goto next_one;
6038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006039 case 200:
6040 case 203:
6041 case 206:
6042 case 300:
6043 case 301:
6044 case 410:
6045 /* RFC2616 @13.4:
6046 * "A response received with a status code of
6047 * 200, 203, 206, 300, 301 or 410 MAY be stored
6048 * by a cache (...) unless a cache-control
6049 * directive prohibits caching."
6050 *
6051 * RFC2616 @9.5: POST method :
6052 * "Responses to this method are not cacheable,
6053 * unless the response includes appropriate
6054 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006055 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006057 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006058 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6059 break;
6060 default:
6061 break;
6062 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006063
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064 /*
6065 * 3: we may need to capture headers
6066 */
6067 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006068 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006069 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070 txn->rsp.cap, s->fe->rsp_cap);
6071
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006072 /* 4: determine the transfer-length.
6073 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6074 * the presence of a message-body in a RESPONSE and its transfer length
6075 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006076 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006077 * All responses to the HEAD request method MUST NOT include a
6078 * message-body, even though the presence of entity-header fields
6079 * might lead one to believe they do. All 1xx (informational), 204
6080 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6081 * message-body. All other responses do include a message-body,
6082 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006083 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006084 * 1. Any response which "MUST NOT" include a message-body (such as the
6085 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6086 * always terminated by the first empty line after the header fields,
6087 * regardless of the entity-header fields present in the message.
6088 *
6089 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6090 * the "chunked" transfer-coding (Section 6.2) is used, the
6091 * transfer-length is defined by the use of this transfer-coding.
6092 * If a Transfer-Encoding header field is present and the "chunked"
6093 * transfer-coding is not present, the transfer-length is defined by
6094 * the sender closing the connection.
6095 *
6096 * 3. If a Content-Length header field is present, its decimal value in
6097 * OCTETs represents both the entity-length and the transfer-length.
6098 * If a message is received with both a Transfer-Encoding header
6099 * field and a Content-Length header field, the latter MUST be ignored.
6100 *
6101 * 4. If the message uses the media type "multipart/byteranges", and
6102 * the transfer-length is not otherwise specified, then this self-
6103 * delimiting media type defines the transfer-length. This media
6104 * type MUST NOT be used unless the sender knows that the recipient
6105 * can parse it; the presence in a request of a Range header with
6106 * multiple byte-range specifiers from a 1.1 client implies that the
6107 * client can parse multipart/byteranges responses.
6108 *
6109 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006110 */
6111
6112 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006113 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006114 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006115 * FIXME: should we parse anyway and return an error on chunked encoding ?
6116 */
6117 if (txn->meth == HTTP_METH_HEAD ||
6118 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006119 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006120 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006121 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006122 goto skip_content_length;
6123 }
6124
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006125 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006127 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006128 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006129 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006130 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6131 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006132 /* bad transfer-encoding (chunked followed by something else) */
6133 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006134 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 break;
6136 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006137 }
6138
6139 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6140 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006141 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006142 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006143 signed long long cl;
6144
Willy Tarreauad14f752011-09-02 20:33:27 +02006145 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006146 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006147 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006148 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006149
Willy Tarreauad14f752011-09-02 20:33:27 +02006150 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006151 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006152 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006153 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006154
Willy Tarreauad14f752011-09-02 20:33:27 +02006155 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006156 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006157 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006158 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006159
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006160 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006161 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006162 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006163 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006164
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006165 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006166 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006167 }
6168
William Lallemand82fe75c2012-10-23 10:25:10 +02006169 if (s->fe->comp || s->be->comp)
6170 select_compression_response_header(s, rep->buf);
6171
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006172skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006173 /* Now we have to check if we need to modify the Connection header.
6174 * This is more difficult on the response than it is on the request,
6175 * because we can have two different HTTP versions and we don't know
6176 * how the client will interprete a response. For instance, let's say
6177 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6178 * HTTP/1.1 response without any header. Maybe it will bound itself to
6179 * HTTP/1.0 because it only knows about it, and will consider the lack
6180 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6181 * the lack of header as a keep-alive. Thus we will use two flags
6182 * indicating how a request MAY be understood by the client. In case
6183 * of multiple possibilities, we'll fix the header to be explicit. If
6184 * ambiguous cases such as both close and keepalive are seen, then we
6185 * will fall back to explicit close. Note that we won't take risks with
6186 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006187 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006188 */
6189
Willy Tarreaudc008c52010-02-01 16:20:08 +01006190 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6191 txn->status == 101)) {
6192 /* Either we've established an explicit tunnel, or we're
6193 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006194 * to understand the next protocols. We have to switch to tunnel
6195 * mode, so that we transfer the request and responses then let
6196 * this protocol pass unmodified. When we later implement specific
6197 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006198 * header which contains information about that protocol for
6199 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006200 */
6201 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6202 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006203 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6204 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006205 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6206 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006207 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006208
Willy Tarreau70dffda2014-01-30 03:07:23 +01006209 /* this situation happens when combining pretend-keepalive with httpclose. */
6210 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006211 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6212 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006213 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6214
Willy Tarreau60466522010-01-18 19:08:45 +01006215 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006216 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006217 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6218 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006219
Willy Tarreau60466522010-01-18 19:08:45 +01006220 /* now adjust header transformations depending on current state */
6221 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6222 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6223 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006224 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006225 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006226 }
Willy Tarreau60466522010-01-18 19:08:45 +01006227 else { /* SCL / KAL */
6228 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006229 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006230 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006231 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006232
Willy Tarreau60466522010-01-18 19:08:45 +01006233 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006234 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006235
Willy Tarreau60466522010-01-18 19:08:45 +01006236 /* Some keep-alive responses are converted to Server-close if
6237 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006238 */
Willy Tarreau60466522010-01-18 19:08:45 +01006239 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6240 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006241 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006242 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006243 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006244 }
6245
Willy Tarreau7959a552013-09-23 16:44:27 +02006246 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006247 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006248
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006249 /* end of job, return OK */
6250 rep->analysers &= ~an_bit;
6251 rep->analyse_exp = TICK_ETERNITY;
6252 channel_auto_close(rep);
6253 return 1;
6254
6255 abort_keep_alive:
6256 /* A keep-alive request to the server failed on a network error.
6257 * The client is required to retry. We need to close without returning
6258 * any other information so that the client retries.
6259 */
6260 txn->status = 0;
6261 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006262 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006263 channel_auto_close(rep);
6264 s->logs.logwait = 0;
6265 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006266 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006267 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006268 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006269 return 0;
6270}
6271
6272/* This function performs all the processing enabled for the current response.
6273 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006274 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006275 * other functions. It works like process_request (see indications above).
6276 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006277int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006278{
6279 struct http_txn *txn = &s->txn;
6280 struct http_msg *msg = &txn->rsp;
6281 struct proxy *cur_proxy;
6282 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006283 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006284
Willy Tarreau87b09662015-04-03 00:22:06 +02006285 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006286 now_ms, __FUNCTION__,
6287 s,
6288 rep,
6289 rep->rex, rep->wex,
6290 rep->flags,
6291 rep->buf->i,
6292 rep->analysers);
6293
6294 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6295 return 0;
6296
Willy Tarreau70730dd2014-04-24 18:06:27 +02006297 /* The stats applet needs to adjust the Connection header but we don't
6298 * apply any filter there.
6299 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006300 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6301 rep->analysers &= ~an_bit;
6302 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006303 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006304 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006305
Willy Tarreau58975672014-04-24 21:13:57 +02006306 /*
6307 * We will have to evaluate the filters.
6308 * As opposed to version 1.2, now they will be evaluated in the
6309 * filters order and not in the header order. This means that
6310 * each filter has to be validated among all headers.
6311 *
6312 * Filters are tried with ->be first, then with ->fe if it is
6313 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006314 *
6315 * Maybe we are in resume condiion. In this case I choose the
6316 * "struct proxy" which contains the rule list matching the resume
6317 * pointer. If none of theses "struct proxy" match, I initialise
6318 * the process with the first one.
6319 *
6320 * In fact, I check only correspondance betwwen the current list
6321 * pointer and the ->fe rule list. If it doesn't match, I initialize
6322 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006323 */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006324 if (s->current_rule_list == &s->fe->http_res_rules)
6325 cur_proxy = s->fe;
6326 else
6327 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006328 while (1) {
6329 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006330
Willy Tarreau58975672014-04-24 21:13:57 +02006331 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006332 if (ret == HTTP_RULE_RES_CONT)
6333 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006334
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006335 /* we need to be called again. */
6336 if (ret == HTTP_RULE_RES_YIELD) {
6337 channel_dont_close(rep);
6338 return 0;
6339 }
6340
Willy Tarreau58975672014-04-24 21:13:57 +02006341 /* try headers filters */
6342 if (rule_set->rsp_exp != NULL) {
6343 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6344 return_bad_resp:
6345 if (objt_server(s->target)) {
6346 objt_server(s->target)->counters.failed_resp++;
6347 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006348 }
Willy Tarreau58975672014-04-24 21:13:57 +02006349 s->be->be_counters.failed_resp++;
6350 return_srv_prx_502:
6351 rep->analysers = 0;
6352 txn->status = 502;
6353 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006354 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006355 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006356 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau58975672014-04-24 21:13:57 +02006357 if (!(s->flags & SN_ERR_MASK))
6358 s->flags |= SN_ERR_PRXCOND;
6359 if (!(s->flags & SN_FINST_MASK))
6360 s->flags |= SN_FINST_H;
6361 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006362 }
Willy Tarreau58975672014-04-24 21:13:57 +02006363 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006364
Willy Tarreau58975672014-04-24 21:13:57 +02006365 /* has the response been denied ? */
6366 if (txn->flags & TX_SVDENY) {
6367 if (objt_server(s->target))
6368 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006369
Willy Tarreau58975672014-04-24 21:13:57 +02006370 s->be->be_counters.denied_resp++;
6371 s->fe->fe_counters.denied_resp++;
6372 if (s->listener->counters)
6373 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006374
Willy Tarreau58975672014-04-24 21:13:57 +02006375 goto return_srv_prx_502;
6376 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006377
Willy Tarreau58975672014-04-24 21:13:57 +02006378 /* add response headers from the rule sets in the same order */
6379 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006380 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006381 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006382 if (wl->cond) {
6383 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6384 ret = acl_pass(ret);
6385 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6386 ret = !ret;
6387 if (!ret)
6388 continue;
6389 }
6390 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6391 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006392 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006393
Willy Tarreau58975672014-04-24 21:13:57 +02006394 /* check whether we're already working on the frontend */
6395 if (cur_proxy == s->fe)
6396 break;
6397 cur_proxy = s->fe;
6398 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006399
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006400 /* After this point, this anayzer can't return yield, so we can
6401 * remove the bit corresponding to this analyzer from the list.
6402 *
6403 * Note that the intermediate returns and goto found previously
6404 * reset the analyzers.
6405 */
6406 rep->analysers &= ~an_bit;
6407 rep->analyse_exp = TICK_ETERNITY;
6408
Willy Tarreau58975672014-04-24 21:13:57 +02006409 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006410 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006411 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006412
Willy Tarreau58975672014-04-24 21:13:57 +02006413 /*
6414 * Now check for a server cookie.
6415 */
6416 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6417 (s->be->options & PR_O_CHK_CACHE))
6418 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006419
Willy Tarreau58975672014-04-24 21:13:57 +02006420 /*
6421 * Check for cache-control or pragma headers if required.
6422 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006423 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 +02006424 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006425
Willy Tarreau58975672014-04-24 21:13:57 +02006426 /*
6427 * Add server cookie in the response if needed
6428 */
6429 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6430 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6431 (!(s->flags & SN_DIRECT) ||
6432 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6433 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6434 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6435 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6436 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6437 !(s->flags & SN_IGNORE_PRST)) {
6438 /* the server is known, it's not the one the client requested, or the
6439 * cookie's last seen date needs to be refreshed. We have to
6440 * insert a set-cookie here, except if we want to insert only on POST
6441 * requests and this one isn't. Note that servers which don't have cookies
6442 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006443 */
Willy Tarreau58975672014-04-24 21:13:57 +02006444 if (!objt_server(s->target)->cookie) {
6445 chunk_printf(&trash,
6446 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6447 s->be->cookie_name);
6448 }
6449 else {
6450 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006451
Willy Tarreau58975672014-04-24 21:13:57 +02006452 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6453 /* emit last_date, which is mandatory */
6454 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6455 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6456 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006457
Willy Tarreau58975672014-04-24 21:13:57 +02006458 if (s->be->cookie_maxlife) {
6459 /* emit first_date, which is either the original one or
6460 * the current date.
6461 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006462 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006463 s30tob64(txn->cookie_first_date ?
6464 txn->cookie_first_date >> 2 :
6465 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006467 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006468 }
Willy Tarreau58975672014-04-24 21:13:57 +02006469 chunk_appendf(&trash, "; path=/");
6470 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006471
Willy Tarreau58975672014-04-24 21:13:57 +02006472 if (s->be->cookie_domain)
6473 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006474
Willy Tarreau58975672014-04-24 21:13:57 +02006475 if (s->be->ck_opts & PR_CK_HTTPONLY)
6476 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006477
Willy Tarreau58975672014-04-24 21:13:57 +02006478 if (s->be->ck_opts & PR_CK_SECURE)
6479 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006480
Willy Tarreau58975672014-04-24 21:13:57 +02006481 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6482 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006483
Willy Tarreau58975672014-04-24 21:13:57 +02006484 txn->flags &= ~TX_SCK_MASK;
6485 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6486 /* the server did not change, only the date was updated */
6487 txn->flags |= TX_SCK_UPDATED;
6488 else
6489 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006490
Willy Tarreau58975672014-04-24 21:13:57 +02006491 /* Here, we will tell an eventual cache on the client side that we don't
6492 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6493 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6494 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006495 */
Willy Tarreau58975672014-04-24 21:13:57 +02006496 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006497
Willy Tarreau58975672014-04-24 21:13:57 +02006498 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006499
Willy Tarreau58975672014-04-24 21:13:57 +02006500 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6501 "Cache-control: private", 22) < 0))
6502 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006503 }
Willy Tarreau58975672014-04-24 21:13:57 +02006504 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505
Willy Tarreau58975672014-04-24 21:13:57 +02006506 /*
6507 * Check if result will be cacheable with a cookie.
6508 * We'll block the response if security checks have caught
6509 * nasty things such as a cacheable cookie.
6510 */
6511 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6512 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6513 (s->be->options & PR_O_CHK_CACHE)) {
6514 /* we're in presence of a cacheable response containing
6515 * a set-cookie header. We'll block it as requested by
6516 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 */
Willy Tarreau58975672014-04-24 21:13:57 +02006518 if (objt_server(s->target))
6519 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006520
Willy Tarreau58975672014-04-24 21:13:57 +02006521 s->be->be_counters.denied_resp++;
6522 s->fe->fe_counters.denied_resp++;
6523 if (s->listener->counters)
6524 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525
Willy Tarreau58975672014-04-24 21:13:57 +02006526 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6527 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6528 send_log(s->be, LOG_ALERT,
6529 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6530 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6531 goto return_srv_prx_502;
6532 }
Willy Tarreau03945942009-12-22 16:50:27 +01006533
Willy Tarreau70730dd2014-04-24 18:06:27 +02006534 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006535 /*
6536 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6537 * If an "Upgrade" token is found, the header is left untouched in order
6538 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006539 * if anything but "Upgrade" is present in the Connection header. We don't
6540 * want to touch any 101 response either since it's switching to another
6541 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006542 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006543 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006544 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6545 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6546 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6547 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006548
Willy Tarreau58975672014-04-24 21:13:57 +02006549 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6550 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6551 /* we want a keep-alive response here. Keep-alive header
6552 * required if either side is not 1.1.
6553 */
6554 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6555 want_flags |= TX_CON_KAL_SET;
6556 }
6557 else {
6558 /* we want a close response here. Close header required if
6559 * the server is 1.1, regardless of the client.
6560 */
6561 if (msg->flags & HTTP_MSGF_VER_11)
6562 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006563 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564
Willy Tarreau58975672014-04-24 21:13:57 +02006565 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6566 http_change_connection_header(txn, msg, want_flags);
6567 }
6568
6569 skip_header_mangling:
6570 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6571 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6572 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573
Willy Tarreau58975672014-04-24 21:13:57 +02006574 /* if the user wants to log as soon as possible, without counting
6575 * bytes from the server, then this is the right moment. We have
6576 * to temporarily assign bytes_out to log what we currently have.
6577 */
6578 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6579 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6580 s->logs.bytes_out = txn->rsp.eoh;
6581 s->do_log(s);
6582 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006583 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006584 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006585}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586
Willy Tarreaud98cf932009-12-27 22:54:55 +01006587/* This function is an analyser which forwards response body (including chunk
6588 * sizes if any). It is called as soon as we must forward, even if we forward
6589 * zero byte. The only situation where it must not be called is when we're in
6590 * tunnel mode and we want to forward till the close. It's used both to forward
6591 * remaining data and to resync after end of body. It expects the msg_state to
6592 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006593 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006594 *
6595 * It is capable of compressing response data both in content-length mode and
6596 * in chunked mode. The state machines follows different flows depending on
6597 * whether content-length and chunked modes are used, since there are no
6598 * trailers in content-length :
6599 *
6600 * chk-mode cl-mode
6601 * ,----- BODY -----.
6602 * / \
6603 * V size > 0 V chk-mode
6604 * .--> SIZE -------------> DATA -------------> CRLF
6605 * | | size == 0 | last byte |
6606 * | v final crlf v inspected |
6607 * | TRAILERS -----------> DONE |
6608 * | |
6609 * `----------------------------------------------'
6610 *
6611 * Compression only happens in the DATA state, and must be flushed in final
6612 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6613 * is performed at once on final states for all bytes parsed, or when leaving
6614 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006615 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006616int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006617{
6618 struct http_txn *txn = &s->txn;
6619 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006620 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006621 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006622 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006623
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006624 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6625 return 0;
6626
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006627 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006628 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006629 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006630 /* Output closed while we were sending data. We must abort and
6631 * wake the other side up.
6632 */
6633 msg->msg_state = HTTP_MSG_ERROR;
6634 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006635 return 1;
6636 }
6637
Willy Tarreau4fe41902010-06-07 22:27:41 +02006638 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006639 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006640
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006641 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006642 /* we have msg->sov which points to the first byte of message
6643 * body, and res->buf.p still points to the beginning of the
6644 * message. We forward the headers now, as we don't need them
6645 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006646 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006647 b_adv(res->buf, msg->sov);
6648 msg->next -= msg->sov;
6649 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006650
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006651 /* The previous analysers guarantee that the state is somewhere
6652 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6653 * msg->next are always correct.
6654 */
6655 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6656 if (msg->flags & HTTP_MSGF_TE_CHNK)
6657 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6658 else
6659 msg->msg_state = HTTP_MSG_DATA;
6660 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006661 }
6662
Willy Tarreauefdf0942014-04-24 20:08:57 +02006663 if (res->to_forward) {
6664 /* We can't process the buffer's contents yet */
6665 res->flags |= CF_WAKE_WRITE;
6666 goto missing_data;
6667 }
6668
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006669 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6670 /* We need a compression buffer in the DATA state to put the
6671 * output of compressed data, and in CRLF state to let the
6672 * TRAILERS state finish the job of removing the trailing CRLF.
6673 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006674 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006675 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006676 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006677 goto aborted_xfer; /* no memory */
6678 }
6679
6680 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006681 if (ret < 0) {
6682 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006683 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006684 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006685 compressing = 1;
6686 }
6687
Willy Tarreaud98cf932009-12-27 22:54:55 +01006688 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006689 switch (msg->msg_state - HTTP_MSG_DATA) {
6690 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006691 /* we may have some pending data starting at res->buf->p */
6692 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006693 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006694 if (ret < 0)
6695 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006696
Willy Tarreaud5a67832014-04-21 10:54:27 +02006697 if (msg->chunk_len) {
6698 /* input empty or output full */
6699 if (res->buf->i > msg->next)
6700 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006701 goto missing_data;
6702 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006703 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006704 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006705 if (msg->chunk_len > res->buf->i - msg->next) {
6706 /* output full */
6707 res->flags |= CF_WAKE_WRITE;
6708 goto missing_data;
6709 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006710 msg->next += msg->chunk_len;
6711 msg->chunk_len = 0;
6712 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006713
6714 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006715 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006716 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006717 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006718 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006719 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006720 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006721 /* fall through for HTTP_MSG_CHUNK_CRLF */
6722
6723 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6724 /* we want the CRLF after the data */
6725
6726 ret = http_skip_chunk_crlf(msg);
6727 if (ret == 0)
6728 goto missing_data;
6729 else if (ret < 0) {
6730 if (msg->err_pos >= 0)
6731 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6732 goto return_bad_res;
6733 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006734 /* we're in MSG_CHUNK_SIZE now, fall through */
6735
6736 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006737 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006738 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006739 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006740 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006741
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006742 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006743 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006744 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006745 else if (ret < 0) {
6746 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006747 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006748 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006749 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006750 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006751 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006752
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006753 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006754 if (unlikely(compressing)) {
6755 /* we need to flush output contents before syncing FSMs */
6756 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6757 compressing = 0;
6758 }
6759
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006760 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006761 if (ret == 0)
6762 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006763 else if (ret < 0) {
6764 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006765 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006767 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006768 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006769
6770 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006771 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006772 if (unlikely(compressing)) {
6773 /* we need to flush output contents before syncing FSMs */
6774 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6775 compressing = 0;
6776 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006777
Willy Tarreauc623c172014-04-18 09:53:50 +02006778 /* we may have some pending data starting at res->buf->p
6779 * such as a last chunk of data or trailers.
6780 */
6781 b_adv(res->buf, msg->next);
6782 msg->next = 0;
6783
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006784 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006785 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006786 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6787 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006788 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006789
Willy Tarreau610ecce2010-01-04 21:15:02 +01006790 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006791 /* some state changes occurred, maybe the analyser
6792 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006793 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006794 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006795 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006796 /* response errors are most likely due to
6797 * the client aborting the transfer.
6798 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006799 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006800 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006801 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006802 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006803 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006804 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006805 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006806 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006807 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006808 }
6809 }
6810
Willy Tarreaud98cf932009-12-27 22:54:55 +01006811 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006812 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006813 if (unlikely(compressing)) {
6814 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006815 compressing = 0;
6816 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006817
Willy Tarreauc623c172014-04-18 09:53:50 +02006818 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6819 b_adv(res->buf, msg->next);
6820 msg->next = 0;
6821 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6822 }
6823
Willy Tarreauf003d372012-11-26 13:35:37 +01006824 if (res->flags & CF_SHUTW)
6825 goto aborted_xfer;
6826
6827 /* stop waiting for data if the input is closed before the end. If the
6828 * client side was already closed, it means that the client has aborted,
6829 * so we don't want to count this as a server abort. Otherwise it's a
6830 * server abort.
6831 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006832 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006833 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006834 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006835 if (!(s->flags & SN_ERR_MASK))
6836 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006837 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006838 if (objt_server(s->target))
6839 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006840 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006841 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006842
Willy Tarreau40dba092010-03-04 18:14:51 +01006843 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006844 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006845 goto return_bad_res;
6846
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006847 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006848 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006849 * Similarly, with keep-alive on the client side, we don't want to forward a
6850 * close.
6851 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006852 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006853 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6854 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006855 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006856
Willy Tarreau5c620922011-05-11 19:56:11 +02006857 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006858 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006859 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006860 * modes are already handled by the stream sock layer. We must not do
6861 * this in content-length mode because it could present the MSG_MORE
6862 * flag with the last block of forwarded data, which would cause an
6863 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006864 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006865 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006866 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006867
Willy Tarreau87b09662015-04-03 00:22:06 +02006868 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006869 return 0;
6870
Willy Tarreau40dba092010-03-04 18:14:51 +01006871 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006872 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006873 if (objt_server(s->target))
6874 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006875
6876 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006877 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006878 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006879 compressing = 0;
6880 }
6881
Willy Tarreauc623c172014-04-18 09:53:50 +02006882 /* we may have some pending data starting at res->buf->p */
6883 if (s->comp_algo == NULL) {
6884 b_adv(res->buf, msg->next);
6885 msg->next = 0;
6886 }
6887
Willy Tarreaud98cf932009-12-27 22:54:55 +01006888 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006889 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006890 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006891 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006892 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006893 if (objt_server(s->target))
6894 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006895
6896 if (!(s->flags & SN_ERR_MASK))
6897 s->flags |= SN_ERR_PRXCOND;
6898 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006899 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006900 return 0;
6901
6902 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006903 if (unlikely(compressing)) {
6904 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6905 compressing = 0;
6906 }
6907
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006908 txn->rsp.msg_state = HTTP_MSG_ERROR;
6909 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006910 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006911 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006912 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006913
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006914 s->fe->fe_counters.cli_aborts++;
6915 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006916 if (objt_server(s->target))
6917 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006918
6919 if (!(s->flags & SN_ERR_MASK))
6920 s->flags |= SN_ERR_CLICL;
6921 if (!(s->flags & SN_FINST_MASK))
6922 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006923 return 0;
6924}
6925
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006926/* Iterate the same filter through all request headers.
6927 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 * Since it can manage the switch to another backend, it updates the per-proxy
6929 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006931int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006932{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 char *cur_ptr, *cur_end, *cur_next;
6934 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006935 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006936 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006937 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006939 last_hdr = 0;
6940
Willy Tarreau9b28e032012-10-12 23:49:43 +02006941 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 old_idx = 0;
6943
6944 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006945 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006946 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006947 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006948 (exp->action == ACT_ALLOW ||
6949 exp->action == ACT_DENY ||
6950 exp->action == ACT_TARPIT))
6951 return 0;
6952
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006953 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 if (!cur_idx)
6955 break;
6956
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006957 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 cur_ptr = cur_next;
6959 cur_end = cur_ptr + cur_hdr->len;
6960 cur_next = cur_end + cur_hdr->cr + 1;
6961
6962 /* Now we have one header between cur_ptr and cur_end,
6963 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964 */
6965
Willy Tarreau15a53a42015-01-21 13:39:42 +01006966 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006967 switch (exp->action) {
6968 case ACT_SETBE:
6969 /* It is not possible to jump a second time.
6970 * FIXME: should we return an HTTP/500 here so that
6971 * the admin knows there's a problem ?
6972 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006973 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006974 break;
6975
6976 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02006977 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 last_hdr = 1;
6979 break;
6980
6981 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006982 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006983 last_hdr = 1;
6984 break;
6985
6986 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006987 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 break;
6990
6991 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006992 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 break;
6995
6996 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006997 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6998 if (trash.len < 0)
6999 return -1;
7000
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007001 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007002 /* FIXME: if the user adds a newline in the replacement, the
7003 * index will not be recalculated for now, and the new line
7004 * will not be counted as a new header.
7005 */
7006
7007 cur_end += delta;
7008 cur_next += delta;
7009 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007010 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 break;
7012
7013 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007014 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007015 cur_next += delta;
7016
Willy Tarreaufa355d42009-11-29 18:12:29 +01007017 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007018 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7019 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007020 cur_hdr->len = 0;
7021 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007022 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007023 break;
7024
7025 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007026 }
7027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028 /* keep the link from this header to next one in case of later
7029 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007030 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 old_idx = cur_idx;
7032 }
7033 return 0;
7034}
7035
7036
7037/* Apply the filter to the request line.
7038 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7039 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007040 * Since it can manage the switch to another backend, it updates the per-proxy
7041 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007043int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 char *cur_ptr, *cur_end;
7046 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007047 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007048 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049
Willy Tarreau3d300592007-03-18 18:34:41 +01007050 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007052 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 (exp->action == ACT_ALLOW ||
7054 exp->action == ACT_DENY ||
7055 exp->action == ACT_TARPIT))
7056 return 0;
7057 else if (exp->action == ACT_REMOVE)
7058 return 0;
7059
7060 done = 0;
7061
Willy Tarreau9b28e032012-10-12 23:49:43 +02007062 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007063 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007064
7065 /* Now we have the request line between cur_ptr and cur_end */
7066
Willy Tarreau15a53a42015-01-21 13:39:42 +01007067 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 switch (exp->action) {
7069 case ACT_SETBE:
7070 /* It is not possible to jump a second time.
7071 * FIXME: should we return an HTTP/500 here so that
7072 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007074 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 break;
7076
7077 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007078 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 done = 1;
7080 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007083 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084 done = 1;
7085 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007087 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007088 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 done = 1;
7090 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007093 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007094 done = 1;
7095 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007098 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7099 if (trash.len < 0)
7100 return -1;
7101
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007102 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007103 /* FIXME: if the user adds a newline in the replacement, the
7104 * index will not be recalculated for now, and the new line
7105 * will not be counted as a new header.
7106 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007107
Willy Tarreaufa355d42009-11-29 18:12:29 +01007108 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007110 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 HTTP_MSG_RQMETH,
7112 cur_ptr, cur_end + 1,
7113 NULL, NULL);
7114 if (unlikely(!cur_end))
7115 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 /* we have a full request and we know that we have either a CR
7118 * or an LF at <ptr>.
7119 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007120 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7121 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 /* there is no point trying this regex on headers */
7123 return 1;
7124 }
7125 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 return done;
7127}
Willy Tarreau97de6242006-12-27 17:18:38 +01007128
Willy Tarreau58f10d72006-12-04 02:26:12 +01007129
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007131/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007132 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 * unparsable request. Since it can manage the switch to another backend, it
7135 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007137int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007138{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007139 struct http_txn *txn = &s->txn;
7140 struct hdr_exp *exp;
7141
7142 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 /*
7146 * The interleaving of transformations and verdicts
7147 * makes it difficult to decide to continue or stop
7148 * the evaluation.
7149 */
7150
Willy Tarreau6c123b12010-01-28 20:22:06 +01007151 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7152 break;
7153
Willy Tarreau3d300592007-03-18 18:34:41 +01007154 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007155 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007156 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007157 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007158
7159 /* if this filter had a condition, evaluate it now and skip to
7160 * next filter if the condition does not match.
7161 */
7162 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007163 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007164 ret = acl_pass(ret);
7165 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7166 ret = !ret;
7167
7168 if (!ret)
7169 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 }
7171
7172 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007173 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174 if (unlikely(ret < 0))
7175 return -1;
7176
7177 if (likely(ret == 0)) {
7178 /* The filter did not match the request, it can be
7179 * iterated through all headers.
7180 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007181 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7182 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007186}
7187
7188
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189
Willy Tarreau58f10d72006-12-04 02:26:12 +01007190/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007191 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007192 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007193 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007194void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007195 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007196 appsess *asession = NULL;
7197 char *sessid_temp = NULL;
7198
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007199 if (len > s->be->appsession_len) {
7200 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007201 }
7202
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007203 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007204 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007205 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007206 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007207 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007208 }
7209
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007210 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007211 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007212 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007213 return;
7214 }
7215
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007216 memcpy(txn->sessid, buf, len);
7217 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007218 }
7219
7220 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7221 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007222 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007223 return;
7224 }
7225
Cyril Bontéb21570a2009-11-29 20:04:48 +01007226 memcpy(sessid_temp, buf, len);
7227 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007228
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007229 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007230 /* free previously allocated memory */
7231 pool_free2(apools.sessid, sessid_temp);
7232
7233 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007234 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7235 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007236 asession->request_count++;
7237
7238 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007239 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007240
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007241 while (srv) {
7242 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007243 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007244 (s->be->options & PR_O_PERSIST) ||
7245 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007246 /* we found the server and it's usable */
7247 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007248 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007249 s->flags |= SN_DIRECT | SN_ASSIGNED;
7250 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007251
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007252 break;
7253 } else {
7254 txn->flags &= ~TX_CK_MASK;
7255 txn->flags |= TX_CK_DOWN;
7256 }
7257 }
7258 srv = srv->next;
7259 }
7260 }
7261 }
7262}
7263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007264/* Find the end of a cookie value contained between <s> and <e>. It works the
7265 * same way as with headers above except that the semi-colon also ends a token.
7266 * See RFC2965 for more information. Note that it requires a valid header to
7267 * return a valid result.
7268 */
7269char *find_cookie_value_end(char *s, const char *e)
7270{
7271 int quoted, qdpair;
7272
7273 quoted = qdpair = 0;
7274 for (; s < e; s++) {
7275 if (qdpair) qdpair = 0;
7276 else if (quoted) {
7277 if (*s == '\\') qdpair = 1;
7278 else if (*s == '"') quoted = 0;
7279 }
7280 else if (*s == '"') quoted = 1;
7281 else if (*s == ',' || *s == ';') return s;
7282 }
7283 return s;
7284}
7285
7286/* Delete a value in a header between delimiters <from> and <next> in buffer
7287 * <buf>. The number of characters displaced is returned, and the pointer to
7288 * the first delimiter is updated if required. The function tries as much as
7289 * possible to respect the following principles :
7290 * - replace <from> delimiter by the <next> one unless <from> points to a
7291 * colon, in which case <next> is simply removed
7292 * - set exactly one space character after the new first delimiter, unless
7293 * there are not enough characters in the block being moved to do so.
7294 * - remove unneeded spaces before the previous delimiter and after the new
7295 * one.
7296 *
7297 * It is the caller's responsibility to ensure that :
7298 * - <from> points to a valid delimiter or the colon ;
7299 * - <next> points to a valid delimiter or the final CR/LF ;
7300 * - there are non-space chars before <from> ;
7301 * - there is a CR/LF at or after <next>.
7302 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007303int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007304{
7305 char *prev = *from;
7306
7307 if (*prev == ':') {
7308 /* We're removing the first value, preserve the colon and add a
7309 * space if possible.
7310 */
7311 if (!http_is_crlf[(unsigned char)*next])
7312 next++;
7313 prev++;
7314 if (prev < next)
7315 *prev++ = ' ';
7316
7317 while (http_is_spht[(unsigned char)*next])
7318 next++;
7319 } else {
7320 /* Remove useless spaces before the old delimiter. */
7321 while (http_is_spht[(unsigned char)*(prev-1)])
7322 prev--;
7323 *from = prev;
7324
7325 /* copy the delimiter and if possible a space if we're
7326 * not at the end of the line.
7327 */
7328 if (!http_is_crlf[(unsigned char)*next]) {
7329 *prev++ = *next++;
7330 if (prev + 1 < next)
7331 *prev++ = ' ';
7332 while (http_is_spht[(unsigned char)*next])
7333 next++;
7334 }
7335 }
7336 return buffer_replace2(buf, prev, next, NULL, 0);
7337}
7338
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007339/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007340 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007341 * desirable to call it only when needed. This code is quite complex because
7342 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7343 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007344 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007345void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007347 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007348 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007350 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7351 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007352
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007353 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007354 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007355 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007356
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007357 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007359 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007361 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007362 hdr_beg = hdr_next;
7363 hdr_end = hdr_beg + cur_hdr->len;
7364 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007366 /* We have one full header between hdr_beg and hdr_end, and the
7367 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368 * "Cookie:" headers.
7369 */
7370
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007371 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007372 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007373 old_idx = cur_idx;
7374 continue;
7375 }
7376
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007377 del_from = NULL; /* nothing to be deleted */
7378 preserve_hdr = 0; /* assume we may kill the whole header */
7379
Willy Tarreau58f10d72006-12-04 02:26:12 +01007380 /* Now look for cookies. Conforming to RFC2109, we have to support
7381 * attributes whose name begin with a '$', and associate them with
7382 * the right cookie, if we want to delete this cookie.
7383 * So there are 3 cases for each cookie read :
7384 * 1) it's a special attribute, beginning with a '$' : ignore it.
7385 * 2) it's a server id cookie that we *MAY* want to delete : save
7386 * some pointers on it (last semi-colon, beginning of cookie...)
7387 * 3) it's an application cookie : we *MAY* have to delete a previous
7388 * "special" cookie.
7389 * At the end of loop, if a "special" cookie remains, we may have to
7390 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 * *MUST* delete it.
7392 *
7393 * Note: RFC2965 is unclear about the processing of spaces around
7394 * the equal sign in the ATTR=VALUE form. A careful inspection of
7395 * the RFC explicitly allows spaces before it, and not within the
7396 * tokens (attrs or values). An inspection of RFC2109 allows that
7397 * too but section 10.1.3 lets one think that spaces may be allowed
7398 * after the equal sign too, resulting in some (rare) buggy
7399 * implementations trying to do that. So let's do what servers do.
7400 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7401 * allowed quoted strings in values, with any possible character
7402 * after a backslash, including control chars and delimitors, which
7403 * causes parsing to become ambiguous. Browsers also allow spaces
7404 * within values even without quotes.
7405 *
7406 * We have to keep multiple pointers in order to support cookie
7407 * removal at the beginning, middle or end of header without
7408 * corrupting the header. All of these headers are valid :
7409 *
7410 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7411 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7412 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7413 * | | | | | | | | |
7414 * | | | | | | | | hdr_end <--+
7415 * | | | | | | | +--> next
7416 * | | | | | | +----> val_end
7417 * | | | | | +-----------> val_beg
7418 * | | | | +--------------> equal
7419 * | | | +----------------> att_end
7420 * | | +---------------------> att_beg
7421 * | +--------------------------> prev
7422 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423 */
7424
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007425 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7426 /* Iterate through all cookies on this line */
7427
7428 /* find att_beg */
7429 att_beg = prev + 1;
7430 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7431 att_beg++;
7432
7433 /* find att_end : this is the first character after the last non
7434 * space before the equal. It may be equal to hdr_end.
7435 */
7436 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007437
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007438 while (equal < hdr_end) {
7439 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007440 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441 if (http_is_spht[(unsigned char)*equal++])
7442 continue;
7443 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007444 }
7445
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007446 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7447 * is between <att_beg> and <equal>, both may be identical.
7448 */
7449
7450 /* look for end of cookie if there is an equal sign */
7451 if (equal < hdr_end && *equal == '=') {
7452 /* look for the beginning of the value */
7453 val_beg = equal + 1;
7454 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7455 val_beg++;
7456
7457 /* find the end of the value, respecting quotes */
7458 next = find_cookie_value_end(val_beg, hdr_end);
7459
7460 /* make val_end point to the first white space or delimitor after the value */
7461 val_end = next;
7462 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7463 val_end--;
7464 } else {
7465 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 /* We have nothing to do with attributes beginning with '$'. However,
7469 * they will automatically be removed if a header before them is removed,
7470 * since they're supposed to be linked together.
7471 */
7472 if (*att_beg == '$')
7473 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007474
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007475 /* Ignore cookies with no equal sign */
7476 if (equal == next) {
7477 /* This is not our cookie, so we must preserve it. But if we already
7478 * scheduled another cookie for removal, we cannot remove the
7479 * complete header, but we can remove the previous block itself.
7480 */
7481 preserve_hdr = 1;
7482 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007483 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007484 val_end += delta;
7485 next += delta;
7486 hdr_end += delta;
7487 hdr_next += delta;
7488 cur_hdr->len += delta;
7489 http_msg_move_end(&txn->req, delta);
7490 prev = del_from;
7491 del_from = NULL;
7492 }
7493 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007494 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007495
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 /* if there are spaces around the equal sign, we need to
7497 * strip them otherwise we'll get trouble for cookie captures,
7498 * or even for rewrites. Since this happens extremely rarely,
7499 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007500 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007501 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7502 int stripped_before = 0;
7503 int stripped_after = 0;
7504
7505 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007506 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 equal += stripped_before;
7508 val_beg += stripped_before;
7509 }
7510
7511 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007512 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 val_beg += stripped_after;
7514 stripped_before += stripped_after;
7515 }
7516
7517 val_end += stripped_before;
7518 next += stripped_before;
7519 hdr_end += stripped_before;
7520 hdr_next += stripped_before;
7521 cur_hdr->len += stripped_before;
7522 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 /* First, let's see if we want to capture this cookie. We check
7527 * that we don't already have a client side cookie, because we
7528 * can only capture one. Also as an optimisation, we ignore
7529 * cookies shorter than the declared name.
7530 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007531 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7532 (val_end - att_beg >= s->fe->capture_namelen) &&
7533 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007534 int log_len = val_end - att_beg;
7535
7536 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7537 Alert("HTTP logging : out of memory.\n");
7538 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007539 if (log_len > s->fe->capture_len)
7540 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 memcpy(txn->cli_cookie, att_beg, log_len);
7542 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007543 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007545
Willy Tarreaubca99692010-10-06 19:25:55 +02007546 /* Persistence cookies in passive, rewrite or insert mode have the
7547 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007549 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007551 * For cookies in prefix mode, the form is :
7552 *
7553 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007555 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7556 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7557 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007559
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7561 * have the server ID between val_beg and delim, and the original cookie between
7562 * delim+1 and val_end. Otherwise, delim==val_end :
7563 *
7564 * Cookie: NAME=SRV; # in all but prefix modes
7565 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7566 * | || || | |+-> next
7567 * | || || | +--> val_end
7568 * | || || +---------> delim
7569 * | || |+------------> val_beg
7570 * | || +-------------> att_end = equal
7571 * | |+-----------------> att_beg
7572 * | +------------------> prev
7573 * +-------------------------> hdr_beg
7574 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007576 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 for (delim = val_beg; delim < val_end; delim++)
7578 if (*delim == COOKIE_DELIM)
7579 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007580 } else {
7581 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007583 /* Now check if the cookie contains a date field, which would
7584 * appear after a vertical bar ('|') just after the server name
7585 * and before the delimiter.
7586 */
7587 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7588 if (vbar1) {
7589 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007590 * right is the last seen date. It is a base64 encoded
7591 * 30-bit value representing the UNIX date since the
7592 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007593 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007594 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007595 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007596 if (val_end - vbar1 >= 5) {
7597 val = b64tos30(vbar1);
7598 if (val > 0)
7599 txn->cookie_last_date = val << 2;
7600 }
7601 /* look for a second vertical bar */
7602 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7603 if (vbar1 && (val_end - vbar1 > 5)) {
7604 val = b64tos30(vbar1 + 1);
7605 if (val > 0)
7606 txn->cookie_first_date = val << 2;
7607 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007608 }
7609 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007610
Willy Tarreauf64d1412010-10-07 20:06:11 +02007611 /* if the cookie has an expiration date and the proxy wants to check
7612 * it, then we do that now. We first check if the cookie is too old,
7613 * then only if it has expired. We detect strict overflow because the
7614 * time resolution here is not great (4 seconds). Cookies with dates
7615 * in the future are ignored if their offset is beyond one day. This
7616 * allows an admin to fix timezone issues without expiring everyone
7617 * and at the same time avoids keeping unwanted side effects for too
7618 * long.
7619 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007620 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7621 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007622 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007623 txn->flags &= ~TX_CK_MASK;
7624 txn->flags |= TX_CK_OLD;
7625 delim = val_beg; // let's pretend we have not found the cookie
7626 txn->cookie_first_date = 0;
7627 txn->cookie_last_date = 0;
7628 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007629 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7630 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007631 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007632 txn->flags &= ~TX_CK_MASK;
7633 txn->flags |= TX_CK_EXPIRED;
7634 delim = val_beg; // let's pretend we have not found the cookie
7635 txn->cookie_first_date = 0;
7636 txn->cookie_last_date = 0;
7637 }
7638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 /* Here, we'll look for the first running server which supports the cookie.
7640 * This allows to share a same cookie between several servers, for example
7641 * to dedicate backup servers to specific servers only.
7642 * However, to prevent clients from sticking to cookie-less backup server
7643 * when they have incidentely learned an empty cookie, we simply ignore
7644 * empty cookies and mark them as invalid.
7645 * The same behaviour is applied when persistence must be ignored.
7646 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007647 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 while (srv) {
7651 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7652 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007653 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007654 (s->be->options & PR_O_PERSIST) ||
7655 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 /* we found the server and we can use it */
7657 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007658 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007659 s->flags |= SN_DIRECT | SN_ASSIGNED;
7660 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 break;
7662 } else {
7663 /* we found a server, but it's down,
7664 * mark it as such and go on in case
7665 * another one is available.
7666 */
7667 txn->flags &= ~TX_CK_MASK;
7668 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007669 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 srv = srv->next;
7672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreauf64d1412010-10-07 20:06:11 +02007674 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007675 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007677 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007678 txn->flags |= TX_CK_UNUSED;
7679 else
7680 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 /* depending on the cookie mode, we may have to either :
7684 * - delete the complete cookie if we're in insert+indirect mode, so that
7685 * the server never sees it ;
7686 * - remove the server id from the cookie value, and tag the cookie as an
7687 * application cookie so that it does not get accidentely removed later,
7688 * if we're in cookie prefix mode
7689 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007690 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692
Willy Tarreau9b28e032012-10-12 23:49:43 +02007693 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 val_end += delta;
7695 next += delta;
7696 hdr_end += delta;
7697 hdr_next += delta;
7698 cur_hdr->len += delta;
7699 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 del_from = NULL;
7702 preserve_hdr = 1; /* we want to keep this cookie */
7703 }
7704 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007705 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 del_from = prev;
7707 }
7708 } else {
7709 /* This is not our cookie, so we must preserve it. But if we already
7710 * scheduled another cookie for removal, we cannot remove the
7711 * complete header, but we can remove the previous block itself.
7712 */
7713 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007716 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007717 if (att_beg >= del_from)
7718 att_beg += delta;
7719 if (att_end >= del_from)
7720 att_end += delta;
7721 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 val_end += delta;
7723 next += delta;
7724 hdr_end += delta;
7725 hdr_next += delta;
7726 cur_hdr->len += delta;
7727 http_msg_move_end(&txn->req, delta);
7728 prev = del_from;
7729 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007732
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007733 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007734 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 int cmp_len, value_len;
7736 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007737
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007738 if (s->be->options2 & PR_O2_AS_PFX) {
7739 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7740 value_begin = att_beg + s->be->appsession_name_len;
7741 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007742 } else {
7743 cmp_len = att_end - att_beg;
7744 value_begin = val_beg;
7745 value_len = val_end - val_beg;
7746 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007747
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007749 if (cmp_len == s->be->appsession_name_len &&
7750 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7751 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007752 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007753 }
7754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 /* continue with next cookie on this header line */
7756 att_beg = next;
7757 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 /* There are no more cookies on this line.
7760 * We may still have one (or several) marked for deletion at the
7761 * end of the line. We must do this now in two ways :
7762 * - if some cookies must be preserved, we only delete from the
7763 * mark to the end of line ;
7764 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007767 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007769 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007770 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007771 cur_hdr->len += delta;
7772 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007773 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774
7775 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007776 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7777 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007779 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007782 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007783 }
7784
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007785 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788}
7789
7790
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791/* Iterate the same filter through all response headers contained in <rtr>.
7792 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7793 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007794int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 char *cur_ptr, *cur_end, *cur_next;
7797 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007798 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007800 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801
7802 last_hdr = 0;
7803
Willy Tarreau9b28e032012-10-12 23:49:43 +02007804 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 old_idx = 0;
7806
7807 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007808 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007810 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811 (exp->action == ACT_ALLOW ||
7812 exp->action == ACT_DENY))
7813 return 0;
7814
7815 cur_idx = txn->hdr_idx.v[old_idx].next;
7816 if (!cur_idx)
7817 break;
7818
7819 cur_hdr = &txn->hdr_idx.v[cur_idx];
7820 cur_ptr = cur_next;
7821 cur_end = cur_ptr + cur_hdr->len;
7822 cur_next = cur_end + cur_hdr->cr + 1;
7823
7824 /* Now we have one header between cur_ptr and cur_end,
7825 * and the next header starts at cur_next.
7826 */
7827
Willy Tarreau15a53a42015-01-21 13:39:42 +01007828 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 switch (exp->action) {
7830 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007831 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 last_hdr = 1;
7833 break;
7834
7835 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007836 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 last_hdr = 1;
7838 break;
7839
7840 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007841 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7842 if (trash.len < 0)
7843 return -1;
7844
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007845 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 /* FIXME: if the user adds a newline in the replacement, the
7847 * index will not be recalculated for now, and the new line
7848 * will not be counted as a new header.
7849 */
7850
7851 cur_end += delta;
7852 cur_next += delta;
7853 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007854 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 break;
7856
7857 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007858 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859 cur_next += delta;
7860
Willy Tarreaufa355d42009-11-29 18:12:29 +01007861 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7863 txn->hdr_idx.used--;
7864 cur_hdr->len = 0;
7865 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007866 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 break;
7868
7869 }
7870 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871
7872 /* keep the link from this header to next one in case of later
7873 * removal of next header.
7874 */
7875 old_idx = cur_idx;
7876 }
7877 return 0;
7878}
7879
7880
7881/* Apply the filter to the status line in the response buffer <rtr>.
7882 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7883 * or -1 if a replacement resulted in an invalid status line.
7884 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007885int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887 char *cur_ptr, *cur_end;
7888 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007889 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007890 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891
7892
Willy Tarreau3d300592007-03-18 18:34:41 +01007893 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007895 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 (exp->action == ACT_ALLOW ||
7897 exp->action == ACT_DENY))
7898 return 0;
7899 else if (exp->action == ACT_REMOVE)
7900 return 0;
7901
7902 done = 0;
7903
Willy Tarreau9b28e032012-10-12 23:49:43 +02007904 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007905 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906
7907 /* Now we have the status line between cur_ptr and cur_end */
7908
Willy Tarreau15a53a42015-01-21 13:39:42 +01007909 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 switch (exp->action) {
7911 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007912 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007913 done = 1;
7914 break;
7915
7916 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007917 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918 done = 1;
7919 break;
7920
7921 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007922 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7923 if (trash.len < 0)
7924 return -1;
7925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007926 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 /* FIXME: if the user adds a newline in the replacement, the
7928 * index will not be recalculated for now, and the new line
7929 * will not be counted as a new header.
7930 */
7931
Willy Tarreaufa355d42009-11-29 18:12:29 +01007932 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007934 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007935 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 cur_ptr, cur_end + 1,
7937 NULL, NULL);
7938 if (unlikely(!cur_end))
7939 return -1;
7940
7941 /* we have a full respnse and we know that we have either a CR
7942 * or an LF at <ptr>.
7943 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007944 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007945 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946 /* there is no point trying this regex on headers */
7947 return 1;
7948 }
7949 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 return done;
7951}
7952
7953
7954
7955/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007956 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7958 * unparsable response.
7959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007960int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007962 struct http_txn *txn = &s->txn;
7963 struct hdr_exp *exp;
7964
7965 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966 int ret;
7967
7968 /*
7969 * The interleaving of transformations and verdicts
7970 * makes it difficult to decide to continue or stop
7971 * the evaluation.
7972 */
7973
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007974 if (txn->flags & TX_SVDENY)
7975 break;
7976
Willy Tarreau3d300592007-03-18 18:34:41 +01007977 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7979 exp->action == ACT_PASS)) {
7980 exp = exp->next;
7981 continue;
7982 }
7983
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007984 /* if this filter had a condition, evaluate it now and skip to
7985 * next filter if the condition does not match.
7986 */
7987 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007988 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007989 ret = acl_pass(ret);
7990 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7991 ret = !ret;
7992 if (!ret)
7993 continue;
7994 }
7995
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007997 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 if (unlikely(ret < 0))
7999 return -1;
8000
8001 if (likely(ret == 0)) {
8002 /* The filter did not match the response, it can be
8003 * iterated through all headers.
8004 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008005 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8006 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 }
8009 return 0;
8010}
8011
8012
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008014 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008015 * desirable to call it only when needed. This function is also used when we
8016 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008018void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008020 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01008021 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008022 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008024 char *hdr_beg, *hdr_end, *hdr_next;
8025 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027 /* Iterate through the headers.
8028 * we start with the start line.
8029 */
8030 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008031 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032
8033 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8034 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008035 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036
8037 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 hdr_beg = hdr_next;
8039 hdr_end = hdr_beg + cur_hdr->len;
8040 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041
Willy Tarreau24581ba2010-08-31 22:39:35 +02008042 /* We have one full header between hdr_beg and hdr_end, and the
8043 * next header starts at hdr_next. We're only interested in
8044 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 */
8046
Willy Tarreau24581ba2010-08-31 22:39:35 +02008047 is_cookie2 = 0;
8048 prev = hdr_beg + 10;
8049 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008050 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008051 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8052 if (!val) {
8053 old_idx = cur_idx;
8054 continue;
8055 }
8056 is_cookie2 = 1;
8057 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 }
8059
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8061 * <prev> points to the colon.
8062 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008063 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8066 * check-cache is enabled) and we are not interested in checking
8067 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008068 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008069 if (s->be->cookie_name == NULL &&
8070 s->be->appsession_name == NULL &&
8071 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 return;
8073
Willy Tarreau24581ba2010-08-31 22:39:35 +02008074 /* OK so now we know we have to process this response cookie.
8075 * The format of the Set-Cookie header is slightly different
8076 * from the format of the Cookie header in that it does not
8077 * support the comma as a cookie delimiter (thus the header
8078 * cannot be folded) because the Expires attribute described in
8079 * the original Netscape's spec may contain an unquoted date
8080 * with a comma inside. We have to live with this because
8081 * many browsers don't support Max-Age and some browsers don't
8082 * support quoted strings. However the Set-Cookie2 header is
8083 * clean.
8084 *
8085 * We have to keep multiple pointers in order to support cookie
8086 * removal at the beginning, middle or end of header without
8087 * corrupting the header (in case of set-cookie2). A special
8088 * pointer, <scav> points to the beginning of the set-cookie-av
8089 * fields after the first semi-colon. The <next> pointer points
8090 * either to the end of line (set-cookie) or next unquoted comma
8091 * (set-cookie2). All of these headers are valid :
8092 *
8093 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8094 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8095 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8096 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8097 * | | | | | | | | | |
8098 * | | | | | | | | +-> next hdr_end <--+
8099 * | | | | | | | +------------> scav
8100 * | | | | | | +--------------> val_end
8101 * | | | | | +--------------------> val_beg
8102 * | | | | +----------------------> equal
8103 * | | | +------------------------> att_end
8104 * | | +----------------------------> att_beg
8105 * | +------------------------------> prev
8106 * +-----------------------------------------> hdr_beg
8107 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108
Willy Tarreau24581ba2010-08-31 22:39:35 +02008109 for (; prev < hdr_end; prev = next) {
8110 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111
Willy Tarreau24581ba2010-08-31 22:39:35 +02008112 /* find att_beg */
8113 att_beg = prev + 1;
8114 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8115 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 /* find att_end : this is the first character after the last non
8118 * space before the equal. It may be equal to hdr_end.
8119 */
8120 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121
Willy Tarreau24581ba2010-08-31 22:39:35 +02008122 while (equal < hdr_end) {
8123 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8124 break;
8125 if (http_is_spht[(unsigned char)*equal++])
8126 continue;
8127 att_end = equal;
8128 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129
Willy Tarreau24581ba2010-08-31 22:39:35 +02008130 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8131 * is between <att_beg> and <equal>, both may be identical.
8132 */
8133
8134 /* look for end of cookie if there is an equal sign */
8135 if (equal < hdr_end && *equal == '=') {
8136 /* look for the beginning of the value */
8137 val_beg = equal + 1;
8138 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8139 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 /* find the end of the value, respecting quotes */
8142 next = find_cookie_value_end(val_beg, hdr_end);
8143
8144 /* make val_end point to the first white space or delimitor after the value */
8145 val_end = next;
8146 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8147 val_end--;
8148 } else {
8149 /* <equal> points to next comma, semi-colon or EOL */
8150 val_beg = val_end = next = equal;
8151 }
8152
8153 if (next < hdr_end) {
8154 /* Set-Cookie2 supports multiple cookies, and <next> points to
8155 * a colon or semi-colon before the end. So skip all attr-value
8156 * pairs and look for the next comma. For Set-Cookie, since
8157 * commas are permitted in values, skip to the end.
8158 */
8159 if (is_cookie2)
8160 next = find_hdr_value_end(next, hdr_end);
8161 else
8162 next = hdr_end;
8163 }
8164
8165 /* Now everything is as on the diagram above */
8166
8167 /* Ignore cookies with no equal sign */
8168 if (equal == val_end)
8169 continue;
8170
8171 /* If there are spaces around the equal sign, we need to
8172 * strip them otherwise we'll get trouble for cookie captures,
8173 * or even for rewrites. Since this happens extremely rarely,
8174 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008176 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8177 int stripped_before = 0;
8178 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008181 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008182 equal += stripped_before;
8183 val_beg += stripped_before;
8184 }
8185
8186 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008187 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008188 val_beg += stripped_after;
8189 stripped_before += stripped_after;
8190 }
8191
8192 val_end += stripped_before;
8193 next += stripped_before;
8194 hdr_end += stripped_before;
8195 hdr_next += stripped_before;
8196 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008197 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008198 }
8199
8200 /* First, let's see if we want to capture this cookie. We check
8201 * that we don't already have a server side cookie, because we
8202 * can only capture one. Also as an optimisation, we ignore
8203 * cookies shorter than the declared name.
8204 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008206 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008207 (val_end - att_beg >= s->fe->capture_namelen) &&
8208 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008210 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 Alert("HTTP logging : out of memory.\n");
8212 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008213 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008214 if (log_len > s->fe->capture_len)
8215 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008216 memcpy(txn->srv_cookie, att_beg, log_len);
8217 txn->srv_cookie[log_len] = 0;
8218 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 }
8220
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008221 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008223 if (!(s->flags & SN_IGNORE_PRST) &&
8224 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8225 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008226 /* assume passive cookie by default */
8227 txn->flags &= ~TX_SCK_MASK;
8228 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229
8230 /* If the cookie is in insert mode on a known server, we'll delete
8231 * this occurrence because we'll insert another one later.
8232 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 * a direct access.
8234 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008236 /* The "preserve" flag was set, we don't want to touch the
8237 * server's cookie.
8238 */
8239 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008240 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8241 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 /* this cookie must be deleted */
8243 if (*prev == ':' && next == hdr_end) {
8244 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008245 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8247 txn->hdr_idx.used--;
8248 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008249 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 hdr_next += delta;
8251 http_msg_move_end(&txn->rsp, delta);
8252 /* note: while both invalid now, <next> and <hdr_end>
8253 * are still equal, so the for() will stop as expected.
8254 */
8255 } else {
8256 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008257 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 next = prev;
8259 hdr_end += delta;
8260 hdr_next += delta;
8261 cur_hdr->len += delta;
8262 http_msg_move_end(&txn->rsp, delta);
8263 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008264 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008265 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008268 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008269 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270 * with this server since we know it.
8271 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008272 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 next += delta;
8274 hdr_end += delta;
8275 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008276 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008277 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008278
Willy Tarreauf1348312010-10-07 15:54:11 +02008279 txn->flags &= ~TX_SCK_MASK;
8280 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008282 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008284 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008285 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008286 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008287 next += delta;
8288 hdr_end += delta;
8289 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008291 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
Willy Tarreau827aee92011-03-10 16:55:02 +01008293 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008294 txn->flags &= ~TX_SCK_MASK;
8295 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296 }
8297 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008298 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008299 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008300 int cmp_len, value_len;
8301 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008302
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008303 if (s->be->options2 & PR_O2_AS_PFX) {
8304 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8305 value_begin = att_beg + s->be->appsession_name_len;
8306 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008307 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008308 cmp_len = att_end - att_beg;
8309 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008310 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008311 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008312
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008313 if ((cmp_len == s->be->appsession_name_len) &&
8314 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008315 /* free a possibly previously allocated memory */
8316 pool_free2(apools.sessid, txn->sessid);
8317
Willy Tarreau87b09662015-04-03 00:22:06 +02008318 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008319 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008320 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008321 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008322 return;
8323 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008324 memcpy(txn->sessid, value_begin, value_len);
8325 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008326 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 }
8328 /* that's done for this cookie, check the next one on the same
8329 * line when next != hdr_end (only if is_cookie2).
8330 */
8331 }
8332 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008335
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008336 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008337 appsess *asession = NULL;
8338 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008339 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008340 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008341 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008342 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8343 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008344 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008345 return;
8346 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008347 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8348
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008349 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8350 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008351 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8352 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008353 return;
8354 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008355 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8356 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008357
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008358 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008359 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008360 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8362 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008363 return;
8364 }
8365 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008366 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008367
8368 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008369 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008370 }
8371
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008372 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008373 asession->request_count++;
8374 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375}
8376
8377
Willy Tarreaua15645d2007-03-18 16:22:39 +01008378/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008379 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008380 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008381void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008382{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008383 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008384 char *p1, *p2;
8385
8386 char *cur_ptr, *cur_end, *cur_next;
8387 int cur_idx;
8388
Willy Tarreau5df51872007-11-25 16:20:08 +01008389 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390 return;
8391
8392 /* Iterate through the headers.
8393 * we start with the start line.
8394 */
8395 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008396 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397
8398 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8399 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008400 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008401
8402 cur_hdr = &txn->hdr_idx.v[cur_idx];
8403 cur_ptr = cur_next;
8404 cur_end = cur_ptr + cur_hdr->len;
8405 cur_next = cur_end + cur_hdr->cr + 1;
8406
8407 /* We have one full header between cur_ptr and cur_end, and the
8408 * next header starts at cur_next. We're only interested in
8409 * "Cookie:" headers.
8410 */
8411
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008412 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8413 if (val) {
8414 if ((cur_end - (cur_ptr + val) >= 8) &&
8415 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8416 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8417 return;
8418 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419 }
8420
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008421 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8422 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 continue;
8424
8425 /* OK, right now we know we have a cache-control header at cur_ptr */
8426
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008427 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008428
8429 if (p1 >= cur_end) /* no more info */
8430 continue;
8431
8432 /* p1 is at the beginning of the value */
8433 p2 = p1;
8434
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008435 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 p2++;
8437
8438 /* we have a complete value between p1 and p2 */
8439 if (p2 < cur_end && *p2 == '=') {
8440 /* we have something of the form no-cache="set-cookie" */
8441 if ((cur_end - p1 >= 21) &&
8442 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8443 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008444 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445 continue;
8446 }
8447
8448 /* OK, so we know that either p2 points to the end of string or to a comma */
8449 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008450 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8452 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8453 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008454 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008455 return;
8456 }
8457
8458 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008459 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 continue;
8461 }
8462 }
8463}
8464
8465
Willy Tarreau58f10d72006-12-04 02:26:12 +01008466/*
8467 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008468 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008469 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008470void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008471{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008472 char *end_params, *first_param, *cur_param, *next_param;
8473 char separator;
8474 int value_len;
8475
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008476 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008477
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008478 if (s->be->appsession_name == NULL ||
8479 (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 +01008480 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008481 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008482
Cyril Bontéb21570a2009-11-29 20:04:48 +01008483 first_param = NULL;
8484 switch (mode) {
8485 case PR_O2_AS_M_PP:
8486 first_param = memchr(begin, ';', len);
8487 break;
8488 case PR_O2_AS_M_QS:
8489 first_param = memchr(begin, '?', len);
8490 break;
8491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008492
Cyril Bontéb21570a2009-11-29 20:04:48 +01008493 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008494 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008495 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008496
Cyril Bontéb21570a2009-11-29 20:04:48 +01008497 switch (mode) {
8498 case PR_O2_AS_M_PP:
8499 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8500 end_params = (char *) begin + len;
8501 }
8502 separator = ';';
8503 break;
8504 case PR_O2_AS_M_QS:
8505 end_params = (char *) begin + len;
8506 separator = '&';
8507 break;
8508 default:
8509 /* unknown mode, shouldn't happen */
8510 return;
8511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008512
Cyril Bontéb21570a2009-11-29 20:04:48 +01008513 cur_param = next_param = end_params;
8514 while (cur_param > first_param) {
8515 cur_param--;
8516 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8517 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008518 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8519 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8520 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008521 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008522 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8523 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008524 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008525 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008526 }
8527 break;
8528 }
8529 next_param = cur_param;
8530 }
8531 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008532#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008533 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008534 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008535#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008536}
8537
Willy Tarreaub2513902006-12-17 14:52:38 +01008538/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008539 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008540 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008541 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008542 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008543 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008544 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008545 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008546 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008547int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008548{
8549 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008550 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008551 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008552
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008553 if (!uri_auth)
8554 return 0;
8555
Cyril Bonté70be45d2010-10-12 00:14:35 +02008556 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008557 return 0;
8558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008559 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008560 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008561 return 0;
8562
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008563 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008564 return 0;
8565
Willy Tarreaub2513902006-12-17 14:52:38 +01008566 return 1;
8567}
8568
Willy Tarreau4076a152009-04-02 15:18:36 +02008569/*
8570 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008571 * By default it tries to report the error position as msg->err_pos. However if
8572 * this one is not set, it will then report msg->next, which is the last known
8573 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008574 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008575 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008576void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008577 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008578 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008579{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008580 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008581 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008582
Willy Tarreau9b28e032012-10-12 23:49:43 +02008583 es->len = MIN(chn->buf->i, sizeof(es->buf));
8584 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008585 len1 = MIN(len1, es->len);
8586 len2 = es->len - len1; /* remaining data if buffer wraps */
8587
Willy Tarreau9b28e032012-10-12 23:49:43 +02008588 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008589 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008590 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008591
Willy Tarreau4076a152009-04-02 15:18:36 +02008592 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008593 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008594 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008595 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008596
Willy Tarreau4076a152009-04-02 15:18:36 +02008597 es->when = date; // user-visible date
8598 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008599 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008600 es->oe = other_end;
Willy Tarreau350f4872014-11-28 14:42:25 +01008601 if (objt_conn(s->si[0].end))
8602 es->src = __objt_conn(s->si[0].end)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008603 else
8604 memset(&es->src, 0, sizeof(es->src));
8605
Willy Tarreau078272e2010-12-12 12:46:33 +01008606 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008607 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008608 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008609 es->s_flags = s->flags;
8610 es->t_flags = s->txn.flags;
8611 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008612 es->b_out = chn->buf->o;
8613 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008614 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008615 es->m_clen = msg->chunk_len;
8616 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008617}
Willy Tarreaub2513902006-12-17 14:52:38 +01008618
Willy Tarreau294c4732011-12-16 21:35:50 +01008619/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8620 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8621 * performed over the whole headers. Otherwise it must contain a valid header
8622 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8623 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8624 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8625 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008626 * -1. The value fetch stops at commas, so this function is suited for use with
8627 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008628 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008629 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008630unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008631 struct hdr_idx *idx, int occ,
8632 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008633{
Willy Tarreau294c4732011-12-16 21:35:50 +01008634 struct hdr_ctx local_ctx;
8635 char *ptr_hist[MAX_HDR_HISTORY];
8636 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008637 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008638 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008639
Willy Tarreau294c4732011-12-16 21:35:50 +01008640 if (!ctx) {
8641 local_ctx.idx = 0;
8642 ctx = &local_ctx;
8643 }
8644
Willy Tarreaubce70882009-09-07 11:51:47 +02008645 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008646 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008647 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008648 occ--;
8649 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008650 *vptr = ctx->line + ctx->val;
8651 *vlen = ctx->vlen;
8652 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008653 }
8654 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008655 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008656 }
8657
8658 /* negative occurrence, we scan all the list then walk back */
8659 if (-occ > MAX_HDR_HISTORY)
8660 return 0;
8661
Willy Tarreau294c4732011-12-16 21:35:50 +01008662 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008663 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008664 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8665 len_hist[hist_ptr] = ctx->vlen;
8666 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008667 hist_ptr = 0;
8668 found++;
8669 }
8670 if (-occ > found)
8671 return 0;
8672 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008673 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8674 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8675 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008676 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008677 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008678 if (hist_ptr >= MAX_HDR_HISTORY)
8679 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 *vptr = ptr_hist[hist_ptr];
8681 *vlen = len_hist[hist_ptr];
8682 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008683}
8684
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008685/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8686 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8687 * performed over the whole headers. Otherwise it must contain a valid header
8688 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8689 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8690 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8691 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8692 * -1. This function differs from http_get_hdr() in that it only returns full
8693 * line header values and does not stop at commas.
8694 * The return value is 0 if nothing was found, or non-zero otherwise.
8695 */
8696unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8697 struct hdr_idx *idx, int occ,
8698 struct hdr_ctx *ctx, char **vptr, int *vlen)
8699{
8700 struct hdr_ctx local_ctx;
8701 char *ptr_hist[MAX_HDR_HISTORY];
8702 int len_hist[MAX_HDR_HISTORY];
8703 unsigned int hist_ptr;
8704 int found;
8705
8706 if (!ctx) {
8707 local_ctx.idx = 0;
8708 ctx = &local_ctx;
8709 }
8710
8711 if (occ >= 0) {
8712 /* search from the beginning */
8713 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8714 occ--;
8715 if (occ <= 0) {
8716 *vptr = ctx->line + ctx->val;
8717 *vlen = ctx->vlen;
8718 return 1;
8719 }
8720 }
8721 return 0;
8722 }
8723
8724 /* negative occurrence, we scan all the list then walk back */
8725 if (-occ > MAX_HDR_HISTORY)
8726 return 0;
8727
8728 found = hist_ptr = 0;
8729 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8730 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8731 len_hist[hist_ptr] = ctx->vlen;
8732 if (++hist_ptr >= MAX_HDR_HISTORY)
8733 hist_ptr = 0;
8734 found++;
8735 }
8736 if (-occ > found)
8737 return 0;
8738 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8739 * find occurrence -occ, so we have to check [hist_ptr+occ].
8740 */
8741 hist_ptr += occ;
8742 if (hist_ptr >= MAX_HDR_HISTORY)
8743 hist_ptr -= MAX_HDR_HISTORY;
8744 *vptr = ptr_hist[hist_ptr];
8745 *vlen = len_hist[hist_ptr];
8746 return 1;
8747}
8748
Willy Tarreaubaaee002006-06-26 02:48:02 +02008749/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008750 * Print a debug line with a header. Always stop at the first CR or LF char,
8751 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8752 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008753 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008754void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008755{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008756 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008757 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008758 dir,
Willy Tarreau350f4872014-11-28 14:42:25 +01008759 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
8760 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 +02008761
8762 for (max = 0; start + max < end; max++)
8763 if (start[max] == '\r' || start[max] == '\n')
8764 break;
8765
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008766 UBOUND(max, trash.size - trash.len - 3);
8767 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8768 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008769 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008770}
8771
Willy Tarreau0937bc42009-12-22 15:03:09 +01008772/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008773 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008774 * the required fields are properly allocated and that we only need to (re)init
8775 * them. This should be used before processing any new request.
8776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008777void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008778{
8779 struct http_txn *txn = &s->txn;
8780 struct proxy *fe = s->fe;
8781
8782 txn->flags = 0;
8783 txn->status = -1;
8784
Willy Tarreauf64d1412010-10-07 20:06:11 +02008785 txn->cookie_first_date = 0;
8786 txn->cookie_last_date = 0;
8787
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008788 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008789 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008790 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008791 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008792 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008793 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008794 txn->req.chunk_len = 0LL;
8795 txn->req.body_len = 0LL;
8796 txn->rsp.chunk_len = 0LL;
8797 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008798 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8799 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008800 txn->req.chn = &s->req;
8801 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008802
8803 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008804
8805 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8806 if (fe->options2 & PR_O2_REQBUG_OK)
8807 txn->req.err_pos = -1; /* let buggy requests pass */
8808
Willy Tarreau46023632010-01-07 22:51:47 +01008809 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008810 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8811
Willy Tarreau46023632010-01-07 22:51:47 +01008812 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008813 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8814
8815 if (txn->hdr_idx.v)
8816 hdr_idx_init(&txn->hdr_idx);
8817}
8818
8819/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008820void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008821{
8822 struct http_txn *txn = &s->txn;
8823
Willy Tarreau75195602014-03-11 15:48:55 +01008824 /* release any possible compression context */
8825 if (s->flags & SN_COMP_READY)
8826 s->comp_algo->end(&s->comp_ctx);
8827 s->comp_algo = NULL;
8828 s->flags &= ~SN_COMP_READY;
8829
Willy Tarreau0937bc42009-12-22 15:03:09 +01008830 /* these ones will have been dynamically allocated */
8831 pool_free2(pool2_requri, txn->uri);
8832 pool_free2(pool2_capture, txn->cli_cookie);
8833 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008834 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008835 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008836
William Lallemanda73203e2012-03-12 12:48:57 +01008837 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008838 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008839 txn->uri = NULL;
8840 txn->srv_cookie = NULL;
8841 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008842
8843 if (txn->req.cap) {
8844 struct cap_hdr *h;
8845 for (h = s->fe->req_cap; h; h = h->next)
8846 pool_free2(h->pool, txn->req.cap[h->index]);
8847 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8848 }
8849
8850 if (txn->rsp.cap) {
8851 struct cap_hdr *h;
8852 for (h = s->fe->rsp_cap; h; h = h->next)
8853 pool_free2(h->pool, txn->rsp.cap[h->index]);
8854 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8855 }
8856
Willy Tarreau0937bc42009-12-22 15:03:09 +01008857}
8858
8859/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008860void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008861{
8862 http_end_txn(s);
8863 http_init_txn(s);
8864
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008865 /* reinitialise the current rule list pointer to NULL. We are sure that
8866 * any rulelist match the NULL pointer.
8867 */
8868 s->current_rule_list = NULL;
8869
Willy Tarreau0937bc42009-12-22 15:03:09 +01008870 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008872 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008873 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008874 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008875 /* re-init store persistence */
8876 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008877 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008878
Willy Tarreau0937bc42009-12-22 15:03:09 +01008879 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008881 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008882
Willy Tarreau739cfba2010-01-25 23:11:14 +01008883 /* We must trim any excess data from the response buffer, because we
8884 * may have blocked an invalid response from a server that we don't
8885 * want to accidentely forward once we disable the analysers, nor do
8886 * we want those data to come along with next response. A typical
8887 * example of such data would be from a buggy server responding to
8888 * a HEAD with some data, or sending more than the advertised
8889 * content-length.
8890 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008891 if (unlikely(s->res.buf->i))
8892 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008893
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008894 s->req.rto = s->fe->timeout.client;
8895 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008896
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008897 s->res.rto = TICK_ETERNITY;
8898 s->res.wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008899
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008900 s->req.rex = TICK_ETERNITY;
8901 s->req.wex = TICK_ETERNITY;
8902 s->req.analyse_exp = TICK_ETERNITY;
8903 s->res.rex = TICK_ETERNITY;
8904 s->res.wex = TICK_ETERNITY;
8905 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008907
Sasha Pachev218f0642014-06-16 12:05:59 -06008908void free_http_res_rules(struct list *r)
8909{
8910 struct http_res_rule *tr, *pr;
8911
8912 list_for_each_entry_safe(pr, tr, r, list) {
8913 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008914 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008915 free(pr);
8916 }
8917}
8918
8919void free_http_req_rules(struct list *r)
8920{
Willy Tarreauff011f22011-01-06 17:51:27 +01008921 struct http_req_rule *tr, *pr;
8922
8923 list_for_each_entry_safe(pr, tr, r, list) {
8924 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008925 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008926 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008927
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008928 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008929 free(pr);
8930 }
8931}
8932
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008933/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008934struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8935{
8936 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008937 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008938 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008939 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008940
8941 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8942 if (!rule) {
8943 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008944 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008945 }
8946
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008947 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008948 rule->action = HTTP_REQ_ACT_ALLOW;
8949 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008950 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008951 rule->action = HTTP_REQ_ACT_DENY;
8952 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008953 } else if (!strcmp(args[0], "tarpit")) {
8954 rule->action = HTTP_REQ_ACT_TARPIT;
8955 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008956 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008957 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008958 cur_arg = 1;
8959
8960 while(*args[cur_arg]) {
8961 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008962 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008963 cur_arg+=2;
8964 continue;
8965 } else
8966 break;
8967 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008968 } else if (!strcmp(args[0], "set-nice")) {
8969 rule->action = HTTP_REQ_ACT_SET_NICE;
8970 cur_arg = 1;
8971
8972 if (!*args[cur_arg] ||
8973 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8974 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8975 file, linenum, args[0]);
8976 goto out_err;
8977 }
8978 rule->arg.nice = atoi(args[cur_arg]);
8979 if (rule->arg.nice < -1024)
8980 rule->arg.nice = -1024;
8981 else if (rule->arg.nice > 1024)
8982 rule->arg.nice = 1024;
8983 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008984 } else if (!strcmp(args[0], "set-tos")) {
8985#ifdef IP_TOS
8986 char *err;
8987 rule->action = HTTP_REQ_ACT_SET_TOS;
8988 cur_arg = 1;
8989
8990 if (!*args[cur_arg] ||
8991 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8992 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8993 file, linenum, args[0]);
8994 goto out_err;
8995 }
8996
8997 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8998 if (err && *err != '\0') {
8999 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9000 file, linenum, err, args[0]);
9001 goto out_err;
9002 }
9003 cur_arg++;
9004#else
9005 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9006 goto out_err;
9007#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009008 } else if (!strcmp(args[0], "set-mark")) {
9009#ifdef SO_MARK
9010 char *err;
9011 rule->action = HTTP_REQ_ACT_SET_MARK;
9012 cur_arg = 1;
9013
9014 if (!*args[cur_arg] ||
9015 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9016 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9017 file, linenum, args[0]);
9018 goto out_err;
9019 }
9020
9021 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9022 if (err && *err != '\0') {
9023 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9024 file, linenum, err, args[0]);
9025 goto out_err;
9026 }
9027 cur_arg++;
9028 global.last_checks |= LSTCHK_NETADM;
9029#else
9030 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9031 goto out_err;
9032#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009033 } else if (!strcmp(args[0], "set-log-level")) {
9034 rule->action = HTTP_REQ_ACT_SET_LOGL;
9035 cur_arg = 1;
9036
9037 if (!*args[cur_arg] ||
9038 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9039 bad_log_level:
9040 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9041 file, linenum, args[0]);
9042 goto out_err;
9043 }
9044 if (strcmp(args[cur_arg], "silent") == 0)
9045 rule->arg.loglevel = -1;
9046 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9047 goto bad_log_level;
9048 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009049 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9050 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9051 cur_arg = 1;
9052
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009053 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9054 (*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 +01009055 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9056 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009057 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009058 }
9059
9060 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9061 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9062 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009063
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009064 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009065 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009066 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9067 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009068 free(proxy->conf.lfs_file);
9069 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9070 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009071 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009072 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9073 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009074 cur_arg = 1;
9075
9076 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009077 (*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 -06009078 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9079 file, linenum, args[0]);
9080 goto out_err;
9081 }
9082
9083 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9084 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9085 LIST_INIT(&rule->arg.hdr_add.fmt);
9086
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009087 error = NULL;
9088 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9089 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9090 args[cur_arg + 1], error);
9091 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009092 goto out_err;
9093 }
9094
9095 proxy->conf.args.ctx = ARGC_HRQ;
9096 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9097 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9098 file, linenum);
9099
9100 free(proxy->conf.lfs_file);
9101 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9102 proxy->conf.lfs_line = proxy->conf.args.line;
9103 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009104 } else if (strcmp(args[0], "del-header") == 0) {
9105 rule->action = HTTP_REQ_ACT_DEL_HDR;
9106 cur_arg = 1;
9107
9108 if (!*args[cur_arg] ||
9109 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9110 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9111 file, linenum, args[0]);
9112 goto out_err;
9113 }
9114
9115 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9116 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9117
9118 proxy->conf.args.ctx = ARGC_HRQ;
9119 free(proxy->conf.lfs_file);
9120 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9121 proxy->conf.lfs_line = proxy->conf.args.line;
9122 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009123 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9124 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009125 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009126 struct sample_expr *expr;
9127 unsigned int where;
9128 char *err = NULL;
9129
9130 cur_arg = 1;
9131 proxy->conf.args.ctx = ARGC_TRK;
9132
9133 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9134 if (!expr) {
9135 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9136 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9137 free(err);
9138 goto out_err;
9139 }
9140
9141 where = 0;
9142 if (proxy->cap & PR_CAP_FE)
9143 where |= SMP_VAL_FE_HRQ_HDR;
9144 if (proxy->cap & PR_CAP_BE)
9145 where |= SMP_VAL_BE_HRQ_HDR;
9146
9147 if (!(expr->fetch->val & where)) {
9148 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9149 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9150 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9151 args[cur_arg-1], sample_src_names(expr->fetch->use));
9152 free(expr);
9153 goto out_err;
9154 }
9155
9156 if (strcmp(args[cur_arg], "table") == 0) {
9157 cur_arg++;
9158 if (!args[cur_arg]) {
9159 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9160 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9161 free(expr);
9162 goto out_err;
9163 }
9164 /* we copy the table name for now, it will be resolved later */
9165 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9166 cur_arg++;
9167 }
9168 rule->act_prm.trk_ctr.expr = expr;
9169 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009170 } else if (strcmp(args[0], "redirect") == 0) {
9171 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009172 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009173
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009174 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009175 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9176 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9177 goto out_err;
9178 }
9179
9180 /* this redirect rule might already contain a parsed condition which
9181 * we'll pass to the http-request rule.
9182 */
9183 rule->action = HTTP_REQ_ACT_REDIR;
9184 rule->arg.redir = redir;
9185 rule->cond = redir->cond;
9186 redir->cond = NULL;
9187 cur_arg = 2;
9188 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009189 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9190 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9191 rule->action = HTTP_REQ_ACT_ADD_ACL;
9192 /*
9193 * '+ 8' for 'add-acl('
9194 * '- 9' for 'add-acl(' + trailing ')'
9195 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009196 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009197
9198 cur_arg = 1;
9199
9200 if (!*args[cur_arg] ||
9201 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9202 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9203 file, linenum, args[0]);
9204 goto out_err;
9205 }
9206
9207 LIST_INIT(&rule->arg.map.key);
9208 proxy->conf.args.ctx = ARGC_HRQ;
9209 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9210 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9211 file, linenum);
9212 free(proxy->conf.lfs_file);
9213 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9214 proxy->conf.lfs_line = proxy->conf.args.line;
9215 cur_arg += 1;
9216 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9217 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9218 rule->action = HTTP_REQ_ACT_DEL_ACL;
9219 /*
9220 * '+ 8' for 'del-acl('
9221 * '- 9' for 'del-acl(' + trailing ')'
9222 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009223 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009224
9225 cur_arg = 1;
9226
9227 if (!*args[cur_arg] ||
9228 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9229 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9230 file, linenum, args[0]);
9231 goto out_err;
9232 }
9233
9234 LIST_INIT(&rule->arg.map.key);
9235 proxy->conf.args.ctx = ARGC_HRQ;
9236 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9237 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9238 file, linenum);
9239 free(proxy->conf.lfs_file);
9240 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9241 proxy->conf.lfs_line = proxy->conf.args.line;
9242 cur_arg += 1;
9243 } else if (strncmp(args[0], "del-map", 7) == 0) {
9244 /* http-request del-map(<reference (map name)>) <key pattern> */
9245 rule->action = HTTP_REQ_ACT_DEL_MAP;
9246 /*
9247 * '+ 8' for 'del-map('
9248 * '- 9' for 'del-map(' + trailing ')'
9249 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009250 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009251
9252 cur_arg = 1;
9253
9254 if (!*args[cur_arg] ||
9255 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9256 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9257 file, linenum, args[0]);
9258 goto out_err;
9259 }
9260
9261 LIST_INIT(&rule->arg.map.key);
9262 proxy->conf.args.ctx = ARGC_HRQ;
9263 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9264 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9265 file, linenum);
9266 free(proxy->conf.lfs_file);
9267 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9268 proxy->conf.lfs_line = proxy->conf.args.line;
9269 cur_arg += 1;
9270 } else if (strncmp(args[0], "set-map", 7) == 0) {
9271 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9272 rule->action = HTTP_REQ_ACT_SET_MAP;
9273 /*
9274 * '+ 8' for 'set-map('
9275 * '- 9' for 'set-map(' + trailing ')'
9276 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009277 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009278
9279 cur_arg = 1;
9280
9281 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9282 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9283 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9284 file, linenum, args[0]);
9285 goto out_err;
9286 }
9287
9288 LIST_INIT(&rule->arg.map.key);
9289 LIST_INIT(&rule->arg.map.value);
9290 proxy->conf.args.ctx = ARGC_HRQ;
9291
9292 /* key pattern */
9293 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9294 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9295 file, linenum);
9296
9297 /* value pattern */
9298 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9299 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9300 file, linenum);
9301 free(proxy->conf.lfs_file);
9302 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9303 proxy->conf.lfs_line = proxy->conf.args.line;
9304
9305 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009306 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9307 char *errmsg = NULL;
9308 cur_arg = 1;
9309 /* try in the module list */
9310 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9311 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9312 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9313 free(errmsg);
9314 goto out_err;
9315 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009316 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009317 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 +01009318 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009319 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009320 }
9321
9322 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9323 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009324 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009325
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009326 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9327 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9328 file, linenum, args[0], errmsg);
9329 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009330 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009331 }
9332 rule->cond = cond;
9333 }
9334 else if (*args[cur_arg]) {
9335 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9336 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9337 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009338 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009339 }
9340
9341 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009342 out_err:
9343 free(rule);
9344 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009345}
9346
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009347/* parse an "http-respose" rule */
9348struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9349{
9350 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009351 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009352 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009353 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009354
9355 rule = calloc(1, sizeof(*rule));
9356 if (!rule) {
9357 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9358 goto out_err;
9359 }
9360
9361 if (!strcmp(args[0], "allow")) {
9362 rule->action = HTTP_RES_ACT_ALLOW;
9363 cur_arg = 1;
9364 } else if (!strcmp(args[0], "deny")) {
9365 rule->action = HTTP_RES_ACT_DENY;
9366 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009367 } else if (!strcmp(args[0], "set-nice")) {
9368 rule->action = HTTP_RES_ACT_SET_NICE;
9369 cur_arg = 1;
9370
9371 if (!*args[cur_arg] ||
9372 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9373 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9374 file, linenum, args[0]);
9375 goto out_err;
9376 }
9377 rule->arg.nice = atoi(args[cur_arg]);
9378 if (rule->arg.nice < -1024)
9379 rule->arg.nice = -1024;
9380 else if (rule->arg.nice > 1024)
9381 rule->arg.nice = 1024;
9382 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009383 } else if (!strcmp(args[0], "set-tos")) {
9384#ifdef IP_TOS
9385 char *err;
9386 rule->action = HTTP_RES_ACT_SET_TOS;
9387 cur_arg = 1;
9388
9389 if (!*args[cur_arg] ||
9390 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9391 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9392 file, linenum, args[0]);
9393 goto out_err;
9394 }
9395
9396 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9397 if (err && *err != '\0') {
9398 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9399 file, linenum, err, args[0]);
9400 goto out_err;
9401 }
9402 cur_arg++;
9403#else
9404 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9405 goto out_err;
9406#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009407 } else if (!strcmp(args[0], "set-mark")) {
9408#ifdef SO_MARK
9409 char *err;
9410 rule->action = HTTP_RES_ACT_SET_MARK;
9411 cur_arg = 1;
9412
9413 if (!*args[cur_arg] ||
9414 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9415 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9416 file, linenum, args[0]);
9417 goto out_err;
9418 }
9419
9420 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9421 if (err && *err != '\0') {
9422 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9423 file, linenum, err, args[0]);
9424 goto out_err;
9425 }
9426 cur_arg++;
9427 global.last_checks |= LSTCHK_NETADM;
9428#else
9429 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9430 goto out_err;
9431#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009432 } else if (!strcmp(args[0], "set-log-level")) {
9433 rule->action = HTTP_RES_ACT_SET_LOGL;
9434 cur_arg = 1;
9435
9436 if (!*args[cur_arg] ||
9437 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9438 bad_log_level:
9439 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9440 file, linenum, args[0]);
9441 goto out_err;
9442 }
9443 if (strcmp(args[cur_arg], "silent") == 0)
9444 rule->arg.loglevel = -1;
9445 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9446 goto bad_log_level;
9447 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009448 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9449 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9450 cur_arg = 1;
9451
9452 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9453 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9454 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9455 file, linenum, args[0]);
9456 goto out_err;
9457 }
9458
9459 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9460 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9461 LIST_INIT(&rule->arg.hdr_add.fmt);
9462
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009463 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009464 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009465 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9466 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009467 free(proxy->conf.lfs_file);
9468 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9469 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009470 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009471 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009472 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009473 cur_arg = 1;
9474
9475 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009476 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9477 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009478 file, linenum, args[0]);
9479 goto out_err;
9480 }
9481
9482 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9483 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9484 LIST_INIT(&rule->arg.hdr_add.fmt);
9485
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009486 error = NULL;
9487 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9488 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9489 args[cur_arg + 1], error);
9490 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009491 goto out_err;
9492 }
9493
9494 proxy->conf.args.ctx = ARGC_HRQ;
9495 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9496 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9497 file, linenum);
9498
9499 free(proxy->conf.lfs_file);
9500 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9501 proxy->conf.lfs_line = proxy->conf.args.line;
9502 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009503 } else if (strcmp(args[0], "del-header") == 0) {
9504 rule->action = HTTP_RES_ACT_DEL_HDR;
9505 cur_arg = 1;
9506
9507 if (!*args[cur_arg] ||
9508 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9509 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9510 file, linenum, args[0]);
9511 goto out_err;
9512 }
9513
9514 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9515 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9516
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009517 proxy->conf.args.ctx = ARGC_HRS;
9518 free(proxy->conf.lfs_file);
9519 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9520 proxy->conf.lfs_line = proxy->conf.args.line;
9521 cur_arg += 1;
9522 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9523 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9524 rule->action = HTTP_RES_ACT_ADD_ACL;
9525 /*
9526 * '+ 8' for 'add-acl('
9527 * '- 9' for 'add-acl(' + trailing ')'
9528 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009529 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009530
9531 cur_arg = 1;
9532
9533 if (!*args[cur_arg] ||
9534 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9535 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9536 file, linenum, args[0]);
9537 goto out_err;
9538 }
9539
9540 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009541 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009542 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9543 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9544 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009545 free(proxy->conf.lfs_file);
9546 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9547 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009548
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009549 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009550 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9551 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9552 rule->action = HTTP_RES_ACT_DEL_ACL;
9553 /*
9554 * '+ 8' for 'del-acl('
9555 * '- 9' for 'del-acl(' + trailing ')'
9556 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009557 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009558
9559 cur_arg = 1;
9560
9561 if (!*args[cur_arg] ||
9562 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9563 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9564 file, linenum, args[0]);
9565 goto out_err;
9566 }
9567
9568 LIST_INIT(&rule->arg.map.key);
9569 proxy->conf.args.ctx = ARGC_HRS;
9570 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9571 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9572 file, linenum);
9573 free(proxy->conf.lfs_file);
9574 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9575 proxy->conf.lfs_line = proxy->conf.args.line;
9576 cur_arg += 1;
9577 } else if (strncmp(args[0], "del-map", 7) == 0) {
9578 /* http-response del-map(<reference (map name)>) <key pattern> */
9579 rule->action = HTTP_RES_ACT_DEL_MAP;
9580 /*
9581 * '+ 8' for 'del-map('
9582 * '- 9' for 'del-map(' + trailing ')'
9583 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009584 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009585
9586 cur_arg = 1;
9587
9588 if (!*args[cur_arg] ||
9589 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9590 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9591 file, linenum, args[0]);
9592 goto out_err;
9593 }
9594
9595 LIST_INIT(&rule->arg.map.key);
9596 proxy->conf.args.ctx = ARGC_HRS;
9597 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9598 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9599 file, linenum);
9600 free(proxy->conf.lfs_file);
9601 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9602 proxy->conf.lfs_line = proxy->conf.args.line;
9603 cur_arg += 1;
9604 } else if (strncmp(args[0], "set-map", 7) == 0) {
9605 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9606 rule->action = HTTP_RES_ACT_SET_MAP;
9607 /*
9608 * '+ 8' for 'set-map('
9609 * '- 9' for 'set-map(' + trailing ')'
9610 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009611 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009612
9613 cur_arg = 1;
9614
9615 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9616 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9617 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9618 file, linenum, args[0]);
9619 goto out_err;
9620 }
9621
9622 LIST_INIT(&rule->arg.map.key);
9623 LIST_INIT(&rule->arg.map.value);
9624
9625 proxy->conf.args.ctx = ARGC_HRS;
9626
9627 /* key pattern */
9628 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9629 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9630 file, linenum);
9631
9632 /* value pattern */
9633 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9634 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9635 file, linenum);
9636
9637 free(proxy->conf.lfs_file);
9638 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9639 proxy->conf.lfs_line = proxy->conf.args.line;
9640
9641 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009642 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9643 char *errmsg = NULL;
9644 cur_arg = 1;
9645 /* try in the module list */
9646 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9647 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9648 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9649 free(errmsg);
9650 goto out_err;
9651 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009652 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009653 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 +02009654 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9655 goto out_err;
9656 }
9657
9658 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9659 struct acl_cond *cond;
9660 char *errmsg = NULL;
9661
9662 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9663 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9664 file, linenum, args[0], errmsg);
9665 free(errmsg);
9666 goto out_err;
9667 }
9668 rule->cond = cond;
9669 }
9670 else if (*args[cur_arg]) {
9671 Alert("parsing [%s:%d]: 'http-response %s' expects"
9672 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9673 file, linenum, args[0], args[cur_arg]);
9674 goto out_err;
9675 }
9676
9677 return rule;
9678 out_err:
9679 free(rule);
9680 return NULL;
9681}
9682
Willy Tarreau4baae242012-12-27 12:00:31 +01009683/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009684 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9685 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009686 */
9687struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009688 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009689{
9690 struct redirect_rule *rule;
9691 int cur_arg;
9692 int type = REDIRECT_TYPE_NONE;
9693 int code = 302;
9694 const char *destination = NULL;
9695 const char *cookie = NULL;
9696 int cookie_set = 0;
9697 unsigned int flags = REDIRECT_FLAG_NONE;
9698 struct acl_cond *cond = NULL;
9699
9700 cur_arg = 0;
9701 while (*(args[cur_arg])) {
9702 if (strcmp(args[cur_arg], "location") == 0) {
9703 if (!*args[cur_arg + 1])
9704 goto missing_arg;
9705
9706 type = REDIRECT_TYPE_LOCATION;
9707 cur_arg++;
9708 destination = args[cur_arg];
9709 }
9710 else if (strcmp(args[cur_arg], "prefix") == 0) {
9711 if (!*args[cur_arg + 1])
9712 goto missing_arg;
9713
9714 type = REDIRECT_TYPE_PREFIX;
9715 cur_arg++;
9716 destination = args[cur_arg];
9717 }
9718 else if (strcmp(args[cur_arg], "scheme") == 0) {
9719 if (!*args[cur_arg + 1])
9720 goto missing_arg;
9721
9722 type = REDIRECT_TYPE_SCHEME;
9723 cur_arg++;
9724 destination = args[cur_arg];
9725 }
9726 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9727 if (!*args[cur_arg + 1])
9728 goto missing_arg;
9729
9730 cur_arg++;
9731 cookie = args[cur_arg];
9732 cookie_set = 1;
9733 }
9734 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9735 if (!*args[cur_arg + 1])
9736 goto missing_arg;
9737
9738 cur_arg++;
9739 cookie = args[cur_arg];
9740 cookie_set = 0;
9741 }
9742 else if (strcmp(args[cur_arg], "code") == 0) {
9743 if (!*args[cur_arg + 1])
9744 goto missing_arg;
9745
9746 cur_arg++;
9747 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009748 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009749 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009750 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009751 args[cur_arg - 1], args[cur_arg]);
9752 return NULL;
9753 }
9754 }
9755 else if (!strcmp(args[cur_arg],"drop-query")) {
9756 flags |= REDIRECT_FLAG_DROP_QS;
9757 }
9758 else if (!strcmp(args[cur_arg],"append-slash")) {
9759 flags |= REDIRECT_FLAG_APPEND_SLASH;
9760 }
9761 else if (strcmp(args[cur_arg], "if") == 0 ||
9762 strcmp(args[cur_arg], "unless") == 0) {
9763 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9764 if (!cond) {
9765 memprintf(errmsg, "error in condition: %s", *errmsg);
9766 return NULL;
9767 }
9768 break;
9769 }
9770 else {
9771 memprintf(errmsg,
9772 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9773 args[cur_arg]);
9774 return NULL;
9775 }
9776 cur_arg++;
9777 }
9778
9779 if (type == REDIRECT_TYPE_NONE) {
9780 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9781 return NULL;
9782 }
9783
9784 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9785 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009786 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009787
9788 if (!use_fmt) {
9789 /* old-style static redirect rule */
9790 rule->rdr_str = strdup(destination);
9791 rule->rdr_len = strlen(destination);
9792 }
9793 else {
9794 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009795
9796 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9797 * if prefix == "/", we don't want to add anything, otherwise it
9798 * makes it hard for the user to configure a self-redirection.
9799 */
Godbachd9722032014-12-18 15:44:58 +08009800 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009801 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009802 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009803 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9804 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009805 free(curproxy->conf.lfs_file);
9806 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9807 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009808 }
9809 }
9810
Willy Tarreau4baae242012-12-27 12:00:31 +01009811 if (cookie) {
9812 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9813 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9814 */
9815 rule->cookie_len = strlen(cookie);
9816 if (cookie_set) {
9817 rule->cookie_str = malloc(rule->cookie_len + 10);
9818 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9819 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9820 rule->cookie_len += 9;
9821 } else {
9822 rule->cookie_str = malloc(rule->cookie_len + 21);
9823 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9824 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9825 rule->cookie_len += 20;
9826 }
9827 }
9828 rule->type = type;
9829 rule->code = code;
9830 rule->flags = flags;
9831 LIST_INIT(&rule->list);
9832 return rule;
9833
9834 missing_arg:
9835 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9836 return NULL;
9837}
9838
Willy Tarreau8797c062007-05-07 00:55:35 +02009839/************************************************************************/
9840/* The code below is dedicated to ACL parsing and matching */
9841/************************************************************************/
9842
9843
Willy Tarreau14174bc2012-04-16 14:34:04 +02009844/* This function ensures that the prerequisites for an L7 fetch are ready,
9845 * which means that a request or response is ready. If some data is missing,
9846 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009847 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9848 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009849 *
9850 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009851 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9852 * decide whether or not an HTTP message is present ;
9853 * 0 if the requested data cannot be fetched or if it is certain that
9854 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009855 * 1 if an HTTP message is ready
9856 */
9857static int
Willy Tarreau87b09662015-04-03 00:22:06 +02009858smp_prefetch_http(struct proxy *px, struct stream *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009859 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009860{
9861 struct http_txn *txn = l7;
9862 struct http_msg *msg = &txn->req;
9863
9864 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9865 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9866 */
9867
9868 if (unlikely(!s || !txn))
9869 return 0;
9870
9871 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009872 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009873
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009874 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009875 /* If the buffer does not leave enough free space at the end,
9876 * we must first realign it.
9877 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009878 if (s->req.buf->p > s->req.buf->data &&
9879 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9880 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009881
Willy Tarreau14174bc2012-04-16 14:34:04 +02009882 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009883 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009884 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009885
9886 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009887 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009888 http_msg_analyzer(msg, &txn->hdr_idx);
9889
9890 /* Still no valid request ? */
9891 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009892 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009893 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009894 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009895 }
9896 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009897 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009898 return 0;
9899 }
9900
9901 /* OK we just got a valid HTTP request. We have some minor
9902 * preparation to perform so that further checks can rely
9903 * on HTTP tests.
9904 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009905
9906 /* If the request was parsed but was too large, we must absolutely
9907 * return an error so that it is not processed. At the moment this
9908 * cannot happen, but if the parsers are to change in the future,
9909 * we want this check to be maintained.
9910 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009911 if (unlikely(s->req.buf->i + s->req.buf->p >
9912 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009913 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009914 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009915 return 1;
9916 }
9917
Willy Tarreau9b28e032012-10-12 23:49:43 +02009918 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009919 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9920 s->flags |= SN_REDIRECTABLE;
9921
Willy Tarreau506d0502013-07-06 13:29:24 +02009922 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9923 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009924 }
9925
Willy Tarreau506d0502013-07-06 13:29:24 +02009926 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009927 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009928 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009929
9930 /* otherwise everything's ready for the request */
9931 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009932 else {
9933 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009934 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9935 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009936 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009937 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009938 }
9939
9940 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009941 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009942 return 1;
9943}
Willy Tarreau8797c062007-05-07 00:55:35 +02009944
Willy Tarreau6c616e02014-06-25 16:56:41 +02009945/* Note: these functinos *do* modify the sample. Even in case of success, at
9946 * least the type and uint value are modified.
9947 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009948#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009949 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 +02009950
Willy Tarreau24e32d82012-04-23 23:55:44 +02009951#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009952 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 +02009953
Willy Tarreau8797c062007-05-07 00:55:35 +02009954
9955/* 1. Check on METHOD
9956 * We use the pre-parsed method if it is known, and store its number as an
9957 * integer. If it is unknown, we use the pointer and the length.
9958 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009959static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009960{
9961 int len, meth;
9962
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009963 len = strlen(text);
9964 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009965
9966 pattern->val.i = meth;
9967 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009968 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009969 pattern->len = len;
9970 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009971 else {
9972 pattern->ptr.str = NULL;
9973 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009974 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009975 return 1;
9976}
9977
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009978/* This function fetches the method of current HTTP request and stores
9979 * it in the global pattern struct as a chunk. There are two possibilities :
9980 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9981 * in <len> and <ptr> is NULL ;
9982 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9983 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009984 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009985 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009986static int
Willy Tarreau87b09662015-04-03 00:22:06 +02009987smp_fetch_meth(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01009988 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009989{
9990 int meth;
9991 struct http_txn *txn = l7;
9992
Willy Tarreau24e32d82012-04-23 23:55:44 +02009993 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009994
Willy Tarreau8797c062007-05-07 00:55:35 +02009995 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009996 smp->type = SMP_T_METH;
9997 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009998 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009999 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10000 /* ensure the indexes are not affected */
10001 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010002 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010003 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10004 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010005 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010006 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010007 return 1;
10008}
10009
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010010/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010011static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010012{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010013 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010014 struct pattern_list *lst;
10015 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010016
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010017 list_for_each_entry(lst, &expr->patterns, list) {
10018 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010019
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010020 /* well-known method */
10021 if (pattern->val.i != HTTP_METH_OTHER) {
10022 if (smp->data.meth.meth == pattern->val.i)
10023 return pattern;
10024 else
10025 continue;
10026 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010027
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010028 /* Other method, we must compare the strings */
10029 if (pattern->len != smp->data.meth.str.len)
10030 continue;
10031
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010032 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010033 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10034 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010035 return pattern;
10036 }
10037 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038}
10039
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010040static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010041smp_fetch_rqver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010042 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010043{
10044 struct http_txn *txn = l7;
10045 char *ptr;
10046 int len;
10047
Willy Tarreauc0239e02012-04-16 14:42:55 +020010048 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010049
Willy Tarreau8797c062007-05-07 00:55:35 +020010050 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010051 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010052
10053 while ((len-- > 0) && (*ptr++ != '/'));
10054 if (len <= 0)
10055 return 0;
10056
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010057 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010058 smp->data.str.str = ptr;
10059 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010060
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010061 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010062 return 1;
10063}
10064
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010065static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010066smp_fetch_stver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010067 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010068{
10069 struct http_txn *txn = l7;
10070 char *ptr;
10071 int len;
10072
Willy Tarreauc0239e02012-04-16 14:42:55 +020010073 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010074
Willy Tarreauf26b2522012-12-14 08:33:14 +010010075 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10076 return 0;
10077
Willy Tarreau8797c062007-05-07 00:55:35 +020010078 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010079 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010080
10081 while ((len-- > 0) && (*ptr++ != '/'));
10082 if (len <= 0)
10083 return 0;
10084
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010085 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010086 smp->data.str.str = ptr;
10087 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010088
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010089 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010090 return 1;
10091}
10092
10093/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010094static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010095smp_fetch_stcode(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010096 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010097{
10098 struct http_txn *txn = l7;
10099 char *ptr;
10100 int len;
10101
Willy Tarreauc0239e02012-04-16 14:42:55 +020010102 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010103
Willy Tarreauf26b2522012-12-14 08:33:14 +010010104 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10105 return 0;
10106
Willy Tarreau8797c062007-05-07 00:55:35 +020010107 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010108 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010109
Willy Tarreauf853c462012-04-23 18:53:56 +020010110 smp->type = SMP_T_UINT;
10111 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010112 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010113 return 1;
10114}
10115
10116/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010117static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010118smp_fetch_url(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010119 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010120{
10121 struct http_txn *txn = l7;
10122
Willy Tarreauc0239e02012-04-16 14:42:55 +020010123 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010124
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010125 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010126 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010127 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010128 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010129 return 1;
10130}
10131
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010132static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010133smp_fetch_url_ip(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010134 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010135{
10136 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010137 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010138
Willy Tarreauc0239e02012-04-16 14:42:55 +020010139 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010140
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010141 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 +020010142 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010143 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010144
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010145 smp->type = SMP_T_IPV4;
10146 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010147 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010148 return 1;
10149}
10150
10151static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010152smp_fetch_url_port(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010153 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010154{
10155 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010156 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010157
Willy Tarreauc0239e02012-04-16 14:42:55 +020010158 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010159
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010160 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 +020010161 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10162 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010163
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010164 smp->type = SMP_T_UINT;
10165 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010166 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010167 return 1;
10168}
10169
Willy Tarreau185b5c42012-04-26 15:11:51 +020010170/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10171 * Accepts an optional argument of type string containing the header field name,
10172 * and an optional argument of type signed or unsigned integer to request an
10173 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010174 * headers are considered from the first one. It does not stop on commas and
10175 * returns full lines instead (useful for User-Agent or Date for example).
10176 */
10177static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010178smp_fetch_fhdr(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010179 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010180{
10181 struct http_txn *txn = l7;
10182 struct hdr_idx *idx = &txn->hdr_idx;
10183 struct hdr_ctx *ctx = smp->ctx.a[0];
10184 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10185 int occ = 0;
10186 const char *name_str = NULL;
10187 int name_len = 0;
10188
10189 if (!ctx) {
10190 /* first call */
10191 ctx = &static_hdr_ctx;
10192 ctx->idx = 0;
10193 smp->ctx.a[0] = ctx;
10194 }
10195
10196 if (args) {
10197 if (args[0].type != ARGT_STR)
10198 return 0;
10199 name_str = args[0].data.str.str;
10200 name_len = args[0].data.str.len;
10201
10202 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10203 occ = args[1].data.uint;
10204 }
10205
10206 CHECK_HTTP_MESSAGE_FIRST();
10207
10208 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10209 /* search for header from the beginning */
10210 ctx->idx = 0;
10211
10212 if (!occ && !(opt & SMP_OPT_ITERATE))
10213 /* no explicit occurrence and single fetch => last header by default */
10214 occ = -1;
10215
10216 if (!occ)
10217 /* prepare to report multiple occurrences for ACL fetches */
10218 smp->flags |= SMP_F_NOT_LAST;
10219
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010220 smp->type = SMP_T_STR;
10221 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010222 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10223 return 1;
10224
10225 smp->flags &= ~SMP_F_NOT_LAST;
10226 return 0;
10227}
10228
10229/* 6. Check on HTTP header count. The number of occurrences is returned.
10230 * Accepts exactly 1 argument of type string. It does not stop on commas and
10231 * returns full lines instead (useful for User-Agent or Date for example).
10232 */
10233static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010234smp_fetch_fhdr_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010235 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010236{
10237 struct http_txn *txn = l7;
10238 struct hdr_idx *idx = &txn->hdr_idx;
10239 struct hdr_ctx ctx;
10240 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10241 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010242 const char *name = NULL;
10243 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010244
Willy Tarreau601a4d12015-04-01 19:16:09 +020010245 if (args && args->type == ARGT_STR) {
10246 name = args->data.str.str;
10247 len = args->data.str.len;
10248 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010249
10250 CHECK_HTTP_MESSAGE_FIRST();
10251
10252 ctx.idx = 0;
10253 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010254 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010255 cnt++;
10256
10257 smp->type = SMP_T_UINT;
10258 smp->data.uint = cnt;
10259 smp->flags = SMP_F_VOL_HDR;
10260 return 1;
10261}
10262
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010263static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010264smp_fetch_hdr_names(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010265 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010266{
10267 struct http_txn *txn = l7;
10268 struct hdr_idx *idx = &txn->hdr_idx;
10269 struct hdr_ctx ctx;
10270 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10271 struct chunk *temp;
10272 char del = ',';
10273
10274 if (args && args->type == ARGT_STR)
10275 del = *args[0].data.str.str;
10276
10277 CHECK_HTTP_MESSAGE_FIRST();
10278
10279 temp = get_trash_chunk();
10280
10281 ctx.idx = 0;
10282 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10283 if (temp->len)
10284 temp->str[temp->len++] = del;
10285 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10286 temp->len += ctx.del;
10287 }
10288
10289 smp->type = SMP_T_STR;
10290 smp->data.str.str = temp->str;
10291 smp->data.str.len = temp->len;
10292 smp->flags = SMP_F_VOL_HDR;
10293 return 1;
10294}
10295
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010296/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10297 * Accepts an optional argument of type string containing the header field name,
10298 * and an optional argument of type signed or unsigned integer to request an
10299 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010300 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010301 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010302static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010303smp_fetch_hdr(struct proxy *px, struct stream *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 Tarreau33a7e692007-06-10 19:45:56 +020010305{
10306 struct http_txn *txn = l7;
10307 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010308 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010309 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010310 int occ = 0;
10311 const char *name_str = NULL;
10312 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010313
Willy Tarreaua890d072013-04-02 12:01:06 +020010314 if (!ctx) {
10315 /* first call */
10316 ctx = &static_hdr_ctx;
10317 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010318 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010319 }
10320
Willy Tarreau185b5c42012-04-26 15:11:51 +020010321 if (args) {
10322 if (args[0].type != ARGT_STR)
10323 return 0;
10324 name_str = args[0].data.str.str;
10325 name_len = args[0].data.str.len;
10326
10327 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10328 occ = args[1].data.uint;
10329 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010330
Willy Tarreaue333ec92012-04-16 16:26:40 +020010331 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010332
Willy Tarreau185b5c42012-04-26 15:11:51 +020010333 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010334 /* search for header from the beginning */
10335 ctx->idx = 0;
10336
Willy Tarreau185b5c42012-04-26 15:11:51 +020010337 if (!occ && !(opt & SMP_OPT_ITERATE))
10338 /* no explicit occurrence and single fetch => last header by default */
10339 occ = -1;
10340
10341 if (!occ)
10342 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010343 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010344
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010345 smp->type = SMP_T_STR;
10346 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010347 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 +020010348 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010349
Willy Tarreau37406352012-04-23 16:16:37 +020010350 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010351 return 0;
10352}
10353
Willy Tarreauc11416f2007-06-17 16:58:38 +020010354/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010355 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010356 */
10357static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010358smp_fetch_hdr_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010359 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010360{
10361 struct http_txn *txn = l7;
10362 struct hdr_idx *idx = &txn->hdr_idx;
10363 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010364 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010365 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010366 const char *name = NULL;
10367 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010368
Willy Tarreau601a4d12015-04-01 19:16:09 +020010369 if (args && args->type == ARGT_STR) {
10370 name = args->data.str.str;
10371 len = args->data.str.len;
10372 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010373
Willy Tarreaue333ec92012-04-16 16:26:40 +020010374 CHECK_HTTP_MESSAGE_FIRST();
10375
Willy Tarreau33a7e692007-06-10 19:45:56 +020010376 ctx.idx = 0;
10377 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010378 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010379 cnt++;
10380
Willy Tarreauf853c462012-04-23 18:53:56 +020010381 smp->type = SMP_T_UINT;
10382 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010383 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010384 return 1;
10385}
10386
Willy Tarreau185b5c42012-04-26 15:11:51 +020010387/* Fetch an HTTP header's integer value. The integer value is returned. It
10388 * takes a mandatory argument of type string and an optional one of type int
10389 * to designate a specific occurrence. It returns an unsigned integer, which
10390 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010391 */
10392static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010393smp_fetch_hdr_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010394 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010395{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010396 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010397
Willy Tarreauf853c462012-04-23 18:53:56 +020010398 if (ret > 0) {
10399 smp->type = SMP_T_UINT;
10400 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10401 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010402
Willy Tarreaud53e2422012-04-16 17:21:11 +020010403 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010404}
10405
Cyril Bonté69fa9922012-10-25 00:01:06 +020010406/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10407 * and an optional one of type int to designate a specific occurrence.
10408 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010409 */
10410static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010411smp_fetch_hdr_ip(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010412 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010413{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010414 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010415
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010416 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010417 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10418 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010419 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010420 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010421 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010422 if (smp->data.str.len < temp->size - 1) {
10423 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10424 temp->str[smp->data.str.len] = '\0';
10425 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10426 smp->type = SMP_T_IPV6;
10427 break;
10428 }
10429 }
10430 }
10431
Willy Tarreaud53e2422012-04-16 17:21:11 +020010432 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010433 if (!(smp->flags & SMP_F_NOT_LAST))
10434 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010435 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010436 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010437}
10438
Willy Tarreau737b0c12007-06-10 21:28:46 +020010439/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10440 * the first '/' after the possible hostname, and ends before the possible '?'.
10441 */
10442static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010443smp_fetch_path(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010444 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010445{
10446 struct http_txn *txn = l7;
10447 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010448
Willy Tarreauc0239e02012-04-16 14:42:55 +020010449 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010450
Willy Tarreau9b28e032012-10-12 23:49:43 +020010451 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010452 ptr = http_get_path(txn);
10453 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010454 return 0;
10455
10456 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010457 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010458 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010459
10460 while (ptr < end && *ptr != '?')
10461 ptr++;
10462
Willy Tarreauf853c462012-04-23 18:53:56 +020010463 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010464 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010465 return 1;
10466}
10467
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010468/* This produces a concatenation of the first occurrence of the Host header
10469 * followed by the path component if it begins with a slash ('/'). This means
10470 * that '*' will not be added, resulting in exactly the first Host entry.
10471 * If no Host header is found, then the path is returned as-is. The returned
10472 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010473 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010474 */
10475static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010476smp_fetch_base(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010477 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010478{
10479 struct http_txn *txn = l7;
10480 char *ptr, *end, *beg;
10481 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010482 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010483
10484 CHECK_HTTP_MESSAGE_FIRST();
10485
10486 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010487 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010488 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010489
10490 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010491 temp = get_trash_chunk();
10492 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010493 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010494 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010495 smp->data.str.len = ctx.vlen;
10496
10497 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010498 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010499 beg = http_get_path(txn);
10500 if (!beg)
10501 beg = end;
10502
10503 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10504
10505 if (beg < ptr && *beg == '/') {
10506 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10507 smp->data.str.len += ptr - beg;
10508 }
10509
10510 smp->flags = SMP_F_VOL_1ST;
10511 return 1;
10512}
10513
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010514/* This produces a 32-bit hash of the concatenation of the first occurrence of
10515 * the Host header followed by the path component if it begins with a slash ('/').
10516 * This means that '*' will not be added, resulting in exactly the first Host
10517 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010518 * is hashed using the path hash followed by a full avalanche hash and provides a
10519 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010520 * high-traffic sites without having to store whole paths.
10521 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010522int
Willy Tarreau87b09662015-04-03 00:22:06 +020010523smp_fetch_base32(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010524 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010525{
10526 struct http_txn *txn = l7;
10527 struct hdr_ctx ctx;
10528 unsigned int hash = 0;
10529 char *ptr, *beg, *end;
10530 int len;
10531
10532 CHECK_HTTP_MESSAGE_FIRST();
10533
10534 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010535 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010536 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10537 ptr = ctx.line + ctx.val;
10538 len = ctx.vlen;
10539 while (len--)
10540 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10541 }
10542
10543 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010544 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010545 beg = http_get_path(txn);
10546 if (!beg)
10547 beg = end;
10548
10549 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10550
10551 if (beg < ptr && *beg == '/') {
10552 while (beg < ptr)
10553 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10554 }
10555 hash = full_hash(hash);
10556
10557 smp->type = SMP_T_UINT;
10558 smp->data.uint = hash;
10559 smp->flags = SMP_F_VOL_1ST;
10560 return 1;
10561}
10562
Willy Tarreau4a550602012-12-09 14:53:32 +010010563/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010564 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10565 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10566 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010567 * that in environments where IPv6 is insignificant, truncating the output to
10568 * 8 bytes would still work.
10569 */
10570static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010571smp_fetch_base32_src(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010572 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010573{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010574 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010575 struct connection *cli_conn = objt_conn(l4->si[0].end);
10576
10577 if (!cli_conn)
10578 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010579
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010580 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010581 return 0;
10582
Willy Tarreau47ca5452012-12-23 20:22:19 +010010583 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010584 *(unsigned int *)temp->str = htonl(smp->data.uint);
10585 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010586
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010587 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010588 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010589 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010590 temp->len += 4;
10591 break;
10592 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010593 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010594 temp->len += 16;
10595 break;
10596 default:
10597 return 0;
10598 }
10599
10600 smp->data.str = *temp;
10601 smp->type = SMP_T_BIN;
10602 return 1;
10603}
10604
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010605/* Extracts the query string, which comes after the question mark '?'. If no
10606 * question mark is found, nothing is returned. Otherwise it returns a sample
10607 * of type string carrying the whole query string.
10608 */
10609static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010610smp_fetch_query(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010611 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010612{
10613 struct http_txn *txn = l7;
10614 char *ptr, *end;
10615
10616 CHECK_HTTP_MESSAGE_FIRST();
10617
10618 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10619 end = ptr + txn->req.sl.rq.u_l;
10620
10621 /* look up the '?' */
10622 do {
10623 if (ptr == end)
10624 return 0;
10625 } while (*ptr++ != '?');
10626
10627 smp->type = SMP_T_STR;
10628 smp->data.str.str = ptr;
10629 smp->data.str.len = end - ptr;
10630 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10631 return 1;
10632}
10633
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010634static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010635smp_fetch_proto_http(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010636 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010637{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010638 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10639 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10640 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010641
Willy Tarreau24e32d82012-04-23 23:55:44 +020010642 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010643
Willy Tarreauf853c462012-04-23 18:53:56 +020010644 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010645 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010646 return 1;
10647}
10648
Willy Tarreau7f18e522010-10-22 20:04:13 +020010649/* return a valid test if the current request is the first one on the connection */
10650static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010651smp_fetch_http_first_req(struct proxy *px, struct stream *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010652 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010653{
10654 if (!s)
10655 return 0;
10656
Willy Tarreauf853c462012-04-23 18:53:56 +020010657 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010658 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010659 return 1;
10660}
10661
Willy Tarreau34db1082012-04-19 17:16:54 +020010662/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010663static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010664smp_fetch_http_auth(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010665 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010666{
10667
Willy Tarreau24e32d82012-04-23 23:55:44 +020010668 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010669 return 0;
10670
Willy Tarreauc0239e02012-04-16 14:42:55 +020010671 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010672
Willy Tarreauc0239e02012-04-16 14:42:55 +020010673 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010674 return 0;
10675
Willy Tarreauf853c462012-04-23 18:53:56 +020010676 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010677 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010678 return 1;
10679}
Willy Tarreau8797c062007-05-07 00:55:35 +020010680
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010681/* Accepts exactly 1 argument of type userlist */
10682static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010683smp_fetch_http_auth_grp(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010684 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010685{
10686
10687 if (!args || args->type != ARGT_USR)
10688 return 0;
10689
10690 CHECK_HTTP_MESSAGE_FIRST();
10691
10692 if (!get_http_auth(l4))
10693 return 0;
10694
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010695 /* if the user does not belong to the userlist or has a wrong password,
10696 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010697 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010698 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010699 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10700 return 0;
10701
10702 /* pat_match_auth() will need the user list */
10703 smp->ctx.a[0] = args->data.usr;
10704
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010705 smp->type = SMP_T_STR;
10706 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010707 smp->data.str.str = l4->txn.auth.user;
10708 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010709
10710 return 1;
10711}
10712
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010713/* Try to find the next occurrence of a cookie name in a cookie header value.
10714 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10715 * the cookie value is returned into *value and *value_l, and the function
10716 * returns a pointer to the next pointer to search from if the value was found.
10717 * Otherwise if the cookie was not found, NULL is returned and neither value
10718 * nor value_l are touched. The input <hdr> string should first point to the
10719 * header's value, and the <hdr_end> pointer must point to the first character
10720 * not part of the value. <list> must be non-zero if value may represent a list
10721 * of values (cookie headers). This makes it faster to abort parsing when no
10722 * list is expected.
10723 */
10724static char *
10725extract_cookie_value(char *hdr, const char *hdr_end,
10726 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010727 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010728{
10729 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10730 char *next;
10731
10732 /* we search at least a cookie name followed by an equal, and more
10733 * generally something like this :
10734 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10735 */
10736 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10737 /* Iterate through all cookies on this line */
10738
10739 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10740 att_beg++;
10741
10742 /* find att_end : this is the first character after the last non
10743 * space before the equal. It may be equal to hdr_end.
10744 */
10745 equal = att_end = att_beg;
10746
10747 while (equal < hdr_end) {
10748 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10749 break;
10750 if (http_is_spht[(unsigned char)*equal++])
10751 continue;
10752 att_end = equal;
10753 }
10754
10755 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10756 * is between <att_beg> and <equal>, both may be identical.
10757 */
10758
10759 /* look for end of cookie if there is an equal sign */
10760 if (equal < hdr_end && *equal == '=') {
10761 /* look for the beginning of the value */
10762 val_beg = equal + 1;
10763 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10764 val_beg++;
10765
10766 /* find the end of the value, respecting quotes */
10767 next = find_cookie_value_end(val_beg, hdr_end);
10768
10769 /* make val_end point to the first white space or delimitor after the value */
10770 val_end = next;
10771 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10772 val_end--;
10773 } else {
10774 val_beg = val_end = next = equal;
10775 }
10776
10777 /* We have nothing to do with attributes beginning with '$'. However,
10778 * they will automatically be removed if a header before them is removed,
10779 * since they're supposed to be linked together.
10780 */
10781 if (*att_beg == '$')
10782 continue;
10783
10784 /* Ignore cookies with no equal sign */
10785 if (equal == next)
10786 continue;
10787
10788 /* Now we have the cookie name between att_beg and att_end, and
10789 * its value between val_beg and val_end.
10790 */
10791
10792 if (att_end - att_beg == cookie_name_l &&
10793 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10794 /* let's return this value and indicate where to go on from */
10795 *value = val_beg;
10796 *value_l = val_end - val_beg;
10797 return next + 1;
10798 }
10799
10800 /* Set-Cookie headers only have the name in the first attr=value part */
10801 if (!list)
10802 break;
10803 }
10804
10805 return NULL;
10806}
10807
William Lallemanda43ba4e2014-01-28 18:14:25 +010010808/* Fetch a captured HTTP request header. The index is the position of
10809 * the "capture" option in the configuration file
10810 */
10811static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010812smp_fetch_capture_header_req(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010813 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010814{
10815 struct proxy *fe = l4->fe;
10816 struct http_txn *txn = l7;
10817 int idx;
10818
10819 if (!args || args->type != ARGT_UINT)
10820 return 0;
10821
10822 idx = args->data.uint;
10823
10824 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10825 return 0;
10826
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010827 smp->type = SMP_T_STR;
10828 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010829 smp->data.str.str = txn->req.cap[idx];
10830 smp->data.str.len = strlen(txn->req.cap[idx]);
10831
10832 return 1;
10833}
10834
10835/* Fetch a captured HTTP response header. The index is the position of
10836 * the "capture" option in the configuration file
10837 */
10838static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010839smp_fetch_capture_header_res(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010840 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010841{
10842 struct proxy *fe = l4->fe;
10843 struct http_txn *txn = l7;
10844 int idx;
10845
10846 if (!args || args->type != ARGT_UINT)
10847 return 0;
10848
10849 idx = args->data.uint;
10850
10851 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10852 return 0;
10853
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010854 smp->type = SMP_T_STR;
10855 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010856 smp->data.str.str = txn->rsp.cap[idx];
10857 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10858
10859 return 1;
10860}
10861
William Lallemand65ad6e12014-01-31 15:08:02 +010010862/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10863static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010864smp_fetch_capture_req_method(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010865 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010866{
10867 struct chunk *temp;
10868 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010869 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010870
10871 if (!txn->uri)
10872 return 0;
10873
William Lallemand96a77852014-02-05 00:30:02 +010010874 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010875
William Lallemand96a77852014-02-05 00:30:02 +010010876 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10877 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010878
William Lallemand96a77852014-02-05 00:30:02 +010010879 temp = get_trash_chunk();
10880 temp->str = txn->uri;
10881 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010882 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010883 smp->type = SMP_T_STR;
10884 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010885
10886 return 1;
10887
10888}
10889
10890/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10891static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010892smp_fetch_capture_req_uri(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010893 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010894{
10895 struct chunk *temp;
10896 struct http_txn *txn = l7;
10897 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010898
10899 if (!txn->uri)
10900 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010901
William Lallemand65ad6e12014-01-31 15:08:02 +010010902 ptr = txn->uri;
10903
10904 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10905 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010906
William Lallemand65ad6e12014-01-31 15:08:02 +010010907 if (!*ptr)
10908 return 0;
10909
10910 ptr++; /* skip the space */
10911
10912 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010913 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010914 if (!ptr)
10915 return 0;
10916 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10917 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010918
10919 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010920 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010921 smp->type = SMP_T_STR;
10922 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010923
10924 return 1;
10925}
10926
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010927/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10928 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10929 */
10930static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010931smp_fetch_capture_req_ver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010932 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010933{
10934 struct http_txn *txn = l7;
10935
10936 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10937 return 0;
10938
10939 if (txn->req.flags & HTTP_MSGF_VER_11)
10940 smp->data.str.str = "HTTP/1.1";
10941 else
10942 smp->data.str.str = "HTTP/1.0";
10943
10944 smp->data.str.len = 8;
10945 smp->type = SMP_T_STR;
10946 smp->flags = SMP_F_CONST;
10947 return 1;
10948
10949}
10950
10951/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10952 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10953 */
10954static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010955smp_fetch_capture_res_ver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010956 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010957{
10958 struct http_txn *txn = l7;
10959
10960 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10961 return 0;
10962
10963 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10964 smp->data.str.str = "HTTP/1.1";
10965 else
10966 smp->data.str.str = "HTTP/1.0";
10967
10968 smp->data.str.len = 8;
10969 smp->type = SMP_T_STR;
10970 smp->flags = SMP_F_CONST;
10971 return 1;
10972
10973}
10974
William Lallemand65ad6e12014-01-31 15:08:02 +010010975
Willy Tarreaue333ec92012-04-16 16:26:40 +020010976/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010977 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010978 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010979 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010980 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010981 * Accepts exactly 1 argument of type string. If the input options indicate
10982 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010983 * The returned sample is of type CSTR. Can be used to parse cookies in other
10984 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010985 */
Willy Tarreau87b09662015-04-03 00:22:06 +020010986int smp_fetch_cookie(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010987 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010988{
10989 struct http_txn *txn = l7;
10990 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010991 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010992 const struct http_msg *msg;
10993 const char *hdr_name;
10994 int hdr_name_len;
10995 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010996 int occ = 0;
10997 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010998
Willy Tarreau24e32d82012-04-23 23:55:44 +020010999 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011000 return 0;
11001
Willy Tarreaua890d072013-04-02 12:01:06 +020011002 if (!ctx) {
11003 /* first call */
11004 ctx = &static_hdr_ctx;
11005 ctx->idx = 0;
11006 smp->ctx.a[2] = ctx;
11007 }
11008
Willy Tarreaue333ec92012-04-16 16:26:40 +020011009 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011010
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011011 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011012 msg = &txn->req;
11013 hdr_name = "Cookie";
11014 hdr_name_len = 6;
11015 } else {
11016 msg = &txn->rsp;
11017 hdr_name = "Set-Cookie";
11018 hdr_name_len = 10;
11019 }
11020
Willy Tarreau28376d62012-04-26 21:26:10 +020011021 if (!occ && !(opt & SMP_OPT_ITERATE))
11022 /* no explicit occurrence and single fetch => last cookie by default */
11023 occ = -1;
11024
11025 /* OK so basically here, either we want only one value and it's the
11026 * last one, or we want to iterate over all of them and we fetch the
11027 * next one.
11028 */
11029
Willy Tarreau9b28e032012-10-12 23:49:43 +020011030 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011031 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011032 /* search for the header from the beginning, we must first initialize
11033 * the search parameters.
11034 */
Willy Tarreau37406352012-04-23 16:16:37 +020011035 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011036 ctx->idx = 0;
11037 }
11038
Willy Tarreau28376d62012-04-26 21:26:10 +020011039 smp->flags |= SMP_F_VOL_HDR;
11040
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011041 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011042 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11043 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011044 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11045 goto out;
11046
Willy Tarreau24e32d82012-04-23 23:55:44 +020011047 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011048 continue;
11049
Willy Tarreau37406352012-04-23 16:16:37 +020011050 smp->ctx.a[0] = ctx->line + ctx->val;
11051 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011052 }
11053
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011054 smp->type = SMP_T_STR;
11055 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011056 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011057 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011058 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011059 &smp->data.str.str,
11060 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011061 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011062 found = 1;
11063 if (occ >= 0) {
11064 /* one value was returned into smp->data.str.{str,len} */
11065 smp->flags |= SMP_F_NOT_LAST;
11066 return 1;
11067 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011068 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011069 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011070 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011071 /* all cookie headers and values were scanned. If we're looking for the
11072 * last occurrence, we may return it now.
11073 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011074 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011075 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011076 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011077}
11078
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011079/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011080 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011081 * multiple cookies may be parsed on the same line. The returned sample is of
11082 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011083 */
11084static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011085smp_fetch_cookie_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011086 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011087{
11088 struct http_txn *txn = l7;
11089 struct hdr_idx *idx = &txn->hdr_idx;
11090 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011091 const struct http_msg *msg;
11092 const char *hdr_name;
11093 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011094 int cnt;
11095 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011096 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011097
Willy Tarreau24e32d82012-04-23 23:55:44 +020011098 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011099 return 0;
11100
Willy Tarreaue333ec92012-04-16 16:26:40 +020011101 CHECK_HTTP_MESSAGE_FIRST();
11102
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011103 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011104 msg = &txn->req;
11105 hdr_name = "Cookie";
11106 hdr_name_len = 6;
11107 } else {
11108 msg = &txn->rsp;
11109 hdr_name = "Set-Cookie";
11110 hdr_name_len = 10;
11111 }
11112
Willy Tarreau9b28e032012-10-12 23:49:43 +020011113 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011114 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115 ctx.idx = 0;
11116 cnt = 0;
11117
11118 while (1) {
11119 /* Note: val_beg == NULL every time we need to fetch a new header */
11120 if (!val_beg) {
11121 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11122 break;
11123
Willy Tarreau24e32d82012-04-23 23:55:44 +020011124 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011125 continue;
11126
11127 val_beg = ctx.line + ctx.val;
11128 val_end = val_beg + ctx.vlen;
11129 }
11130
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011131 smp->type = SMP_T_STR;
11132 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011133 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011134 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011135 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011136 &smp->data.str.str,
11137 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011138 cnt++;
11139 }
11140 }
11141
Willy Tarreaub169eba2013-12-16 15:14:43 +010011142 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011143 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011144 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011145 return 1;
11146}
11147
Willy Tarreau51539362012-05-08 12:46:28 +020011148/* Fetch an cookie's integer value. The integer value is returned. It
11149 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11150 */
11151static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011152smp_fetch_cookie_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011153 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011154{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011155 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011156
11157 if (ret > 0) {
11158 smp->type = SMP_T_UINT;
11159 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11160 }
11161
11162 return ret;
11163}
11164
Willy Tarreau8797c062007-05-07 00:55:35 +020011165/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011166/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011167/************************************************************************/
11168
David Cournapeau16023ee2010-12-23 20:55:41 +090011169/*
11170 * Given a path string and its length, find the position of beginning of the
11171 * query string. Returns NULL if no query string is found in the path.
11172 *
11173 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11174 *
11175 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11176 */
bedis4c75cca2012-10-05 08:38:24 +020011177static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011178{
11179 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011180
bedis4c75cca2012-10-05 08:38:24 +020011181 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011182 return p ? p + 1 : NULL;
11183}
11184
bedis4c75cca2012-10-05 08:38:24 +020011185static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011186{
bedis4c75cca2012-10-05 08:38:24 +020011187 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011188}
11189
11190/*
11191 * Given a url parameter, find the starting position of the first occurence,
11192 * or NULL if the parameter is not found.
11193 *
11194 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11195 * the function will return query_string+8.
11196 */
11197static char*
11198find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011199 char* url_param_name, size_t url_param_name_l,
11200 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011201{
11202 char *pos, *last;
11203
11204 pos = query_string;
11205 last = query_string + query_string_l - url_param_name_l - 1;
11206
11207 while (pos <= last) {
11208 if (pos[url_param_name_l] == '=') {
11209 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11210 return pos;
11211 pos += url_param_name_l + 1;
11212 }
bedis4c75cca2012-10-05 08:38:24 +020011213 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011214 pos++;
11215 pos++;
11216 }
11217 return NULL;
11218}
11219
11220/*
11221 * Given a url parameter name, returns its value and size into *value and
11222 * *value_l respectively, and returns non-zero. If the parameter is not found,
11223 * zero is returned and value/value_l are not touched.
11224 */
11225static int
11226find_url_param_value(char* path, size_t path_l,
11227 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011228 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011229{
11230 char *query_string, *qs_end;
11231 char *arg_start;
11232 char *value_start, *value_end;
11233
bedis4c75cca2012-10-05 08:38:24 +020011234 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011235 if (!query_string)
11236 return 0;
11237
11238 qs_end = path + path_l;
11239 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011240 url_param_name, url_param_name_l,
11241 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011242 if (!arg_start)
11243 return 0;
11244
11245 value_start = arg_start + url_param_name_l + 1;
11246 value_end = value_start;
11247
bedis4c75cca2012-10-05 08:38:24 +020011248 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011249 value_end++;
11250
11251 *value = value_start;
11252 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011253 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011254}
11255
11256static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011257smp_fetch_url_param(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011258 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011259{
bedis4c75cca2012-10-05 08:38:24 +020011260 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011261 struct http_txn *txn = l7;
11262 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011263
bedis4c75cca2012-10-05 08:38:24 +020011264 if (!args || args[0].type != ARGT_STR ||
11265 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011266 return 0;
11267
11268 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011269
bedis4c75cca2012-10-05 08:38:24 +020011270 if (args[1].type)
11271 delim = *args[1].data.str.str;
11272
Willy Tarreau9b28e032012-10-12 23:49:43 +020011273 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011274 args->data.str.str, args->data.str.len,
11275 &smp->data.str.str, &smp->data.str.len,
11276 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011277 return 0;
11278
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011279 smp->type = SMP_T_STR;
11280 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011281 return 1;
11282}
11283
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011284/* Return the signed integer value for the specified url parameter (see url_param
11285 * above).
11286 */
11287static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011288smp_fetch_url_param_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011289 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011290{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011291 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011292
11293 if (ret > 0) {
11294 smp->type = SMP_T_UINT;
11295 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11296 }
11297
11298 return ret;
11299}
11300
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011301/* This produces a 32-bit hash of the concatenation of the first occurrence of
11302 * the Host header followed by the path component if it begins with a slash ('/').
11303 * This means that '*' will not be added, resulting in exactly the first Host
11304 * entry. If no Host header is found, then the path is used. The resulting value
11305 * is hashed using the url hash followed by a full avalanche hash and provides a
11306 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11307 * high-traffic sites without having to store whole paths.
11308 * this differs from the base32 functions in that it includes the url parameters
11309 * as well as the path
11310 */
11311static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011312smp_fetch_url32(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011313 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011314{
11315 struct http_txn *txn = l7;
11316 struct hdr_ctx ctx;
11317 unsigned int hash = 0;
11318 char *ptr, *beg, *end;
11319 int len;
11320
11321 CHECK_HTTP_MESSAGE_FIRST();
11322
11323 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011324 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011325 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11326 ptr = ctx.line + ctx.val;
11327 len = ctx.vlen;
11328 while (len--)
11329 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11330 }
11331
11332 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011333 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 +000011334 beg = http_get_path(txn);
11335 if (!beg)
11336 beg = end;
11337
11338 for (ptr = beg; ptr < end ; ptr++);
11339
11340 if (beg < ptr && *beg == '/') {
11341 while (beg < ptr)
11342 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11343 }
11344 hash = full_hash(hash);
11345
11346 smp->type = SMP_T_UINT;
11347 smp->data.uint = hash;
11348 smp->flags = SMP_F_VOL_1ST;
11349 return 1;
11350}
11351
11352/* This concatenates the source address with the 32-bit hash of the Host and
11353 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11354 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11355 * on the source address length. The URL hash is stored before the address so
11356 * that in environments where IPv6 is insignificant, truncating the output to
11357 * 8 bytes would still work.
11358 */
11359static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011360smp_fetch_url32_src(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011361 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011362{
11363 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011364 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011365
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011366 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011367 return 0;
11368
11369 temp = get_trash_chunk();
11370 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11371 temp->len += sizeof(smp->data.uint);
11372
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011373 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011374 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011375 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011376 temp->len += 4;
11377 break;
11378 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011379 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011380 temp->len += 16;
11381 break;
11382 default:
11383 return 0;
11384 }
11385
11386 smp->data.str = *temp;
11387 smp->type = SMP_T_BIN;
11388 return 1;
11389}
11390
Willy Tarreau185b5c42012-04-26 15:11:51 +020011391/* This function is used to validate the arguments passed to any "hdr" fetch
11392 * keyword. These keywords support an optional positive or negative occurrence
11393 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11394 * is assumed that the types are already the correct ones. Returns 0 on error,
11395 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11396 * error message in case of error, that the caller is responsible for freeing.
11397 * The initial location must either be freeable or NULL.
11398 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011399int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011400{
11401 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011402 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011403 return 0;
11404 }
11405 return 1;
11406}
11407
Willy Tarreau276fae92013-07-25 14:36:01 +020011408/* takes an UINT value on input supposed to represent the time since EPOCH,
11409 * adds an optional offset found in args[0] and emits a string representing
11410 * the date in RFC-1123/5322 format.
11411 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011412static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011413 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011414{
11415 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11416 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11417 struct chunk *temp;
11418 struct tm *tm;
11419 time_t curr_date = smp->data.uint;
11420
11421 /* add offset */
11422 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11423 curr_date += args[0].data.sint;
11424
11425 tm = gmtime(&curr_date);
11426
11427 temp = get_trash_chunk();
11428 temp->len = snprintf(temp->str, temp->size - temp->len,
11429 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11430 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11431 tm->tm_hour, tm->tm_min, tm->tm_sec);
11432
11433 smp->data.str = *temp;
11434 smp->type = SMP_T_STR;
11435 return 1;
11436}
11437
Thierry FOURNIERad903512014-04-11 17:51:01 +020011438/* Match language range with language tag. RFC2616 14.4:
11439 *
11440 * A language-range matches a language-tag if it exactly equals
11441 * the tag, or if it exactly equals a prefix of the tag such
11442 * that the first tag character following the prefix is "-".
11443 *
11444 * Return 1 if the strings match, else return 0.
11445 */
11446static inline int language_range_match(const char *range, int range_len,
11447 const char *tag, int tag_len)
11448{
11449 const char *end = range + range_len;
11450 const char *tend = tag + tag_len;
11451 while (range < end) {
11452 if (*range == '-' && tag == tend)
11453 return 1;
11454 if (*range != *tag || tag == tend)
11455 return 0;
11456 range++;
11457 tag++;
11458 }
11459 /* Return true only if the last char of the tag is matched. */
11460 return tag == tend;
11461}
11462
11463/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011464static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011465 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011466{
11467 const char *al = smp->data.str.str;
11468 const char *end = al + smp->data.str.len;
11469 const char *token;
11470 int toklen;
11471 int qvalue;
11472 const char *str;
11473 const char *w;
11474 int best_q = 0;
11475
11476 /* Set the constant to the sample, because the output of the
11477 * function will be peek in the constant configuration string.
11478 */
11479 smp->flags |= SMP_F_CONST;
11480 smp->data.str.size = 0;
11481 smp->data.str.str = "";
11482 smp->data.str.len = 0;
11483
11484 /* Parse the accept language */
11485 while (1) {
11486
11487 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011488 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011489 al++;
11490 if (al >= end)
11491 break;
11492
11493 /* Start of the fisrt word. */
11494 token = al;
11495
11496 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011497 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011498 al++;
11499 if (al == token)
11500 goto expect_comma;
11501
11502 /* Length of the token. */
11503 toklen = al - token;
11504 qvalue = 1000;
11505
11506 /* Check if the token exists in the list. If the token not exists,
11507 * jump to the next token.
11508 */
11509 str = args[0].data.str.str;
11510 w = str;
11511 while (1) {
11512 if (*str == ';' || *str == '\0') {
11513 if (language_range_match(token, toklen, w, str-w))
11514 goto look_for_q;
11515 if (*str == '\0')
11516 goto expect_comma;
11517 w = str + 1;
11518 }
11519 str++;
11520 }
11521 goto expect_comma;
11522
11523look_for_q:
11524
11525 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011526 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011527 al++;
11528 if (al >= end)
11529 goto process_value;
11530
11531 /* If ',' is found, process the result */
11532 if (*al == ',')
11533 goto process_value;
11534
11535 /* If the character is different from ';', look
11536 * for the end of the header part in best effort.
11537 */
11538 if (*al != ';')
11539 goto expect_comma;
11540
11541 /* Assumes that the char is ';', now expect "q=". */
11542 al++;
11543
11544 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011545 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011546 al++;
11547 if (al >= end)
11548 goto process_value;
11549
11550 /* Expect 'q'. If no 'q', continue in best effort */
11551 if (*al != 'q')
11552 goto process_value;
11553 al++;
11554
11555 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011556 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011557 al++;
11558 if (al >= end)
11559 goto process_value;
11560
11561 /* Expect '='. If no '=', continue in best effort */
11562 if (*al != '=')
11563 goto process_value;
11564 al++;
11565
11566 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011567 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011568 al++;
11569 if (al >= end)
11570 goto process_value;
11571
11572 /* Parse the q value. */
11573 qvalue = parse_qvalue(al, &al);
11574
11575process_value:
11576
11577 /* If the new q value is the best q value, then store the associated
11578 * language in the response. If qvalue is the biggest value (1000),
11579 * break the process.
11580 */
11581 if (qvalue > best_q) {
11582 smp->data.str.str = (char *)w;
11583 smp->data.str.len = str - w;
11584 if (qvalue >= 1000)
11585 break;
11586 best_q = qvalue;
11587 }
11588
11589expect_comma:
11590
11591 /* Expect comma or end. If the end is detected, quit the loop. */
11592 while (al < end && *al != ',')
11593 al++;
11594 if (al >= end)
11595 break;
11596
11597 /* Comma is found, jump it and restart the analyzer. */
11598 al++;
11599 }
11600
11601 /* Set default value if required. */
11602 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11603 smp->data.str.str = args[1].data.str.str;
11604 smp->data.str.len = args[1].data.str.len;
11605 }
11606
11607 /* Return true only if a matching language was found. */
11608 return smp->data.str.len != 0;
11609}
11610
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011611/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011612 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011613 * the relevant part of the request line accordingly. Then it updates various
11614 * pointers to the next elements which were moved, and the total buffer length.
11615 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011616 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11617 * error, though this can be revisited when this code is finally exploited.
11618 *
11619 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11620 * query string and 3 to replace uri.
11621 *
11622 * In query string case, the mark question '?' must be set at the start of the
11623 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011624 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011625int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau87b09662015-04-03 00:22:06 +020011626 struct proxy *px, struct stream *s, struct http_txn *txn)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011627{
11628 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011629 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011630 int delta;
11631
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011632 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011633 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011634 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011635 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11636
11637 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011638 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011639 txn->req.sl.rq.m_l += delta;
11640 txn->req.sl.rq.u += delta;
11641 txn->req.sl.rq.v += delta;
11642 break;
11643
11644 case 1: // path
11645 cur_ptr = http_get_path(txn);
11646 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011647 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011648
11649 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011650 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 +010011651 cur_end++;
11652
11653 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011654 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011655 txn->req.sl.rq.u_l += delta;
11656 txn->req.sl.rq.v += delta;
11657 break;
11658
11659 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011660 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011661 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011662 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11663 while (cur_ptr < cur_end && *cur_ptr != '?')
11664 cur_ptr++;
11665
11666 /* skip the question mark or indicate that we must insert it
11667 * (but only if the format string is not empty then).
11668 */
11669 if (cur_ptr < cur_end)
11670 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011671 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011672 offset = 0;
11673
11674 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011675 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011676 txn->req.sl.rq.u_l += delta;
11677 txn->req.sl.rq.v += delta;
11678 break;
11679
11680 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011681 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011682 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11683
11684 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011685 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011686 txn->req.sl.rq.u_l += delta;
11687 txn->req.sl.rq.v += delta;
11688 break;
11689
11690 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011691 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011692 }
11693
11694 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011695 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011696 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011697 return 0;
11698}
11699
11700/* This function executes one of the set-{method,path,query,uri} actions. It
11701 * builds a string in the trash from the specified format string. It finds
11702 * the action to be performed in p[2], previously filled by function
11703 * parse_set_req_line(). The replacement action is excuted by the function
11704 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11705 * the action is canceled, but the rule processing continue.
11706 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011707int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s, struct http_txn *txn)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011708{
11709 chunk_reset(&trash);
11710
11711 /* If we have to create a query string, prepare a '?'. */
11712 if (*(int *)&rule->arg.act.p[2] == 2)
11713 trash.str[trash.len++] = '?';
11714 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11715
11716 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s, txn);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011717 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011718}
11719
11720/* parse an http-request action among :
11721 * set-method
11722 * set-path
11723 * set-query
11724 * set-uri
11725 *
11726 * All of them accept a single argument of type string representing a log-format.
11727 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11728 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11729 * It returns 0 on success, < 0 on error.
11730 */
11731int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11732{
11733 int cur_arg = *orig_arg;
11734
11735 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11736
11737 switch (args[0][4]) {
11738 case 'm' :
11739 *(int *)&rule->arg.act.p[2] = 0;
11740 rule->action_ptr = http_action_set_req_line;
11741 break;
11742 case 'p' :
11743 *(int *)&rule->arg.act.p[2] = 1;
11744 rule->action_ptr = http_action_set_req_line;
11745 break;
11746 case 'q' :
11747 *(int *)&rule->arg.act.p[2] = 2;
11748 rule->action_ptr = http_action_set_req_line;
11749 break;
11750 case 'u' :
11751 *(int *)&rule->arg.act.p[2] = 3;
11752 rule->action_ptr = http_action_set_req_line;
11753 break;
11754 default:
11755 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11756 return -1;
11757 }
11758
11759 if (!*args[cur_arg] ||
11760 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11761 memprintf(err, "expects exactly 1 argument <format>");
11762 return -1;
11763 }
11764
11765 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11766 proxy->conf.args.ctx = ARGC_HRQ;
11767 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11768 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11769 proxy->conf.args.file, proxy->conf.args.line);
11770
11771 (*orig_arg)++;
11772 return 0;
11773}
11774
William Lallemand73025dd2014-04-24 14:38:37 +020011775/*
11776 * Return the struct http_req_action_kw associated to a keyword.
11777 */
11778struct http_req_action_kw *action_http_req_custom(const char *kw)
11779{
11780 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11781 struct http_req_action_kw_list *kw_list;
11782 int i;
11783
11784 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11785 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11786 if (!strcmp(kw, kw_list->kw[i].kw))
11787 return &kw_list->kw[i];
11788 }
11789 }
11790 }
11791 return NULL;
11792}
11793
11794/*
11795 * Return the struct http_res_action_kw associated to a keyword.
11796 */
11797struct http_res_action_kw *action_http_res_custom(const char *kw)
11798{
11799 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11800 struct http_res_action_kw_list *kw_list;
11801 int i;
11802
11803 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11804 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11805 if (!strcmp(kw, kw_list->kw[i].kw))
11806 return &kw_list->kw[i];
11807 }
11808 }
11809 }
11810 return NULL;
11811}
11812
Willy Tarreau4a568972010-05-12 08:08:50 +020011813/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011814/* All supported ACL keywords must be declared here. */
11815/************************************************************************/
11816
11817/* Note: must not be declared <const> as its list will be overwritten.
11818 * Please take care of keeping this list alphabetically sorted.
11819 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011820static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011821 { "base", "base", PAT_MATCH_STR },
11822 { "base_beg", "base", PAT_MATCH_BEG },
11823 { "base_dir", "base", PAT_MATCH_DIR },
11824 { "base_dom", "base", PAT_MATCH_DOM },
11825 { "base_end", "base", PAT_MATCH_END },
11826 { "base_len", "base", PAT_MATCH_LEN },
11827 { "base_reg", "base", PAT_MATCH_REG },
11828 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011829
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011830 { "cook", "req.cook", PAT_MATCH_STR },
11831 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11832 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11833 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11834 { "cook_end", "req.cook", PAT_MATCH_END },
11835 { "cook_len", "req.cook", PAT_MATCH_LEN },
11836 { "cook_reg", "req.cook", PAT_MATCH_REG },
11837 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011838
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011839 { "hdr", "req.hdr", PAT_MATCH_STR },
11840 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11841 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11842 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11843 { "hdr_end", "req.hdr", PAT_MATCH_END },
11844 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11845 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11846 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011847
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011848 /* these two declarations uses strings with list storage (in place
11849 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11850 * and delete functions are relative to the list management. The parse
11851 * and match method are related to the corresponding fetch methods. This
11852 * is very particular ACL declaration mode.
11853 */
11854 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11855 { "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 +020011856
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011857 { "path", "path", PAT_MATCH_STR },
11858 { "path_beg", "path", PAT_MATCH_BEG },
11859 { "path_dir", "path", PAT_MATCH_DIR },
11860 { "path_dom", "path", PAT_MATCH_DOM },
11861 { "path_end", "path", PAT_MATCH_END },
11862 { "path_len", "path", PAT_MATCH_LEN },
11863 { "path_reg", "path", PAT_MATCH_REG },
11864 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011865
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011866 { "req_ver", "req.ver", PAT_MATCH_STR },
11867 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011868
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011869 { "scook", "res.cook", PAT_MATCH_STR },
11870 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11871 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11872 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11873 { "scook_end", "res.cook", PAT_MATCH_END },
11874 { "scook_len", "res.cook", PAT_MATCH_LEN },
11875 { "scook_reg", "res.cook", PAT_MATCH_REG },
11876 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011877
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011878 { "shdr", "res.hdr", PAT_MATCH_STR },
11879 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11880 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11881 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11882 { "shdr_end", "res.hdr", PAT_MATCH_END },
11883 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11884 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11885 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011886
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011887 { "url", "url", PAT_MATCH_STR },
11888 { "url_beg", "url", PAT_MATCH_BEG },
11889 { "url_dir", "url", PAT_MATCH_DIR },
11890 { "url_dom", "url", PAT_MATCH_DOM },
11891 { "url_end", "url", PAT_MATCH_END },
11892 { "url_len", "url", PAT_MATCH_LEN },
11893 { "url_reg", "url", PAT_MATCH_REG },
11894 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011895
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011896 { "urlp", "urlp", PAT_MATCH_STR },
11897 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11898 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11899 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11900 { "urlp_end", "urlp", PAT_MATCH_END },
11901 { "urlp_len", "urlp", PAT_MATCH_LEN },
11902 { "urlp_reg", "urlp", PAT_MATCH_REG },
11903 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011904
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011905 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011906}};
11907
11908/************************************************************************/
11909/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011910/************************************************************************/
11911/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011912static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011913 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011914 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11915 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11916
Willy Tarreau87b09662015-04-03 00:22:06 +020011917 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011918 { "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 +020011919
11920 /* retrieve these captures from the HTTP logs */
11921 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11922 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11923 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11924
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011925 { "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 +020011926 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011927
Willy Tarreau409bcde2013-01-08 00:31:00 +010011928 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11929 * are only here to match the ACL's name, are request-only and are used
11930 * for ACL compatibility only.
11931 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011932 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11933 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011934 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11935 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11936
11937 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11938 * only here to match the ACL's name, are request-only and are used for
11939 * ACL compatibility only.
11940 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011941 { "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 +010011942 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11943 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11944 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11945
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011946 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011947 { "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 +010011948 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011949 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011950 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011951 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011952
11953 /* HTTP protocol on the request path */
11954 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011955 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011956
11957 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011958 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11959 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011960
11961 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011962 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11963 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011964
Willy Tarreau18ed2562013-01-14 15:56:36 +010011965 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011966 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011967 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11968 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11969
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011970 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011971 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011972 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011973 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11974 { "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 +010011975 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011976 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11977
11978 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011979 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011980 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11981 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11982
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011983 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011984 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011985 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011986 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11987 { "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 +010011988 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011989 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11990
Willy Tarreau409bcde2013-01-08 00:31:00 +010011991 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011992 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011993 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11994 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011995 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011996
11997 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011998 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011999 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12000 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12001 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12002
12003 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012004 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012005 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12006 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012007 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12008 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012009 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12010 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012011 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12012 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012013}};
12014
Willy Tarreau8797c062007-05-07 00:55:35 +020012015
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012016/************************************************************************/
12017/* All supported converter keywords must be declared here. */
12018/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012019/* Note: must not be declared <const> as its list will be overwritten */
12020static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012021 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12022 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012023 { NULL, NULL, 0, 0, 0 },
12024}};
12025
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012026/************************************************************************/
12027/* All supported http-request action keywords must be declared here. */
12028/************************************************************************/
12029struct http_req_action_kw_list http_req_actions = {
12030 .scope = "http",
12031 .kw = {
12032 { "set-method", parse_set_req_line },
12033 { "set-path", parse_set_req_line },
12034 { "set-query", parse_set_req_line },
12035 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012036 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012037 }
12038};
12039
Willy Tarreau8797c062007-05-07 00:55:35 +020012040__attribute__((constructor))
12041static void __http_protocol_init(void)
12042{
12043 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012044 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012045 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012046 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012047}
12048
12049
Willy Tarreau58f10d72006-12-04 02:26:12 +010012050/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012051 * Local variables:
12052 * c-indent-level: 8
12053 * c-basic-offset: 8
12054 * End:
12055 */