blob: 7bf80ca6817f15f1242ffedda50a6e1f82c00503 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreauc90dc232015-02-20 13:51:36 +0100590/* Find the first or next header field in message buffer <sol> using headers
591 * index <idx>, and return it in the <ctx> structure. This structure holds
592 * everything necessary to use the header and find next occurrence. If its
593 * <idx> member is 0, the first header is retrieved. Otherwise, the next
594 * occurrence is returned. The function returns 1 when it finds a value, and
595 * 0 when there is no more. It is equivalent to http_find_full_header2() with
596 * no header name.
597 */
598int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
599{
600 char *eol, *sov;
601 int cur_idx, old_idx;
602 int len;
603
604 cur_idx = ctx->idx;
605 if (cur_idx) {
606 /* We have previously returned a header, let's search another one */
607 sol = ctx->line;
608 eol = sol + idx->v[cur_idx].len;
609 goto next_hdr;
610 }
611
612 /* first request for this header */
613 sol += hdr_idx_first_pos(idx);
614 old_idx = 0;
615 cur_idx = hdr_idx_first_idx(idx);
616 while (cur_idx) {
617 eol = sol + idx->v[cur_idx].len;
618
619 len = 0;
620 while (1) {
621 if (len >= eol - sol)
622 goto next_hdr;
623 if (sol[len] == ':')
624 break;
625 len++;
626 }
627
628 ctx->del = len;
629 sov = sol + len + 1;
630 while (sov < eol && http_is_lws[(unsigned char)*sov])
631 sov++;
632
633 ctx->line = sol;
634 ctx->prev = old_idx;
635 ctx->idx = cur_idx;
636 ctx->val = sov - sol;
637 ctx->tws = 0;
638
639 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
640 eol--;
641 ctx->tws++;
642 }
643 ctx->vlen = eol - sov;
644 return 1;
645
646 next_hdr:
647 sol = eol + idx->v[cur_idx].cr + 1;
648 old_idx = cur_idx;
649 cur_idx = idx->v[cur_idx].next;
650 }
651 return 0;
652}
653
Willy Tarreau68085d82010-01-18 14:54:04 +0100654/* Find the end of the header value contained between <s> and <e>. See RFC2616,
655 * par 2.2 for more information. Note that it requires a valid header to return
656 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100658char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200659{
660 int quoted, qdpair;
661
662 quoted = qdpair = 0;
663 for (; s < e; s++) {
664 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200665 else if (quoted) {
666 if (*s == '\\') qdpair = 1;
667 else if (*s == '"') quoted = 0;
668 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 else if (*s == '"') quoted = 1;
670 else if (*s == ',') return s;
671 }
672 return s;
673}
674
675/* Find the first or next occurrence of header <name> in message buffer <sol>
676 * using headers index <idx>, and return it in the <ctx> structure. This
677 * structure holds everything necessary to use the header and find next
678 * occurrence. If its <idx> member is 0, the header is searched from the
679 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * 1 when it finds a value, and 0 when there is no more. It is designed to work
681 * with headers defined as comma-separated lists. As a special case, if ctx->val
682 * is NULL when searching for a new values of a header, the current header is
683 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200684 */
685int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
Willy Tarreau68085d82010-01-18 14:54:04 +0100689 char *eol, *sov;
690 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 cur_idx = ctx->idx;
693 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200694 /* We have previously returned a value, let's search
695 * another one on the same line.
696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200697 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200698 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 eol = sol + idx->v[cur_idx].len;
701
702 if (sov >= eol)
703 /* no more values in this header */
704 goto next_hdr;
705
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 /* values remaining for this header, skip the comma but save it
707 * for later use (eg: for header deletion).
708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200709 sov++;
710 while (sov < eol && http_is_lws[(unsigned char)*sov])
711 sov++;
712
713 goto return_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200723 if (len == 0) {
724 /* No argument was passed, we want any header.
725 * To achieve this, we simply build a fake request. */
726 while (sol + len < eol && sol[len] != ':')
727 len++;
728 name = sol;
729 }
730
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 if ((len < eol - sol) &&
732 (sol[len] == ':') &&
733 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200735 sov = sol + len + 1;
736 while (sov < eol && http_is_lws[(unsigned char)*sov])
737 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100738
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 ctx->prev = old_idx;
741 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744
745 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200747 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200748 eol--;
749 ctx->tws++;
750 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 ctx->vlen = eol - sov;
752 return 1;
753 }
754 next_hdr:
755 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100756 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 cur_idx = idx->v[cur_idx].next;
758 }
759 return 0;
760}
761
762int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 struct hdr_ctx *ctx)
765{
766 return http_find_header2(name, strlen(name), sol, idx, ctx);
767}
768
Willy Tarreau68085d82010-01-18 14:54:04 +0100769/* Remove one value of a header. This only works on a <ctx> returned by one of
770 * the http_find_header functions. The value is removed, as well as surrounding
771 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * message <msg>. The new index is returned. If it is zero, it means there is
774 * no more header, so any processing may stop. The ctx is always left in a form
775 * that can be handled by http_find_header2() to find next occurrence.
776 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100778{
779 int cur_idx = ctx->idx;
780 char *sol = ctx->line;
781 struct hdr_idx_elem *hdr;
782 int delta, skip_comma;
783
784 if (!cur_idx)
785 return 0;
786
787 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 http_msg_move_end(msg, delta);
792 idx->used--;
793 hdr->len = 0; /* unused entry */
794 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100795 if (idx->tail == ctx->idx)
796 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100798 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200800 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 return ctx->idx;
802 }
803
804 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
806 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 */
808
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200809 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200811 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 NULL, 0);
813 hdr->len += delta;
814 http_msg_move_end(msg, delta);
815 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200816 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 return ctx->idx;
818}
819
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820/* This function handles a server error at the stream interface level. The
821 * stream interface is assumed to be already in a closed state. An optional
822 * message is copied into the input buffer, and an HTTP status code stored.
823 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100824 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200826static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100829 channel_auto_read(si_oc(si));
830 channel_abort(si_oc(si));
831 channel_auto_close(si_oc(si));
832 channel_erase(si_oc(si));
833 channel_auto_close(si_ic(si));
834 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100835 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200836 s->txn.status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100837 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200839 if (!(s->flags & SN_ERR_MASK))
840 s->flags |= err;
841 if (!(s->flags & SN_FINST_MASK))
842 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843}
844
Willy Tarreau80587432006-12-24 17:47:20 +0100845/* This function returns the appropriate error location for the given session
846 * and message.
847 */
848
Willy Tarreau783f2582012-09-04 12:19:04 +0200849struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100850{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 if (s->be->errmsg[msgnum].str)
852 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100853 else if (s->fe->errmsg[msgnum].str)
854 return &s->fe->errmsg[msgnum];
855 else
856 return &http_err_chunks[msgnum];
857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreau53b6c742006-12-17 13:37:46 +0100859/*
860 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
861 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
862 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100864{
865 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100867
868 m = ((unsigned)*str - 'A');
869
870 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 for (h = http_methods[m]; h->len > 0; h++) {
872 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100873 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100875 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 };
877 return HTTP_METH_OTHER;
878 }
879 return HTTP_METH_NONE;
880
881}
882
Willy Tarreau21d2af32008-02-14 20:25:24 +0100883/* Parse the URI from the given transaction (which is assumed to be in request
884 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
885 * It is returned otherwise.
886 */
887static char *
888http_get_path(struct http_txn *txn)
889{
890 char *ptr, *end;
891
Willy Tarreau9b28e032012-10-12 23:49:43 +0200892 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 end = ptr + txn->req.sl.rq.u_l;
894
895 if (ptr >= end)
896 return NULL;
897
898 /* RFC2616, par. 5.1.2 :
899 * Request-URI = "*" | absuri | abspath | authority
900 */
901
902 if (*ptr == '*')
903 return NULL;
904
905 if (isalpha((unsigned char)*ptr)) {
906 /* this is a scheme as described by RFC3986, par. 3.1 */
907 ptr++;
908 while (ptr < end &&
909 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
910 ptr++;
911 /* skip '://' */
912 if (ptr == end || *ptr++ != ':')
913 return NULL;
914 if (ptr == end || *ptr++ != '/')
915 return NULL;
916 if (ptr == end || *ptr++ != '/')
917 return NULL;
918 }
919 /* skip [user[:passwd]@]host[:[port]] */
920
921 while (ptr < end && *ptr != '/')
922 ptr++;
923
924 if (ptr == end)
925 return NULL;
926
927 /* OK, we got the '/' ! */
928 return ptr;
929}
930
William Lallemand65ad6e12014-01-31 15:08:02 +0100931/* Parse the URI from the given string and look for the "/" beginning the PATH.
932 * If not found, return NULL. It is returned otherwise.
933 */
934static char *
935http_get_path_from_string(char *str)
936{
937 char *ptr = str;
938
939 /* RFC2616, par. 5.1.2 :
940 * Request-URI = "*" | absuri | abspath | authority
941 */
942
943 if (*ptr == '*')
944 return NULL;
945
946 if (isalpha((unsigned char)*ptr)) {
947 /* this is a scheme as described by RFC3986, par. 3.1 */
948 ptr++;
949 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
950 ptr++;
951 /* skip '://' */
952 if (*ptr == '\0' || *ptr++ != ':')
953 return NULL;
954 if (*ptr == '\0' || *ptr++ != '/')
955 return NULL;
956 if (*ptr == '\0' || *ptr++ != '/')
957 return NULL;
958 }
959 /* skip [user[:passwd]@]host[:[port]] */
960
961 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
962 ptr++;
963
964 if (*ptr == '\0' || *ptr == ' ')
965 return NULL;
966
967 /* OK, we got the '/' ! */
968 return ptr;
969}
970
Willy Tarreau71241ab2012-12-27 11:30:54 +0100971/* Returns a 302 for a redirectable request that reaches a server working in
972 * in redirect mode. This may only be called just after the stream interface
973 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
974 * follow normal proxy processing. NOTE: this function is designed to support
975 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100977void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978{
979 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200982 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 trash.len = strlen(HTTP_302);
986 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100988 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100989
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100991 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992 return;
993
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100994 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100996 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
997 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100998 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001000 /* 3: add the request URI. Since it was already forwarded, we need
1001 * to temporarily rewind the buffer.
1002 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 txn = &s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001004 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001005
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001007 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001009 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001010
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 if (!path)
1012 return;
1013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001014 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 return;
1016
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 memcpy(trash.str + trash.len, path, len);
1018 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001019
1020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001021 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1022 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001023 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001024 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1025 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001026 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001027
1028 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->state = SI_ST_CLO;
1033
1034 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +02001035 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001036
1037 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001038 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001039 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040}
1041
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001043 * that the server side is closed. Note that err_type is actually a
1044 * bitmask, where almost only aborts may be cumulated with other
1045 * values. We consider that aborted operations are more important
1046 * than timeouts or errors due to the fact that nobody else in the
1047 * logs might explain incomplete retries. All others should avoid
1048 * being cumulated. It should normally not be possible to have multiple
1049 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001050 * Note that connection errors appearing on the second request of a keep-alive
1051 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001053void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001054{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001058 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001059 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001061 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001062 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1063 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001064 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001065 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001066 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001068 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001069 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001071 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001072 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1073 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001075 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001076 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001077 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001078 else if (err_type & SI_ET_CONN_RES)
1079 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001080 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1081 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001083 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001084 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085}
1086
Willy Tarreau42250582007-04-01 01:30:43 +02001087extern const char sess_term_cond[8];
1088extern const char sess_fin_state[8];
1089extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001090struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001091struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001092struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Willy Tarreau117f59e2007-03-04 18:17:17 +01001094/*
1095 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001096 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001097 */
1098void capture_headers(char *som, struct hdr_idx *idx,
1099 char **cap, struct cap_hdr *cap_hdr)
1100{
1101 char *eol, *sol, *col, *sov;
1102 int cur_idx;
1103 struct cap_hdr *h;
1104 int len;
1105
1106 sol = som + hdr_idx_first_pos(idx);
1107 cur_idx = hdr_idx_first_idx(idx);
1108
1109 while (cur_idx) {
1110 eol = sol + idx->v[cur_idx].len;
1111
1112 col = sol;
1113 while (col < eol && *col != ':')
1114 col++;
1115
1116 sov = col + 1;
1117 while (sov < eol && http_is_lws[(unsigned char)*sov])
1118 sov++;
1119
1120 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001121 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001122 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1123 if (cap[h->index] == NULL)
1124 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001125 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001126
1127 if (cap[h->index] == NULL) {
1128 Alert("HTTP capture : out of memory.\n");
1129 continue;
1130 }
1131
1132 len = eol - sov;
1133 if (len > h->len)
1134 len = h->len;
1135
1136 memcpy(cap[h->index], sov, len);
1137 cap[h->index][len]=0;
1138 }
1139 }
1140 sol = eol + idx->v[cur_idx].cr + 1;
1141 cur_idx = idx->v[cur_idx].next;
1142 }
1143}
1144
1145
Willy Tarreau42250582007-04-01 01:30:43 +02001146/* either we find an LF at <ptr> or we jump to <bad>.
1147 */
1148#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1149
1150/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1151 * otherwise to <http_msg_ood> with <state> set to <st>.
1152 */
1153#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1154 ptr++; \
1155 if (likely(ptr < end)) \
1156 goto good; \
1157 else { \
1158 state = (st); \
1159 goto http_msg_ood; \
1160 } \
1161 } while (0)
1162
1163
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001165 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1167 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1168 * will give undefined results.
1169 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1170 * and that msg->sol points to the beginning of the response.
1171 * If a complete line is found (which implies that at least one CR or LF is
1172 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1173 * returned indicating an incomplete line (which does not mean that parts have
1174 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1175 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1176 * upon next call.
1177 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001178 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1180 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001181 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001183const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001184 enum ht_state state, const char *ptr, const char *end,
1185 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001188
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001192 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1194
1195 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001196 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1198 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001199 state = HTTP_MSG_ERROR;
1200 break;
1201
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001203 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001205 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 goto http_msg_rpcode;
1207 }
1208 if (likely(HTTP_IS_SPHT(*ptr)))
1209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001215 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 if (likely(!HTTP_IS_LWS(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1218
1219 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1222 }
1223
1224 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 http_msg_rsp_reason:
1227 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001228 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 msg->sl.st.r_l = 0;
1230 goto http_msg_rpline_eol;
1231
Willy Tarreau8973c702007-01-21 23:58:29 +01001232 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 goto http_msg_rpreason;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1240 /* so it's a CR/LF, so there is no reason phrase */
1241 goto http_msg_rsp_reason;
1242
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (likely(!HTTP_IS_CRLF(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 http_msg_rpline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the response would not be
1252 * complete. We can then record the response length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 return ptr;
1257
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001259#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1261 exit(1);
1262#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001263 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 }
1265
1266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001273}
1274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 * This function parses a request line between <ptr> and <end>, starting with
1277 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1278 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the request.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (likely(HTTP_IS_TOKEN(*ptr)))
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_CRLF(*ptr))) {
1312 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001313 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sl.rq.v_l = 0;
1321 goto http_msg_rqline_eol;
1322 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
1325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001329 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rquri;
1331 }
1332 if (likely(HTTP_IS_SPHT(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1334 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1335 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001339 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001343 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001347 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001348 /* non-ASCII chars are forbidden unless option
1349 * accept-invalid-http-request is enabled in the frontend.
1350 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001351 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001352 if (msg->err_pos < -1)
1353 goto invalid_char;
1354 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001356 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1357 }
1358
1359 if (likely(HTTP_IS_CRLF(*ptr))) {
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri_e;
1362 }
1363
1364 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001365 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 state = HTTP_MSG_ERROR;
1368 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001373 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 goto http_msg_rqver;
1375 }
1376 if (likely(HTTP_IS_SPHT(*ptr)))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1378 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1379 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001383 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385
1386 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001387 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001388 http_msg_rqline_eol:
1389 /* We have seen the end of line. Note that we do not
1390 * necessarily have the \n yet, but at least we know that we
1391 * have EITHER \r OR \n, otherwise the request would not be
1392 * complete. We can then record the request length and return
1393 * to the caller which will be able to register it.
1394 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001395 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001396 return ptr;
1397 }
1398
1399 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001400 state = HTTP_MSG_ERROR;
1401 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001404#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
1407#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001408 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001412 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (ret_state)
1414 *ret_state = state;
1415 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001420/*
1421 * Returns the data from Authorization header. Function may be called more
1422 * than once so data is stored in txn->auth_data. When no header is found
1423 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001424 * searching again for something we are unable to find anyway. However, if
1425 * the result if valid, the cache is not reused because we would risk to
1426 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001427 */
1428
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001429/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1430 * set according to global.tune.bufsize.
1431 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001432char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001433
1434int
1435get_http_auth(struct session *s)
1436{
1437
1438 struct http_txn *txn = &s->txn;
1439 struct chunk auth_method;
1440 struct hdr_ctx ctx;
1441 char *h, *p;
1442 int len;
1443
1444#ifdef DEBUG_AUTH
1445 printf("Auth for session %p: %d\n", s, txn->auth.method);
1446#endif
1447
1448 if (txn->auth.method == HTTP_AUTH_WRONG)
1449 return 0;
1450
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001451 txn->auth.method = HTTP_AUTH_WRONG;
1452
1453 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001454
1455 if (txn->flags & TX_USE_PX_CONN) {
1456 h = "Proxy-Authorization";
1457 len = strlen(h);
1458 } else {
1459 h = "Authorization";
1460 len = strlen(h);
1461 }
1462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001463 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001464 return 0;
1465
1466 h = ctx.line + ctx.val;
1467
1468 p = memchr(h, ' ', ctx.vlen);
1469 if (!p || p == h)
1470 return 0;
1471
1472 chunk_initlen(&auth_method, h, 0, p-h);
1473 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1474
1475 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1476
1477 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001478 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001479
1480 if (len < 0)
1481 return 0;
1482
1483
1484 get_http_auth_buff[len] = '\0';
1485
1486 p = strchr(get_http_auth_buff, ':');
1487
1488 if (!p)
1489 return 0;
1490
1491 txn->auth.user = get_http_auth_buff;
1492 *p = '\0';
1493 txn->auth.pass = p+1;
1494
1495 txn->auth.method = HTTP_AUTH_BASIC;
1496 return 1;
1497 }
1498
1499 return 0;
1500}
1501
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503/*
1504 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001505 * depending on the initial msg->msg_state. The caller is responsible for
1506 * ensuring that the message does not wrap. The function can be preempted
1507 * everywhere when data are missing and recalled at the exact same location
1508 * with no information loss. The message may even be realigned between two
1509 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001510 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001511 * fields. Note that msg->sol will be initialized after completing the first
1512 * state, so that none of the msg pointers has to be initialized prior to the
1513 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001515void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516{
Willy Tarreau3770f232013-12-07 00:01:53 +01001517 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001522 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 ptr = buf->p + msg->next;
1524 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(ptr >= end))
1527 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /*
1531 * First, states that are specific to the response only.
1532 * We check them first so that request and headers are
1533 * closer to each other (accessed more often).
1534 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 hdr_idx_init(idx);
1551 state = HTTP_MSG_RPVER;
1552 goto http_msg_rpver;
1553 }
1554
1555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
1557
1558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1561 /* stop here */
1562
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1567 /* stop here */
1568
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER_SP:
1572 case HTTP_MSG_RPCODE:
1573 case HTTP_MSG_RPCODE_SP:
1574 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 if (unlikely(!ptr))
1579 return;
1580
1581 /* we have a full response and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1589 goto http_msg_rpline_end;
1590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* msg->sol must point to the first of CR or LF. */
1594 EXPECT_LF_HERE(ptr, http_msg_invalid);
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1596 /* stop here */
1597
1598 /*
1599 * Second, states that are specific to the request only
1600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001602 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001604 /* we have a start of message, but we have to check
1605 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001606 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 if (likely(ptr != buf->p)) {
1609 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001611 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001612 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 }
Willy Tarreau26927362012-05-18 23:22:52 +02001614 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001615 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 state = HTTP_MSG_RQMETH;
1617 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1621 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(*ptr == '\n'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1625 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH_SP:
1637 case HTTP_MSG_RQURI:
1638 case HTTP_MSG_RQURI_SP:
1639 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001640 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001641 state, ptr, end,
1642 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(!ptr))
1644 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* we have a full request and we know that we have either a CR
1647 * or an LF at <ptr>.
1648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 /* check for HTTP/0.9 request : no version information available.
1659 * msg->sol must point to the first of CR or LF.
1660 */
1661 if (unlikely(msg->sl.rq.v_l == 0))
1662 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /*
1669 * Common states below
1670 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 if (likely(!HTTP_IS_CRLF(*ptr))) {
1675 goto http_msg_hdr_name;
1676 }
1677
1678 if (likely(*ptr == '\r'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1680 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 /* assumes msg->sol points to the first char */
1685 if (likely(HTTP_IS_TOKEN(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001688 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001691 if (likely(msg->err_pos < -1) || *ptr == '\n')
1692 goto http_msg_invalid;
1693
1694 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001696
1697 /* and we still accept this non-token character */
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001701 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_SPHT(*ptr)))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_val;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1715 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 EXPECT_LF_HERE(ptr, http_msg_invalid);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr))) {
1725 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 for (; buf->p + msg->sov < ptr; msg->sov++)
1727 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto http_msg_hdr_l1_sp;
1729 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001730 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001731 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 goto http_msg_complete_header;
1733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001736 /* assumes msg->sol points to the first char, and msg->sov
1737 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 */
1739 if (likely(!HTTP_IS_CRLF(*ptr)))
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* Note: we could also copy eol into ->eoh so that we have the
1744 * real header end in case it ends with lots of LWS, but is this
1745 * really needed ?
1746 */
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1749 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 EXPECT_LF_HERE(ptr, http_msg_invalid);
1754 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1759 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001760 for (; buf->p + msg->eol < ptr; msg->eol++)
1761 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 goto http_msg_hdr_val;
1763 }
1764 http_msg_complete_header:
1765 /*
1766 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001767 * Assumes msg->sol points to the first char, msg->sov points
1768 * to the first character of the value and msg->eol to the
1769 * first CR or LF so we know how the line ends. We insert last
1770 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 idx, idx->tail) < 0))
1774 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001775
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001787 /* Assumes msg->sol points to the first of either CR or LF.
1788 * Sets ->sov and ->next to the total header length, ->eoh to
1789 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EXPECT_LF_HERE(ptr, http_msg_invalid);
1792 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001794 msg->eoh = msg->sol;
1795 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001796 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001797 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001799
1800 case HTTP_MSG_ERROR:
1801 /* this may only happen if we call http_msg_analyser() twice with an error */
1802 break;
1803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001805#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1807 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001809 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
1811 http_msg_ood:
1812 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001813 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 http_msg_invalid:
1818 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001819 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 return;
1822}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001823
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1825 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1826 * nothing is done and 1 is returned.
1827 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001828static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001829{
1830 int delta;
1831 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001832 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001833
1834 if (msg->sl.rq.v_l != 0)
1835 return 1;
1836
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001837 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1838 if (txn->meth != HTTP_METH_GET)
1839 return 0;
1840
Willy Tarreau9b28e032012-10-12 23:49:43 +02001841 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001842 delta = 0;
1843
1844 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001845 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1846 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001847 }
1848 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001850 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001851 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001852 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001854 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 NULL, NULL);
1856 if (unlikely(!cur_end))
1857 return 0;
1858
1859 /* we have a full HTTP/1.0 request now and we know that
1860 * we have either a CR or an LF at <ptr>.
1861 */
1862 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1863 return 1;
1864}
1865
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001867 * and "keep-alive" values. If we already know that some headers may safely
1868 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1870 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001871 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1873 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1874 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001878{
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 const char *hdr_val = "Connection";
1881 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 return;
1885
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1887 hdr_val = "Proxy-Connection";
1888 hdr_len = 16;
1889 }
1890
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001893 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1895 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 if (to_del & 2)
1897 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else
1899 txn->flags |= TX_CON_KAL_SET;
1900 }
1901 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1902 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 if (to_del & 1)
1904 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 else
1906 txn->flags |= TX_CON_CLO_SET;
1907 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001908 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1909 txn->flags |= TX_HDR_CONN_UPG;
1910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 }
1912
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags |= TX_HDR_CONN_PRS;
1914 return;
1915}
Willy Tarreau5b154472009-12-21 20:11:07 +01001916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001917/* Apply desired changes on the Connection: header. Values may be removed and/or
1918 * added depending on the <wanted> flags, which are exclusively composed of
1919 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1920 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1921 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001922void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001923{
1924 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001925 const char *hdr_val = "Connection";
1926 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001927
1928 ctx.idx = 0;
1929
Willy Tarreau88d349d2010-01-25 12:15:43 +01001930
1931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1932 hdr_val = "Proxy-Connection";
1933 hdr_len = 16;
1934 }
1935
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001936 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001937 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1939 if (wanted & TX_CON_KAL_SET)
1940 txn->flags |= TX_CON_KAL_SET;
1941 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1945 if (wanted & TX_CON_CLO_SET)
1946 txn->flags |= TX_CON_CLO_SET;
1947 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001948 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001949 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001950 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951
1952 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1953 return;
1954
1955 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1956 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 hdr_val = "Connection: close";
1958 hdr_len = 17;
1959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1960 hdr_val = "Proxy-Connection: close";
1961 hdr_len = 23;
1962 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 }
1965
1966 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1967 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001968 hdr_val = "Connection: keep-alive";
1969 hdr_len = 22;
1970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection: keep-alive";
1972 hdr_len = 28;
1973 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001974 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 }
1976 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001977}
1978
Willy Tarreauc24715e2014-04-17 15:21:20 +02001979/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1980 * the first byte of data after the chunk size, so that we know we can forward
1981 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1982 * the chunk size. That way it is always possible to differentiate between the
1983 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001987static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001988{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001989 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001991 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *end = buf->data + buf->size;
1993 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 unsigned int chunk = 0;
1995
1996 /* The chunk size is in the following form, though we are only
1997 * interested in the size and CRLF :
1998 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1999 */
2000 while (1) {
2001 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 if (c < 0) /* not a hex digit anymore */
2006 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002009 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002011 chunk = (chunk << 4) + c;
2012 }
2013
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002015 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017
2018 while (http_is_spht[(unsigned char)*ptr]) {
2019 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002021 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 }
2024
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 /* Up to there, we know that at least one byte is present at *ptr. Check
2026 * for the end of chunk size.
2027 */
2028 while (1) {
2029 if (likely(HTTP_IS_CRLF(*ptr))) {
2030 /* we now have a CR or an LF at ptr */
2031 if (likely(*ptr == '\r')) {
2032 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002037
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002039 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 /* done */
2043 break;
2044 }
2045 else if (*ptr == ';') {
2046 /* chunk extension, ends at next CRLF */
2047 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 while (!HTTP_IS_CRLF(*ptr)) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
2058 /* we have a CRLF now, loop above */
2059 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 }
2064
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002066 * which may or may not be present. We save that into ->next,
2067 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002069 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002070 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002073 msg->chunk_len = chunk;
2074 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002077 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080}
2081
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002082/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 * the trailers is found, it is automatically scheduled to be forwarded,
2085 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2086 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * except maybe msg->next if it could parse some lines, and returns zero.
2088 * If a parse error is encountered, the function returns < 0 and does not
2089 * change anything except maybe msg->next. Note that the message must
2090 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002091 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 * forwarding, and that msg->next exactly matches the length of trailers
2093 * already parsed and not forwarded. It is also important to note that this
2094 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002096static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002098 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002099
Willy Tarreaua458b672012-03-05 11:17:50 +01002100 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *ptr = b_ptr(buf, msg->next);
2104 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002105 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106
2107 /* scan current line and stop at LF or CRLF */
2108 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002109 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 return 0;
2111
2112 if (*ptr == '\n') {
2113 if (!p1)
2114 p1 = ptr;
2115 p2 = ptr;
2116 break;
2117 }
2118
2119 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 p1 = ptr;
2125 }
2126
2127 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 if (ptr >= buf->data + buf->size)
2129 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 }
2131
2132 /* after LF; point to beginning of next line */
2133 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (p2 >= buf->data + buf->size)
2135 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002137 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002138 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002140
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 /* LF/CRLF at beginning of line => end of trailers at p2.
2143 * Everything was scheduled for forwarding, there's nothing
2144 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_DONE;
2148 return 1;
2149 }
2150 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 }
2153}
2154
Willy Tarreauc24715e2014-04-17 15:21:20 +02002155/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2156 * or a possible LF alone at the end of a chunk. It automatically adjusts
2157 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002158 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2160 * not enough data are available, the function does not change anything and
2161 * returns zero. If a parse error is encountered, the function returns < 0 and
2162 * does not change anything. Note: this function is designed to parse wrapped
2163 * CRLF at the end of the buffer.
2164 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002165static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002167 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002168 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169 int bytes;
2170
2171 /* NB: we'll check data availabilty at the end. It's not a
2172 * problem because whatever we match first will be checked
2173 * against the correct length.
2174 */
2175 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002176 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 if (*ptr == '\r') {
2178 bytes++;
2179 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 if (ptr >= buf->data + buf->size)
2181 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 }
2183
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002184 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002185 return 0;
2186
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002187 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002188 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191
2192 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002193 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002194 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2198 return 1;
2199}
Willy Tarreau5b154472009-12-21 20:11:07 +01002200
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002201/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2202 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2203 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2204 * Unparsable qvalues return 1000 as "q=1.000".
2205 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002206int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207{
2208 int q = 1000;
2209
Willy Tarreau506c69a2014-07-08 00:59:48 +02002210 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002211 goto out;
2212 q = (*qvalue++ - '0') * 1000;
2213
2214 if (*qvalue++ != '.')
2215 goto out;
2216
Willy Tarreau506c69a2014-07-08 00:59:48 +02002217 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002218 goto out;
2219 q += (*qvalue++ - '0') * 100;
2220
Willy Tarreau506c69a2014-07-08 00:59:48 +02002221 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 goto out;
2223 q += (*qvalue++ - '0') * 10;
2224
Willy Tarreau506c69a2014-07-08 00:59:48 +02002225 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 goto out;
2227 q += (*qvalue++ - '0') * 1;
2228 out:
2229 if (q > 1000)
2230 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002231 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002232 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 return q;
2234}
William Lallemand82fe75c2012-10-23 10:25:10 +02002235
2236/*
2237 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002238 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002239int select_compression_request_header(struct session *s, struct buffer *req)
2240{
2241 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 struct hdr_ctx ctx;
2244 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002245 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002247 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2248 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002249 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2250 */
2251 ctx.idx = 0;
2252 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2253 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002254 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2255 (ctx.vlen < 31 ||
2256 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2257 ctx.line[ctx.val + 30] < '6' ||
2258 (ctx.line[ctx.val + 30] == '6' &&
2259 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2260 s->comp_algo = NULL;
2261 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002262 }
2263
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 int best_q = 0;
2267
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 ctx.idx = 0;
2269 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002270 const char *qval;
2271 int q;
2272 int toklen;
2273
2274 /* try to isolate the token from the optional q-value */
2275 toklen = 0;
2276 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2277 toklen++;
2278
2279 qval = ctx.line + ctx.val + toklen;
2280 while (1) {
2281 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2282 qval++;
2283
2284 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2285 qval = NULL;
2286 break;
2287 }
2288 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002289
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2291 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002292
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002293 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2294 qval = NULL;
2295 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002297 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2298 break;
2299
2300 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2301 qval++;
2302 }
2303
2304 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002305 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306
2307 if (q <= best_q)
2308 continue;
2309
2310 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2311 if (*(ctx.line + ctx.val) == '*' ||
2312 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2313 s->comp_algo = comp_algo;
2314 best_q = q;
2315 break;
2316 }
2317 }
2318 }
2319 }
2320
2321 /* remove all occurrences of the header when "compression offload" is set */
2322 if (s->comp_algo) {
2323 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2324 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2325 ctx.idx = 0;
2326 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2327 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 }
2329 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002330 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
2332
2333 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2335 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 if (comp_algo->add_data == identity_add_data) {
2337 s->comp_algo = comp_algo;
2338 return 1;
2339 }
2340 }
2341 }
2342
2343 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 return 0;
2345}
2346
2347/*
2348 * Selects a comression algorithm depending of the server response.
2349 */
2350int select_compression_response_header(struct session *s, struct buffer *res)
2351{
2352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->rsp;
2354 struct hdr_ctx ctx;
2355 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002356
2357 /* no common compression algorithm was found in request header */
2358 if (s->comp_algo == NULL)
2359 goto fail;
2360
2361 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002362 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 goto fail;
2364
William Lallemandd3002612012-11-26 14:34:47 +01002365 /* 200 only */
2366 if (txn->status != 200)
2367 goto fail;
2368
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 /* Content-Length is null */
2370 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2371 goto fail;
2372
2373 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002374 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2376 goto fail;
2377
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002378 /* no compression when Cache-Control: no-transform is present in the message */
2379 ctx.idx = 0;
2380 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2381 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2382 goto fail;
2383 }
2384
William Lallemand82fe75c2012-10-23 10:25:10 +02002385 comp_type = NULL;
2386
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002387 /* we don't want to compress multipart content-types, nor content-types that are
2388 * not listed in the "compression type" directive if any. If no content-type was
2389 * found but configuration requires one, we don't compress either. Backend has
2390 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002391 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002392 ctx.idx = 0;
2393 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2394 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2395 goto fail;
2396
2397 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2398 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002399 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002400 if (ctx.vlen >= comp_type->name_len &&
2401 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 /* this Content-Type should be compressed */
2403 break;
2404 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002405 /* this Content-Type should not be compressed */
2406 if (comp_type == NULL)
2407 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002409 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 else { /* no content-type header */
2411 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2412 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002413 }
2414
William Lallemandd85f9172012-11-09 17:05:39 +01002415 /* limit compression rate */
2416 if (global.comp_rate_lim > 0)
2417 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2418 goto fail;
2419
William Lallemand072a2bf2012-11-20 17:01:01 +01002420 /* limit cpu usage */
2421 if (idle_pct < compress_min_idle)
2422 goto fail;
2423
William Lallemand4c49fae2012-11-07 15:00:23 +01002424 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002425 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002426 goto fail;
2427
William Lallemandec3e3892012-11-12 17:02:18 +01002428 s->flags |= SN_COMP_READY;
2429
William Lallemand82fe75c2012-10-23 10:25:10 +02002430 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002431 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2434
2435 /* add Transfer-Encoding header */
2436 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2437 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2438
2439 /*
2440 * Add Content-Encoding header when it's not identity encoding.
2441 * RFC 2616 : Identity encoding: This content-coding is used only in the
2442 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2443 * header.
2444 */
2445 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002446 trash.len = 18;
2447 memcpy(trash.str, "Content-Encoding: ", trash.len);
2448 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2449 trash.len += s->comp_algo->name_len;
2450 trash.str[trash.len] = '\0';
2451 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002452 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 return 1;
2454
2455fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002456 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002457 return 0;
2458}
2459
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002460void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2461{
2462 int tmp = TX_CON_WANT_KAL;
2463
2464 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2465 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2467 tmp = TX_CON_WANT_TUN;
2468
2469 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2470 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2471 tmp = TX_CON_WANT_TUN;
2472 }
2473
2474 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2475 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2476 /* option httpclose + server_close => forceclose */
2477 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2479 tmp = TX_CON_WANT_CLO;
2480 else
2481 tmp = TX_CON_WANT_SCL;
2482 }
2483
2484 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2486 tmp = TX_CON_WANT_CLO;
2487
2488 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2490
2491 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2492 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2493 /* parse the Connection header and possibly clean it */
2494 int to_del = 0;
2495 if ((msg->flags & HTTP_MSGF_VER_11) ||
2496 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2497 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2498 to_del |= 2; /* remove "keep-alive" */
2499 if (!(msg->flags & HTTP_MSGF_VER_11))
2500 to_del |= 1; /* remove "close" */
2501 http_parse_connection_header(txn, msg, to_del);
2502 }
2503
2504 /* check if client or config asks for explicit close in KAL/SCL */
2505 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2506 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2507 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2508 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2509 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2510 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2511 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2512}
William Lallemand82fe75c2012-10-23 10:25:10 +02002513
Willy Tarreaud787e662009-07-07 10:14:51 +02002514/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2515 * processing can continue on next analysers, or zero if it either needs more
2516 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002517 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002518 * when it has nothing left to do, and may remove any analyser when it wants to
2519 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002520 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002521int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002522{
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 /*
2524 * We will parse the partial (or complete) lines.
2525 * We will check the request syntax, and also join multi-line
2526 * headers. An index of all the lines will be elaborated while
2527 * parsing.
2528 *
2529 * For the parsing, we use a 28 states FSM.
2530 *
2531 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002532 * req->buf->p = beginning of request
2533 * req->buf->p + msg->eoh = end of processed headers / start of current one
2534 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002535 * msg->eol = end of current header or line (LF or CRLF)
2536 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 *
2538 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002539 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2541 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002545 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 struct http_txn *txn = &s->txn;
2547 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002548 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002549
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002550 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002551 now_ms, __FUNCTION__,
2552 s,
2553 req,
2554 req->rex, req->wex,
2555 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002557 req->analysers);
2558
Willy Tarreau52a0c602009-08-16 22:45:38 +02002559 /* we're speaking HTTP here, so let's speak HTTP to the client */
2560 s->srv_error = http_return_srv_error;
2561
Willy Tarreau83e3af02009-12-28 17:39:57 +01002562 /* There's a protected area at the end of the buffer for rewriting
2563 * purposes. We don't want to start to parse the request if the
2564 * protected area is affected, because we may have to move processed
2565 * data later, which is much more complicated.
2566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002567 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002568 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002569 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002570 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002571 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002572 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002573 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002574 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002575 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002576 return 0;
2577 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002578 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2579 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2580 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002581 }
2582
Willy Tarreau065e8332010-01-08 00:30:20 +01002583 /* Note that we have the same problem with the response ; we
2584 * may want to send a redirect, error or anything which requires
2585 * some spare space. So we'll ensure that we have at least
2586 * maxrewrite bytes available in the response buffer before
2587 * processing that one. This will only affect pipelined
2588 * keep-alive requests.
2589 */
2590 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002591 unlikely(!channel_is_rewritable(&s->res) ||
2592 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2593 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2594 if (s->res.buf->o) {
2595 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002597 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002599 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2600 s->res.flags |= CF_WAKE_WRITE;
2601 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002602 return 0;
2603 }
2604 }
2605
Willy Tarreau9b28e032012-10-12 23:49:43 +02002606 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002607 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002608 }
2609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* 1: we might have to print this header in debug mode */
2611 if (unlikely((global.mode & MODE_DEBUG) &&
2612 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002613 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau9b28e032012-10-12 23:49:43 +02002616 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002617 /* this is a bit complex : in case of error on the request line,
2618 * we know that rq.l is still zero, so we display only the part
2619 * up to the end of the line (truncated by debug_hdr).
2620 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002621 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 sol += hdr_idx_first_pos(&txn->hdr_idx);
2625 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 while (cur_idx) {
2628 eol = sol + txn->hdr_idx.v[cur_idx].len;
2629 debug_hdr("clihdr", s, sol, eol);
2630 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2631 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 }
2634
Willy Tarreau58f10d72006-12-04 02:26:12 +01002635
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 /*
2637 * Now we quickly check if we have found a full valid request.
2638 * If not so, we check the FD and buffer states before leaving.
2639 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002640 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002641 * requests are checked first. When waiting for a second request
2642 * on a keep-alive session, if we encounter and error, close, t/o,
2643 * we note the error in the session flags but don't set any state.
2644 * Since the error will be noted there, it will not be counted by
2645 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002646 * Last, we may increase some tracked counters' http request errors on
2647 * the cases that are deliberately the client's fault. For instance,
2648 * a timeout or connection reset is not counted as an error. However
2649 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau655dce92009-11-08 13:10:58 +01002652 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002653 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002655 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002656 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_req_ctr(s);
2658 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 /* 1: Since we are in header mode, if there's no space
2664 * left for headers, we won't be able to free more
2665 * later, so the session will never terminate. We
2666 * must terminate it now.
2667 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /* FIXME: check if URI is set and return Status
2670 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002671 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002672 session_inc_http_req_ctr(s);
2673 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002674 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002675 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 goto return_bad_req;
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002682 if (!(s->flags & SN_ERR_MASK))
2683 s->flags |= SN_ERR_CLICL;
2684
Willy Tarreaufcffa692010-01-10 14:21:19 +01002685 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002686 goto failed_keep_alive;
2687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002690 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002691 session_inc_http_err_ctr(s);
2692 }
2693
Willy Tarreaudc979f22012-12-04 10:39:01 +01002694 txn->status = 400;
2695 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 msg->msg_state = HTTP_MSG_ERROR;
2697 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002700 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002701 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002702 if (s->listener->counters)
2703 s->listener->counters->failed_req++;
2704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 if (!(s->flags & SN_FINST_MASK))
2706 s->flags |= SN_FINST_R;
2707 return 0;
2708 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002709
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002711 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002712 if (!(s->flags & SN_ERR_MASK))
2713 s->flags |= SN_ERR_CLITO;
2714
Willy Tarreaufcffa692010-01-10 14:21:19 +01002715 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002716 goto failed_keep_alive;
2717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002719 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002720 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002721 session_inc_http_err_ctr(s);
2722 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002724 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 msg->msg_state = HTTP_MSG_ERROR;
2726 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002727
Willy Tarreauda7ff642010-06-23 11:44:09 +02002728 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002729 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002730 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731 if (s->listener->counters)
2732 s->listener->counters->failed_req++;
2733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 if (!(s->flags & SN_FINST_MASK))
2735 s->flags |= SN_FINST_R;
2736 return 0;
2737 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002740 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002741 if (!(s->flags & SN_ERR_MASK))
2742 s->flags |= SN_ERR_CLICL;
2743
Willy Tarreaufcffa692010-01-10 14:21:19 +01002744 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002745 goto failed_keep_alive;
2746
Willy Tarreau4076a152009-04-02 15:18:36 +02002747 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002748 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 msg->msg_state = HTTP_MSG_ERROR;
2752 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreauda7ff642010-06-23 11:44:09 +02002754 session_inc_http_err_ctr(s);
2755 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002756 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002757 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758 if (s->listener->counters)
2759 s->listener->counters->failed_req++;
2760
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 if (!(s->flags & SN_FINST_MASK))
2762 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002763 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 }
2765
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002766 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002767 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002768 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002769#ifdef TCP_QUICKACK
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002770 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req.prod->end) && conn_ctrl_ready(__objt_conn(s->req.prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002771 /* We need more data, we have to re-enable quick-ack in case we
2772 * previously disabled it, otherwise we might cause the client
2773 * to delay next data.
2774 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002775 setsockopt(__objt_conn(s->req.prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002776 }
2777#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002778
Willy Tarreaufcffa692010-01-10 14:21:19 +01002779 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2780 /* If the client starts to talk, let's fall back to
2781 * request timeout processing.
2782 */
2783 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002784 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002785 }
2786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002788 if (!tick_isset(req->analyse_exp)) {
2789 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2790 (txn->flags & TX_WAIT_NEXT_RQ) &&
2791 tick_isset(s->be->timeout.httpka))
2792 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2793 else
2794 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002796
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 /* we're not ready yet */
2798 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002799
2800 failed_keep_alive:
2801 /* Here we process low-level errors for keep-alive requests. In
2802 * short, if the request is not the first one and it experiences
2803 * a timeout, read error or shutdown, we just silently close so
2804 * that the client can try again.
2805 */
2806 txn->status = 0;
2807 msg->msg_state = HTTP_MSG_RQBEFORE;
2808 req->analysers = 0;
2809 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002810 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002811 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002812 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002813 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002815
Willy Tarreaud787e662009-07-07 10:14:51 +02002816 /* OK now we have a complete HTTP request with indexed headers. Let's
2817 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002818 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002819 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002820 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002821 * byte after the last LF. msg->sov points to the first byte of data.
2822 * msg->eol cannot be trusted because it may have been left uninitialized
2823 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002825
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002827 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2828
Willy Tarreaub16a5742010-01-10 14:46:16 +01002829 if (txn->flags & TX_WAIT_NEXT_RQ) {
2830 /* kill the pending keep-alive timeout */
2831 txn->flags &= ~TX_WAIT_NEXT_RQ;
2832 req->analyse_exp = TICK_ETERNITY;
2833 }
2834
2835
Willy Tarreaud787e662009-07-07 10:14:51 +02002836 /* Maybe we found in invalid header name while we were configured not
2837 * to block on that, so we have to capture it now.
2838 */
2839 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002840 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002841
Willy Tarreau59234e92008-11-30 23:51:27 +01002842 /*
2843 * 1: identify the method
2844 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002845 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002846
2847 /* we can make use of server redirect on GET and HEAD */
2848 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2849 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /*
2852 * 2: check if the URI matches the monitor_uri.
2853 * We have to do this for every request which gets in, because
2854 * the monitor-uri is defined by the frontend.
2855 */
2856 if (unlikely((s->fe->monitor_uri_len != 0) &&
2857 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002858 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 s->fe->monitor_uri,
2860 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002863 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002865
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002867 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002871 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002872
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 ret = acl_pass(ret);
2874 if (cond->pol == ACL_COND_UNLESS)
2875 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 if (ret) {
2878 /* we fail this request, let's return 503 service unavail */
2879 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002880 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002881 if (!(s->flags & SN_ERR_MASK))
2882 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002884 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002885 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002886
Willy Tarreau59234e92008-11-30 23:51:27 +01002887 /* nothing to fail, let's reply normaly */
2888 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002889 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002890 if (!(s->flags & SN_ERR_MASK))
2891 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002892 goto return_prx_cond;
2893 }
2894
2895 /*
2896 * 3: Maybe we have to copy the original REQURI for the logs ?
2897 * Note: we cannot log anymore if the request has been
2898 * classified as invalid.
2899 */
2900 if (unlikely(s->logs.logwait & LW_REQ)) {
2901 /* we have a complete HTTP request that we must log */
2902 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2903 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002904
Willy Tarreau59234e92008-11-30 23:51:27 +01002905 if (urilen >= REQURI_LEN)
2906 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002907 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002909
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002910 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 s->do_log(s);
2912 } else {
2913 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002914 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 }
Willy Tarreau06619262006-12-17 08:37:22 +01002916
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002918 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002919 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002920
Willy Tarreau5b154472009-12-21 20:11:07 +01002921 /* ... and check if the request is HTTP/1.1 or above */
2922 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002923 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2924 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2925 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002926 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002927
2928 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002929 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002930
Willy Tarreau88d349d2010-01-25 12:15:43 +01002931 /* if the frontend has "option http-use-proxy-header", we'll check if
2932 * we have what looks like a proxied connection instead of a connection,
2933 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2934 * Note that this is *not* RFC-compliant, however browsers and proxies
2935 * happen to do that despite being non-standard :-(
2936 * We consider that a request not beginning with either '/' or '*' is
2937 * a proxied connection, which covers both "scheme://location" and
2938 * CONNECT ip:port.
2939 */
2940 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002941 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002942 txn->flags |= TX_USE_PX_CONN;
2943
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002944 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002945 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002946
Willy Tarreau59234e92008-11-30 23:51:27 +01002947 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002948 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002951
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002952 /* 6: determine the transfer-length.
2953 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2954 * the presence of a message-body in a REQUEST and its transfer length
2955 * must be determined that way (in order of precedence) :
2956 * 1. The presence of a message-body in a request is signaled by the
2957 * inclusion of a Content-Length or Transfer-Encoding header field
2958 * in the request's header fields. When a request message contains
2959 * both a message-body of non-zero length and a method that does
2960 * not define any semantics for that request message-body, then an
2961 * origin server SHOULD either ignore the message-body or respond
2962 * with an appropriate error message (e.g., 413). A proxy or
2963 * gateway, when presented the same request, SHOULD either forward
2964 * the request inbound with the message- body or ignore the
2965 * message-body when determining a response.
2966 *
2967 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2968 * and the "chunked" transfer-coding (Section 6.2) is used, the
2969 * transfer-length is defined by the use of this transfer-coding.
2970 * If a Transfer-Encoding header field is present and the "chunked"
2971 * transfer-coding is not present, the transfer-length is defined
2972 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002974 * 3. If a Content-Length header field is present, its decimal value in
2975 * OCTETs represents both the entity-length and the transfer-length.
2976 * If a message is received with both a Transfer-Encoding header
2977 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002978 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002979 * 4. By the server closing the connection. (Closing the connection
2980 * cannot be used to indicate the end of a request body, since that
2981 * would leave no possibility for the server to send back a response.)
2982 *
2983 * Whenever a transfer-coding is applied to a message-body, the set of
2984 * transfer-codings MUST include "chunked", unless the message indicates
2985 * it is terminated by closing the connection. When the "chunked"
2986 * transfer-coding is used, it MUST be the last transfer-coding applied
2987 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002988 */
2989
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002990 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002991 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002992 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002993 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002994 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2997 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* bad transfer-encoding (chunked followed by something else) */
2999 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003000 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 break;
3002 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 }
3004
Willy Tarreau32b47f42009-10-18 20:55:02 +02003005 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003006 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003007 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 signed long long cl;
3009
Willy Tarreauad14f752011-09-02 20:33:27 +02003010 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreauad14f752011-09-02 20:33:27 +02003015 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003016 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003019
Willy Tarreauad14f752011-09-02 20:33:27 +02003020 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003021 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003024
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003025 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003028 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003030 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003031 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032 }
3033
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003034 /* bodyless requests have a known length */
3035 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003036 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003037
Willy Tarreau179085c2014-04-28 16:48:56 +02003038 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3039 * only change if both the request and the config reference something else.
3040 * Option httpclose by itself sets tunnel mode where headers are mangled.
3041 * However, if another mode is set, it will affect it (eg: server-close/
3042 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3043 * if FE and BE have the same settings (common). The method consists in
3044 * checking if options changed between the two calls (implying that either
3045 * one is non-null, or one of them is non-null and we are there for the first
3046 * time.
3047 */
3048 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003049 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3050 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003051
Willy Tarreaud787e662009-07-07 10:14:51 +02003052 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003053 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003054 req->analyse_exp = TICK_ETERNITY;
3055 return 1;
3056
3057 return_bad_req:
3058 /* We centralize bad requests processing here */
3059 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3060 /* we detected a parsing error. We want to archive this request
3061 * in the dedicated proxy area for later troubleshooting.
3062 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003063 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003064 }
3065
3066 txn->req.msg_state = HTTP_MSG_ERROR;
3067 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003068 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003069
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003070 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003071 if (s->listener->counters)
3072 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003073
3074 return_prx_cond:
3075 if (!(s->flags & SN_ERR_MASK))
3076 s->flags |= SN_ERR_PRXCOND;
3077 if (!(s->flags & SN_FINST_MASK))
3078 s->flags |= SN_FINST_R;
3079
3080 req->analysers = 0;
3081 req->analyse_exp = TICK_ETERNITY;
3082 return 0;
3083}
3084
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003085
Willy Tarreau347a35d2013-11-22 17:51:09 +01003086/* This function prepares an applet to handle the stats. It can deal with the
3087 * "100-continue" expectation, check that admin rules are met for POST requests,
3088 * and program a response message if something was unexpected. It cannot fail
3089 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003090 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003091 * s->target which is supposed to already point to the stats applet. The caller
3092 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 */
3094int http_handle_stats(struct session *s, struct channel *req)
3095{
3096 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003097 struct stream_interface *si = s->res.prod;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003098 struct http_txn *txn = &s->txn;
3099 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003100 struct uri_auth *uri_auth = s->be->uri_auth;
3101 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003104 appctx = si_appctx(si);
3105 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3106 appctx->st1 = appctx->st2 = 0;
3107 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3108 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003109 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3110 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111
3112 uri = msg->chn->buf->p + msg->sl.rq.u;
3113 lookup = uri + uri_auth->uri_len;
3114
3115 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3116 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 break;
3119 }
3120 }
3121
3122 if (uri_auth->refresh) {
3123 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3124 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126 break;
3127 }
3128 }
3129 }
3130
3131 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3132 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003134 break;
3135 }
3136 }
3137
3138 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3139 if (memcmp(h, ";st=", 4) == 0) {
3140 int i;
3141 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003142 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003143 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3144 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 break;
3147 }
3148 }
3149 break;
3150 }
3151 }
3152
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003153 appctx->ctx.stats.scope_str = 0;
3154 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003155 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3156 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3157 int itx = 0;
3158 const char *h2;
3159 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3160 const char *err;
3161
3162 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3163 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003165 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3166 itx++;
3167 h++;
3168 }
3169
3170 if (itx > STAT_SCOPE_TXT_MAXLEN)
3171 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003173
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 memcpy(scope_txt, h2, itx);
3176 scope_txt[itx] = '\0';
3177 err = invalid_char(scope_txt);
3178 if (err) {
3179 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.scope_str = 0;
3181 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 }
3183 break;
3184 }
3185 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003186
3187 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189 int ret = 1;
3190
3191 if (stats_admin_rule->cond) {
3192 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3193 ret = acl_pass(ret);
3194 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3195 ret = !ret;
3196 }
3197
3198 if (ret) {
3199 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003201 break;
3202 }
3203 }
3204
3205 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003206 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003208 /* we'll need the request body, possibly after sending 100-continue */
3209 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003211 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003212 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3214 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003215 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003217 else {
3218 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003220 }
3221
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003222 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003223 return 1;
3224}
3225
Lukas Tribus67db8df2013-06-23 17:37:13 +02003226/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3227 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3228 */
3229static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3230{
3231#ifdef IP_TOS
3232 if (from.ss_family == AF_INET)
3233 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3234#endif
3235#ifdef IPV6_TCLASS
3236 if (from.ss_family == AF_INET6) {
3237 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3238 /* v4-mapped addresses need IP_TOS */
3239 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3240 else
3241 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3242 }
3243#endif
3244}
3245
Sasha Pachev218f0642014-06-16 12:05:59 -06003246/* Returns the number of characters written to destination,
3247 * -1 on internal error and -2 if no replacement took place.
3248 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003249static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003250 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003251{
Willy Tarreau15a53a42015-01-21 13:39:42 +01003252 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch, 0))
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 return -2;
3254
3255 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3256}
3257
3258/* Returns the number of characters written to destination,
3259 * -1 on internal error and -2 if no replacement took place.
3260 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003261static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003262 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003263{
3264 char* p = val;
3265 char* dst_end = dst + dst_size;
3266 char* dst_p = dst;
3267
3268 for (;;) {
3269 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003270
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003271 /* look for delim. */
3272 p_delim = p;
3273 while (p_delim < p + len && *p_delim != delim)
3274 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003275
Willy Tarreau15a53a42015-01-21 13:39:42 +01003276 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch, 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003277 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3278
3279 if (replace_n < 0)
3280 return -1;
3281
3282 dst_p += replace_n;
3283 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003284 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285
3286 if (dst_p + len >= dst_end)
3287 return -1;
3288
3289 memcpy(dst_p, p, len);
3290 dst_p += len;
3291 }
3292
3293 if (dst_p >= dst_end)
3294 return -1;
3295
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003296 /* end of the replacements. */
3297 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003298 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003299
3300 /* Next part. */
3301 *dst_p++ = delim;
3302 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003303 }
3304
3305 return dst_p - dst;
3306}
3307
3308static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003309 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003310 struct hdr_ctx* ctx, int action)
3311{
3312 ctx->idx = 0;
3313
3314 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3315 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3316 int delta;
Willy Tarreauaa435e72015-01-29 14:01:34 +01003317 char* val = (char*)ctx->line + ctx->val;
Sasha Pachev218f0642014-06-16 12:05:59 -06003318 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003319 char* reg_dst_buf;
3320 uint reg_dst_buf_size;
3321 int n_replaced;
3322
Sasha Pachev218f0642014-06-16 12:05:59 -06003323 trash.len = build_logline(s, trash.str, trash.size, fmt);
3324
3325 if (trash.len >= trash.size - 1)
3326 return -1;
3327
3328 reg_dst_buf = trash.str + trash.len + 1;
3329 reg_dst_buf_size = trash.size - trash.len - 1;
3330
3331 switch (action) {
3332 case HTTP_REQ_ACT_REPLACE_VAL:
3333 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003334 n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003335 break;
3336 case HTTP_REQ_ACT_REPLACE_HDR:
3337 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003338 n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003339 break;
3340 default: /* impossible */
3341 return -1;
3342 }
3343
Sasha Pachev218f0642014-06-16 12:05:59 -06003344 switch (n_replaced) {
3345 case -1: return -1;
3346 case -2: continue;
3347 }
3348
3349 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3350
3351 hdr->len += delta;
3352 http_msg_move_end(msg, delta);
3353 }
3354
3355 return 0;
3356}
3357
Willy Tarreau20b0de52012-12-24 15:45:22 +01003358/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 * transaction <txn>. Returns the verdict of the first rule that prevents
3360 * further processing of the request (auth, deny, ...), and defaults to
3361 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3362 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3363 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003364 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003365enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003366http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003367{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003368 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003369 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003370 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003371 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003372
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003373 /* If "the current_rule_list" match the executed rule list, we are in
3374 * resume condition. If a resume is needed it is always in the action
3375 * and never in the ACL or converters. In this case, we initialise the
3376 * current rule, and go to the action execution point.
3377 */
3378 if (s->current_rule_list == rules) {
3379 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3380 goto resume_execution;
3381 }
3382 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003383 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003384 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003385 continue;
3386
Willy Tarreau96257ec2012-12-27 10:46:37 +01003387 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003388 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003389 int ret;
3390
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003391 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003392 ret = acl_pass(ret);
3393
Willy Tarreauff011f22011-01-06 17:51:27 +01003394 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003395 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003396
3397 if (!ret) /* condition not matched */
3398 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003399 }
3400
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003401resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003402
Willy Tarreau96257ec2012-12-27 10:46:37 +01003403 switch (rule->action) {
3404 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003405 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003406
3407 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003408 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003409
Willy Tarreauccbcc372012-12-27 12:37:57 +01003410 case HTTP_REQ_ACT_TARPIT:
3411 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003412 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003413
Willy Tarreau96257ec2012-12-27 10:46:37 +01003414 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003415 /* Auth might be performed on regular http-req rules as well as on stats */
3416 auth_realm = rule->arg.auth.realm;
3417 if (!auth_realm) {
3418 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3419 auth_realm = STATS_DEFAULT_REALM;
3420 else
3421 auth_realm = px->id;
3422 }
3423 /* send 401/407 depending on whether we use a proxy or not. We still
3424 * count one error, because normal browsing won't significantly
3425 * increase the counter but brute force attempts will.
3426 */
3427 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3428 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3429 stream_int_retnclose(&s->si[0], &trash);
3430 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003431 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003432
Willy Tarreau81499eb2012-12-27 12:19:02 +01003433 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003434 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3435 return HTTP_RULE_RES_BADREQ;
3436 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003437
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003438 case HTTP_REQ_ACT_SET_NICE:
3439 s->task->nice = rule->arg.nice;
3440 break;
3441
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003442 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003443 if ((cli_conn = objt_conn(s->req.prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003444 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003445 break;
3446
Willy Tarreau51347ed2013-06-11 19:34:13 +02003447 case HTTP_REQ_ACT_SET_MARK:
3448#ifdef SO_MARK
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003449 if ((cli_conn = objt_conn(s->req.prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003450 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003451#endif
3452 break;
3453
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003454 case HTTP_REQ_ACT_SET_LOGL:
3455 s->logs.level = rule->arg.loglevel;
3456 break;
3457
Sasha Pachev218f0642014-06-16 12:05:59 -06003458 case HTTP_REQ_ACT_REPLACE_HDR:
3459 case HTTP_REQ_ACT_REPLACE_VAL:
3460 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3461 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003462 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003463 return HTTP_RULE_RES_BADREQ;
3464 break;
3465
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003466 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003467 ctx.idx = 0;
3468 /* remove all occurrences of the header */
3469 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3470 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3471 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003472 }
Willy Tarreau85603282015-01-21 20:39:27 +01003473 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003474
Willy Tarreau85603282015-01-21 20:39:27 +01003475 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003476 case HTTP_REQ_ACT_ADD_HDR:
3477 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3478 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3479 trash.len = rule->arg.hdr_add.name_len;
3480 trash.str[trash.len++] = ':';
3481 trash.str[trash.len++] = ' ';
3482 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 +01003483
3484 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3485 /* remove all occurrences of the header */
3486 ctx.idx = 0;
3487 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3488 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3489 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3490 }
3491 }
3492
Willy Tarreau96257ec2012-12-27 10:46:37 +01003493 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3494 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003495
3496 case HTTP_REQ_ACT_DEL_ACL:
3497 case HTTP_REQ_ACT_DEL_MAP: {
3498 struct pat_ref *ref;
3499 char *key;
3500 int len;
3501
3502 /* collect reference */
3503 ref = pat_ref_lookup(rule->arg.map.ref);
3504 if (!ref)
3505 continue;
3506
3507 /* collect key */
3508 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3509 key = trash.str;
3510 key[len] = '\0';
3511
3512 /* perform update */
3513 /* returned code: 1=ok, 0=ko */
3514 pat_ref_delete(ref, key);
3515
3516 break;
3517 }
3518
3519 case HTTP_REQ_ACT_ADD_ACL: {
3520 struct pat_ref *ref;
3521 char *key;
3522 struct chunk *trash_key;
3523 int len;
3524
3525 trash_key = get_trash_chunk();
3526
3527 /* collect reference */
3528 ref = pat_ref_lookup(rule->arg.map.ref);
3529 if (!ref)
3530 continue;
3531
3532 /* collect key */
3533 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3534 key = trash_key->str;
3535 key[len] = '\0';
3536
3537 /* perform update */
3538 /* add entry only if it does not already exist */
3539 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003540 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003541
3542 break;
3543 }
3544
3545 case HTTP_REQ_ACT_SET_MAP: {
3546 struct pat_ref *ref;
3547 char *key, *value;
3548 struct chunk *trash_key, *trash_value;
3549 int len;
3550
3551 trash_key = get_trash_chunk();
3552 trash_value = get_trash_chunk();
3553
3554 /* collect reference */
3555 ref = pat_ref_lookup(rule->arg.map.ref);
3556 if (!ref)
3557 continue;
3558
3559 /* collect key */
3560 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3561 key = trash_key->str;
3562 key[len] = '\0';
3563
3564 /* collect value */
3565 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3566 value = trash_value->str;
3567 value[len] = '\0';
3568
3569 /* perform update */
3570 if (pat_ref_find_elt(ref, key) != NULL)
3571 /* update entry if it exists */
3572 pat_ref_set(ref, key, value, NULL);
3573 else
3574 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003575 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003576
3577 break;
3578 }
William Lallemand73025dd2014-04-24 14:38:37 +02003579
3580 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003581 if (!rule->action_ptr(rule, px, s, txn)) {
3582 s->current_rule = &rule->list;
3583 return HTTP_RULE_RES_YIELD;
3584 }
William Lallemand73025dd2014-04-24 14:38:37 +02003585 break;
3586
3587 case HTTP_REQ_ACT_CUSTOM_STOP:
3588 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003589 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003590
3591 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3592 /* Note: only the first valid tracking parameter of each
3593 * applies.
3594 */
3595
3596 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3597 struct stktable *t;
3598 struct stksess *ts;
3599 struct stktable_key *key;
3600 void *ptr;
3601
3602 t = rule->act_prm.trk_ctr.table.t;
3603 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3604
3605 if (key && (ts = stktable_get_entry(t, key))) {
3606 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3607
3608 /* let's count a new HTTP request as it's the first time we do it */
3609 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3610 if (ptr)
3611 stktable_data_cast(ptr, http_req_cnt)++;
3612
3613 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3614 if (ptr)
3615 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3616 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3617
3618 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3619 if (s->fe != s->be)
3620 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3621 }
3622 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003623 }
3624 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003625
3626 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003627 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003628}
3629
Willy Tarreau71241ab2012-12-27 11:30:54 +01003630
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003631/* Executes the http-response rules <rules> for session <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003632 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3633 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3634 * evaluation of next rule list. If *STOP is returned, the process must stop
3635 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3636 * rule. If *YIELD is returned, the czller must call again the function with
3637 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003638 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003639static enum rule_result
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003640http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3641{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003642 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003643 struct http_res_rule *rule;
3644 struct hdr_ctx ctx;
3645
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003646 /* If "the current_rule_list" match the executed rule list, we are in
3647 * resume condition. If a resume is needed it is always in the action
3648 * and never in the ACL or converters. In this case, we initialise the
3649 * current rule, and go to the action execution point.
3650 */
3651 if (s->current_rule_list == rules) {
3652 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3653 goto resume_execution;
3654 }
3655 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003656 list_for_each_entry(rule, rules, list) {
3657 if (rule->action >= HTTP_RES_ACT_MAX)
3658 continue;
3659
3660 /* check optional condition */
3661 if (rule->cond) {
3662 int ret;
3663
3664 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3665 ret = acl_pass(ret);
3666
3667 if (rule->cond->pol == ACL_COND_UNLESS)
3668 ret = !ret;
3669
3670 if (!ret) /* condition not matched */
3671 continue;
3672 }
3673
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003674resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003675
3676 switch (rule->action) {
3677 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003678 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003679
3680 case HTTP_RES_ACT_DENY:
3681 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003682 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003683
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003684 case HTTP_RES_ACT_SET_NICE:
3685 s->task->nice = rule->arg.nice;
3686 break;
3687
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003688 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003689 if ((cli_conn = objt_conn(s->req.prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003690 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003691 break;
3692
Willy Tarreau51347ed2013-06-11 19:34:13 +02003693 case HTTP_RES_ACT_SET_MARK:
3694#ifdef SO_MARK
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003695 if ((cli_conn = objt_conn(s->req.prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003696 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003697#endif
3698 break;
3699
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003700 case HTTP_RES_ACT_SET_LOGL:
3701 s->logs.level = rule->arg.loglevel;
3702 break;
3703
Sasha Pachev218f0642014-06-16 12:05:59 -06003704 case HTTP_RES_ACT_REPLACE_HDR:
3705 case HTTP_RES_ACT_REPLACE_VAL:
3706 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3707 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003708 &rule->arg.hdr_add.re, &ctx, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003709 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003710 break;
3711
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003712 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003713 ctx.idx = 0;
3714 /* remove all occurrences of the header */
3715 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3716 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3717 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3718 }
Willy Tarreau85603282015-01-21 20:39:27 +01003719 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003720
Willy Tarreau85603282015-01-21 20:39:27 +01003721 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003722 case HTTP_RES_ACT_ADD_HDR:
3723 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3724 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3725 trash.len = rule->arg.hdr_add.name_len;
3726 trash.str[trash.len++] = ':';
3727 trash.str[trash.len++] = ' ';
3728 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 +01003729
3730 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3731 /* remove all occurrences of the header */
3732 ctx.idx = 0;
3733 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3734 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3735 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3736 }
3737 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003738 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3739 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003740
3741 case HTTP_RES_ACT_DEL_ACL:
3742 case HTTP_RES_ACT_DEL_MAP: {
3743 struct pat_ref *ref;
3744 char *key;
3745 int len;
3746
3747 /* collect reference */
3748 ref = pat_ref_lookup(rule->arg.map.ref);
3749 if (!ref)
3750 continue;
3751
3752 /* collect key */
3753 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3754 key = trash.str;
3755 key[len] = '\0';
3756
3757 /* perform update */
3758 /* returned code: 1=ok, 0=ko */
3759 pat_ref_delete(ref, key);
3760
3761 break;
3762 }
3763
3764 case HTTP_RES_ACT_ADD_ACL: {
3765 struct pat_ref *ref;
3766 char *key;
3767 struct chunk *trash_key;
3768 int len;
3769
3770 trash_key = get_trash_chunk();
3771
3772 /* collect reference */
3773 ref = pat_ref_lookup(rule->arg.map.ref);
3774 if (!ref)
3775 continue;
3776
3777 /* collect key */
3778 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3779 key = trash_key->str;
3780 key[len] = '\0';
3781
3782 /* perform update */
3783 /* check if the entry already exists */
3784 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003785 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003786
3787 break;
3788 }
3789
3790 case HTTP_RES_ACT_SET_MAP: {
3791 struct pat_ref *ref;
3792 char *key, *value;
3793 struct chunk *trash_key, *trash_value;
3794 int len;
3795
3796 trash_key = get_trash_chunk();
3797 trash_value = get_trash_chunk();
3798
3799 /* collect reference */
3800 ref = pat_ref_lookup(rule->arg.map.ref);
3801 if (!ref)
3802 continue;
3803
3804 /* collect key */
3805 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3806 key = trash_key->str;
3807 key[len] = '\0';
3808
3809 /* collect value */
3810 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3811 value = trash_value->str;
3812 value[len] = '\0';
3813
3814 /* perform update */
3815 if (pat_ref_find_elt(ref, key) != NULL)
3816 /* update entry if it exists */
3817 pat_ref_set(ref, key, value, NULL);
3818 else
3819 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003820 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003821
3822 break;
3823 }
William Lallemand73025dd2014-04-24 14:38:37 +02003824
3825 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003826 if (!rule->action_ptr(rule, px, s, txn)) {
3827 s->current_rule = &rule->list;
3828 return HTTP_RULE_RES_YIELD;
3829 }
William Lallemand73025dd2014-04-24 14:38:37 +02003830 break;
3831
3832 case HTTP_RES_ACT_CUSTOM_STOP:
3833 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003834 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003835 }
3836 }
3837
3838 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003839 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003840}
3841
3842
Willy Tarreau71241ab2012-12-27 11:30:54 +01003843/* Perform an HTTP redirect based on the information in <rule>. The function
3844 * returns non-zero on success, or zero in case of a, irrecoverable error such
3845 * as too large a request to build a valid response.
3846 */
3847static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3848{
3849 struct http_msg *msg = &txn->req;
3850 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003851 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003852
3853 /* build redirect message */
3854 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003855 case 308:
3856 msg_fmt = HTTP_308;
3857 break;
3858 case 307:
3859 msg_fmt = HTTP_307;
3860 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003861 case 303:
3862 msg_fmt = HTTP_303;
3863 break;
3864 case 301:
3865 msg_fmt = HTTP_301;
3866 break;
3867 case 302:
3868 default:
3869 msg_fmt = HTTP_302;
3870 break;
3871 }
3872
3873 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3874 return 0;
3875
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003876 location = trash.str + trash.len;
3877
Willy Tarreau71241ab2012-12-27 11:30:54 +01003878 switch(rule->type) {
3879 case REDIRECT_TYPE_SCHEME: {
3880 const char *path;
3881 const char *host;
3882 struct hdr_ctx ctx;
3883 int pathlen;
3884 int hostlen;
3885
3886 host = "";
3887 hostlen = 0;
3888 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003889 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003890 host = ctx.line + ctx.val;
3891 hostlen = ctx.vlen;
3892 }
3893
3894 path = http_get_path(txn);
3895 /* build message using path */
3896 if (path) {
3897 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3898 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3899 int qs = 0;
3900 while (qs < pathlen) {
3901 if (path[qs] == '?') {
3902 pathlen = qs;
3903 break;
3904 }
3905 qs++;
3906 }
3907 }
3908 } else {
3909 path = "/";
3910 pathlen = 1;
3911 }
3912
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003913 if (rule->rdr_str) { /* this is an old "redirect" rule */
3914 /* check if we can add scheme + "://" + host + path */
3915 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3916 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003917
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003918 /* add scheme */
3919 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3920 trash.len += rule->rdr_len;
3921 }
3922 else {
3923 /* add scheme with executing log format */
3924 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003925
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003926 /* check if we can add scheme + "://" + host + path */
3927 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3928 return 0;
3929 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003930 /* add "://" */
3931 memcpy(trash.str + trash.len, "://", 3);
3932 trash.len += 3;
3933
3934 /* add host */
3935 memcpy(trash.str + trash.len, host, hostlen);
3936 trash.len += hostlen;
3937
3938 /* add path */
3939 memcpy(trash.str + trash.len, path, pathlen);
3940 trash.len += pathlen;
3941
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003942 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003943 if (trash.len && trash.str[trash.len - 1] != '/' &&
3944 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3945 if (trash.len > trash.size - 5)
3946 return 0;
3947 trash.str[trash.len] = '/';
3948 trash.len++;
3949 }
3950
3951 break;
3952 }
3953 case REDIRECT_TYPE_PREFIX: {
3954 const char *path;
3955 int pathlen;
3956
3957 path = http_get_path(txn);
3958 /* build message using path */
3959 if (path) {
3960 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3961 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3962 int qs = 0;
3963 while (qs < pathlen) {
3964 if (path[qs] == '?') {
3965 pathlen = qs;
3966 break;
3967 }
3968 qs++;
3969 }
3970 }
3971 } else {
3972 path = "/";
3973 pathlen = 1;
3974 }
3975
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003976 if (rule->rdr_str) { /* this is an old "redirect" rule */
3977 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3978 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003979
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003980 /* add prefix. Note that if prefix == "/", we don't want to
3981 * add anything, otherwise it makes it hard for the user to
3982 * configure a self-redirection.
3983 */
3984 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3985 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3986 trash.len += rule->rdr_len;
3987 }
3988 }
3989 else {
3990 /* add prefix with executing log format */
3991 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3992
3993 /* Check length */
3994 if (trash.len + pathlen > trash.size - 4)
3995 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996 }
3997
3998 /* add path */
3999 memcpy(trash.str + trash.len, path, pathlen);
4000 trash.len += pathlen;
4001
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004002 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004003 if (trash.len && trash.str[trash.len - 1] != '/' &&
4004 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4005 if (trash.len > trash.size - 5)
4006 return 0;
4007 trash.str[trash.len] = '/';
4008 trash.len++;
4009 }
4010
4011 break;
4012 }
4013 case REDIRECT_TYPE_LOCATION:
4014 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004015 if (rule->rdr_str) { /* this is an old "redirect" rule */
4016 if (trash.len + rule->rdr_len > trash.size - 4)
4017 return 0;
4018
4019 /* add location */
4020 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4021 trash.len += rule->rdr_len;
4022 }
4023 else {
4024 /* add location with executing log format */
4025 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004026
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004027 /* Check left length */
4028 if (trash.len > trash.size - 4)
4029 return 0;
4030 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004031 break;
4032 }
4033
4034 if (rule->cookie_len) {
4035 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4036 trash.len += 14;
4037 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4038 trash.len += rule->cookie_len;
4039 memcpy(trash.str + trash.len, "\r\n", 2);
4040 trash.len += 2;
4041 }
4042
4043 /* add end of headers and the keep-alive/close status.
4044 * We may choose to set keep-alive if the Location begins
4045 * with a slash, because the client will come back to the
4046 * same server.
4047 */
4048 txn->status = rule->code;
4049 /* let's log the request time */
4050 s->logs.tv_request = now;
4051
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004052 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4054 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4055 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4056 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4057 /* keep-alive possible */
4058 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4059 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4060 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4061 trash.len += 30;
4062 } else {
4063 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4064 trash.len += 24;
4065 }
4066 }
4067 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4068 trash.len += 4;
4069 bo_inject(txn->rsp.chn, trash.str, trash.len);
4070 /* "eat" the request */
4071 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004072 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004073 msg->sov = 0;
4074 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004075 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004076 txn->req.msg_state = HTTP_MSG_CLOSED;
4077 txn->rsp.msg_state = HTTP_MSG_DONE;
4078 } else {
4079 /* keep-alive not possible */
4080 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4081 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4082 trash.len += 29;
4083 } else {
4084 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4085 trash.len += 23;
4086 }
4087 stream_int_retnclose(txn->req.chn->prod, &trash);
4088 txn->req.chn->analysers = 0;
4089 }
4090
4091 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004092 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004093 if (!(s->flags & SN_FINST_MASK))
4094 s->flags |= SN_FINST_R;
4095
4096 return 1;
4097}
4098
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004099/* This stream analyser runs all HTTP request processing which is common to
4100 * frontends and backends, which means blocking ACLs, filters, connection-close,
4101 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004102 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004103 * either needs more data or wants to immediately abort the request (eg: deny,
4104 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004105 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004106int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004107{
Willy Tarreaud787e662009-07-07 10:14:51 +02004108 struct http_txn *txn = &s->txn;
4109 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004110 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004111 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004112 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004113
Willy Tarreau655dce92009-11-08 13:10:58 +01004114 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004115 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004116 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004117 }
4118
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004119 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004120 now_ms, __FUNCTION__,
4121 s,
4122 req,
4123 req->rex, req->wex,
4124 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004125 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004126 req->analysers);
4127
Willy Tarreau65410832014-04-28 21:25:43 +02004128 /* just in case we have some per-backend tracking */
4129 session_inc_be_http_req_ctr(s);
4130
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004131 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004132 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4133 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004134
Willy Tarreau0b748332014-04-29 00:13:29 +02004135 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004136 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4137 goto return_prx_yield;
4138
Willy Tarreau0b748332014-04-29 00:13:29 +02004139 case HTTP_RULE_RES_CONT:
4140 case HTTP_RULE_RES_STOP: /* nothing to do */
4141 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004142
Willy Tarreau0b748332014-04-29 00:13:29 +02004143 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4144 if (txn->flags & TX_CLTARPIT)
4145 goto tarpit;
4146 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004147
Willy Tarreau0b748332014-04-29 00:13:29 +02004148 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4149 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004150
Willy Tarreau0b748332014-04-29 00:13:29 +02004151 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004152 goto done;
4153
Willy Tarreau0b748332014-04-29 00:13:29 +02004154 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4155 goto return_bad_req;
4156 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 }
4158
Willy Tarreau52542592014-04-28 18:33:26 +02004159 /* OK at this stage, we know that the request was accepted according to
4160 * the http-request rules, we can check for the stats. Note that the
4161 * URI is detected *before* the req* rules in order not to be affected
4162 * by a possible reqrep, while they are processed *after* so that a
4163 * reqdeny can still block them. This clearly needs to change in 1.6!
4164 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004165 if (stats_check_uri(s->res.prod, txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004166 s->target = &http_stats_applet.obj_type;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004167 if (unlikely(!stream_int_register_handler(s->res.prod, objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004168 txn->status = 500;
4169 s->logs.tv_request = now;
4170 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004171
Willy Tarreau52542592014-04-28 18:33:26 +02004172 if (!(s->flags & SN_ERR_MASK))
4173 s->flags |= SN_ERR_RESOURCE;
4174 goto return_prx_cond;
4175 }
4176
4177 /* parse the whole stats request and extract the relevant information */
4178 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004179 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4180 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004181
Willy Tarreau0b748332014-04-29 00:13:29 +02004182 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4183 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004184
Willy Tarreau0b748332014-04-29 00:13:29 +02004185 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4186 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004187 }
4188
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004189 /* evaluate the req* rules except reqadd */
4190 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004191 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004192 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004193
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004194 if (txn->flags & TX_CLDENY)
4195 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004196
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004197 if (txn->flags & TX_CLTARPIT)
4198 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004199 }
Willy Tarreau06619262006-12-17 08:37:22 +01004200
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004201 /* add request headers from the rule sets in the same order */
4202 list_for_each_entry(wl, &px->req_add, list) {
4203 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004204 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004205 ret = acl_pass(ret);
4206 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4207 ret = !ret;
4208 if (!ret)
4209 continue;
4210 }
4211
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004212 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004213 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004214 }
4215
Willy Tarreau52542592014-04-28 18:33:26 +02004216
4217 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004218 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004219 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004220 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4221 s->fe->fe_counters.intercepted_req++;
4222
4223 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4224 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4225 if (!(s->flags & SN_FINST_MASK))
4226 s->flags |= SN_FINST_R;
4227
Willy Tarreau70730dd2014-04-24 18:06:27 +02004228 /* we may want to compress the stats page */
4229 if (s->fe->comp || s->be->comp)
4230 select_compression_request_header(s, req->buf);
4231
4232 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004233 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004235 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004236
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004237 /* check whether we have some ACLs set to redirect this request */
4238 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004239 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240 int ret;
4241
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004242 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004243 ret = acl_pass(ret);
4244 if (rule->cond->pol == ACL_COND_UNLESS)
4245 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004246 if (!ret)
4247 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004248 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 if (!http_apply_redirect_rule(rule, s, txn))
4250 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004251 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004252 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004253
Willy Tarreau2be39392010-01-03 17:24:51 +01004254 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4255 * If this happens, then the data will not come immediately, so we must
4256 * send all what we have without waiting. Note that due to the small gain
4257 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004258 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004259 * itself once used.
4260 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004261 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004262
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004263 done: /* done with this analyser, continue with next ones that the calling
4264 * points will have set, if any.
4265 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004267 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4268 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004269 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004270
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004271 tarpit:
4272 /* When a connection is tarpitted, we use the tarpit timeout,
4273 * which may be the same as the connect timeout if unspecified.
4274 * If unset, then set it to zero because we really want it to
4275 * eventually expire. We build the tarpit as an analyser.
4276 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004277 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004278
4279 /* wipe the request out so that we can drop the connection early
4280 * if the client closes first.
4281 */
4282 channel_dont_connect(req);
4283 req->analysers = 0; /* remove switching rules etc... */
4284 req->analysers |= AN_REQ_HTTP_TARPIT;
4285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4286 if (!req->analyse_exp)
4287 req->analyse_exp = tick_add(now_ms, 0);
4288 session_inc_http_err_ctr(s);
4289 s->fe->fe_counters.denied_req++;
4290 if (s->fe != s->be)
4291 s->be->be_counters.denied_req++;
4292 if (s->listener->counters)
4293 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004294 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004295
4296 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004297 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004298 txn->status = 403;
4299 s->logs.tv_request = now;
4300 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4301 session_inc_http_err_ctr(s);
4302 s->fe->fe_counters.denied_req++;
4303 if (s->fe != s->be)
4304 s->be->be_counters.denied_req++;
4305 if (s->listener->counters)
4306 s->listener->counters->denied_req++;
4307 goto return_prx_cond;
4308
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004309 return_bad_req:
4310 /* We centralize bad requests processing here */
4311 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4312 /* we detected a parsing error. We want to archive this request
4313 * in the dedicated proxy area for later troubleshooting.
4314 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004315 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004317
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004318 txn->req.msg_state = HTTP_MSG_ERROR;
4319 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004320 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004321
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004322 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004323 if (s->listener->counters)
4324 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004325
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004326 return_prx_cond:
4327 if (!(s->flags & SN_ERR_MASK))
4328 s->flags |= SN_ERR_PRXCOND;
4329 if (!(s->flags & SN_FINST_MASK))
4330 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004331
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004332 req->analysers = 0;
4333 req->analyse_exp = TICK_ETERNITY;
4334 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004335
4336 return_prx_yield:
4337 channel_dont_connect(req);
4338 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004339}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004341/* This function performs all the processing enabled for the current request.
4342 * It returns 1 if the processing can continue on next analysers, or zero if it
4343 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004344 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004345 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004346int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004347{
4348 struct http_txn *txn = &s->txn;
4349 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004350 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004351
Willy Tarreau655dce92009-11-08 13:10:58 +01004352 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004353 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004354 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004355 return 0;
4356 }
4357
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004358 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004359 now_ms, __FUNCTION__,
4360 s,
4361 req,
4362 req->rex, req->wex,
4363 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004364 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004365 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004366
William Lallemand82fe75c2012-10-23 10:25:10 +02004367 if (s->fe->comp || s->be->comp)
4368 select_compression_request_header(s, req->buf);
4369
Willy Tarreau59234e92008-11-30 23:51:27 +01004370 /*
4371 * Right now, we know that we have processed the entire headers
4372 * and that unwanted requests have been filtered out. We can do
4373 * whatever we want with the remaining request. Also, now we
4374 * may have separate values for ->fe, ->be.
4375 */
Willy Tarreau06619262006-12-17 08:37:22 +01004376
Willy Tarreau59234e92008-11-30 23:51:27 +01004377 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004378 * If HTTP PROXY is set we simply get remote server address parsing
4379 * incoming request. Note that this requires that a connection is
4380 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004381 */
4382 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004383 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004384 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004385
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004386 /* Note that for now we don't reuse existing proxy connections */
4387 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004388 txn->req.msg_state = HTTP_MSG_ERROR;
4389 txn->status = 500;
4390 req->analysers = 0;
4391 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4392
4393 if (!(s->flags & SN_ERR_MASK))
4394 s->flags |= SN_ERR_RESOURCE;
4395 if (!(s->flags & SN_FINST_MASK))
4396 s->flags |= SN_FINST_R;
4397
4398 return 0;
4399 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004400
4401 path = http_get_path(txn);
4402 url2sa(req->buf->p + msg->sl.rq.u,
4403 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004404 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004405 /* if the path was found, we have to remove everything between
4406 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4407 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4408 * u_l characters by a single "/".
4409 */
4410 if (path) {
4411 char *cur_ptr = req->buf->p;
4412 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4413 int delta;
4414
4415 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4416 http_msg_move_end(&txn->req, delta);
4417 cur_end += delta;
4418 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4419 goto return_bad_req;
4420 }
4421 else {
4422 char *cur_ptr = req->buf->p;
4423 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4424 int delta;
4425
4426 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4427 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4428 http_msg_move_end(&txn->req, delta);
4429 cur_end += delta;
4430 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4431 goto return_bad_req;
4432 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004434
Willy Tarreau59234e92008-11-30 23:51:27 +01004435 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004436 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004437 * Note that doing so might move headers in the request, but
4438 * the fields will stay coherent and the URI will not move.
4439 * This should only be performed in the backend.
4440 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004441 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004442 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4443 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004444
Willy Tarreau59234e92008-11-30 23:51:27 +01004445 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004446 * 8: the appsession cookie was looked up very early in 1.2,
4447 * so let's do the same now.
4448 */
4449
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004450 /* It needs to look into the URI unless persistence must be ignored */
4451 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004452 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 +01004453 }
4454
William Lallemanda73203e2012-03-12 12:48:57 +01004455 /* add unique-id if "header-unique-id" is specified */
4456
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004457 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4458 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4459 goto return_bad_req;
4460 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004461 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004462 }
William Lallemanda73203e2012-03-12 12:48:57 +01004463
4464 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004465 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4466 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004467 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004468 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004469 goto return_bad_req;
4470 }
4471
Cyril Bontéb21570a2009-11-29 20:04:48 +01004472 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 * 9: add X-Forwarded-For if either the frontend or the backend
4474 * asks for it.
4475 */
4476 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004477 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004478 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004479 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4480 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004481 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004482 /* The header is set to be added only if none is present
4483 * and we found it, so don't do anything.
4484 */
4485 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004486 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 /* Add an X-Forwarded-For header unless the source IP is
4488 * in the 'except' network range.
4489 */
4490 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004491 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004492 != s->fe->except_net.s_addr) &&
4493 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004496 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004498 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004499
4500 /* Note: we rely on the backend to get the header name to be used for
4501 * x-forwarded-for, because the header is really meant for the backends.
4502 * However, if the backend did not specify any option, we have to rely
4503 * on the frontend's header name.
4504 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 if (s->be->fwdfor_hdr_len) {
4506 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004507 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004508 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004509 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004511 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004512 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 +01004513
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004514 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004515 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004516 }
4517 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004518 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004519 /* FIXME: for the sake of completeness, we should also support
4520 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004521 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 int len;
4523 char pn[INET6_ADDRSTRLEN];
4524 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004525 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004526 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004527
Willy Tarreau59234e92008-11-30 23:51:27 +01004528 /* Note: we rely on the backend to get the header name to be used for
4529 * x-forwarded-for, because the header is really meant for the backends.
4530 * However, if the backend did not specify any option, we have to rely
4531 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004532 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004533 if (s->be->fwdfor_hdr_len) {
4534 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004535 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004536 } else {
4537 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004538 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004539 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004540 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004541
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004542 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004543 goto return_bad_req;
4544 }
4545 }
4546
4547 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004548 * 10: add X-Original-To if either the frontend or the backend
4549 * asks for it.
4550 */
4551 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4552
4553 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004554 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004555 /* Add an X-Original-To header unless the destination IP is
4556 * in the 'except' network range.
4557 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004558 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004559
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004560 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004561 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004562 (((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 +02004563 != s->fe->except_to.s_addr) &&
4564 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004565 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004566 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004567 int len;
4568 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004569 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004570
4571 /* Note: we rely on the backend to get the header name to be used for
4572 * x-original-to, because the header is really meant for the backends.
4573 * However, if the backend did not specify any option, we have to rely
4574 * on the frontend's header name.
4575 */
4576 if (s->be->orgto_hdr_len) {
4577 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004578 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004579 } else {
4580 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004581 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004582 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004583 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 +02004584
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004585 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004586 goto return_bad_req;
4587 }
4588 }
4589 }
4590
Willy Tarreau50fc7772012-11-11 22:19:57 +01004591 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4592 * If an "Upgrade" token is found, the header is left untouched in order not to have
4593 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4594 * "Upgrade" is present in the Connection header.
4595 */
4596 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4597 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004598 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4599 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004600 unsigned int want_flags = 0;
4601
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004602 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004603 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004604 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4605 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004606 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004607 want_flags |= TX_CON_CLO_SET;
4608 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004609 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004610 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4611 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004612 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004613 want_flags |= TX_CON_KAL_SET;
4614 }
4615
4616 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004617 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004619
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004620
Willy Tarreau522d6c02009-12-06 18:49:18 +01004621 /* If we have no server assigned yet and we're balancing on url_param
4622 * with a POST request, we may be interested in checking the body for
4623 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004624 */
4625 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4626 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004627 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004628 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004629 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004631
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004632 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004633 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004634#ifdef TCP_QUICKACK
4635 /* We expect some data from the client. Unless we know for sure
4636 * we already have a full request, we have to re-enable quick-ack
4637 * in case we previously disabled it, otherwise we might cause
4638 * the client to delay further data.
4639 */
4640 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004641 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004642 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004643 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004644 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004645#endif
4646 }
Willy Tarreau03945942009-12-22 16:50:27 +01004647
Willy Tarreau59234e92008-11-30 23:51:27 +01004648 /*************************************************************
4649 * OK, that's finished for the headers. We have done what we *
4650 * could. Let's switch to the DATA state. *
4651 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004652 req->analyse_exp = TICK_ETERNITY;
4653 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004654
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004655 /* if the server closes the connection, we want to immediately react
4656 * and close the socket to save packets and syscalls.
4657 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004658 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4659 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004660
Willy Tarreau59234e92008-11-30 23:51:27 +01004661 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 /* OK let's go on with the BODY now */
4663 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004664
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004666 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004667 /* we detected a parsing error. We want to archive this request
4668 * in the dedicated proxy area for later troubleshooting.
4669 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004670 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004671 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004672
Willy Tarreau59234e92008-11-30 23:51:27 +01004673 txn->req.msg_state = HTTP_MSG_ERROR;
4674 txn->status = 400;
4675 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004676 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004677
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004678 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004679 if (s->listener->counters)
4680 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004681
Willy Tarreau59234e92008-11-30 23:51:27 +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_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004686 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004687}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004688
Willy Tarreau60b85b02008-11-30 23:28:40 +01004689/* This function is an analyser which processes the HTTP tarpit. It always
4690 * returns zero, at the beginning because it prevents any other processing
4691 * from occurring, and at the end because it terminates the request.
4692 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004693int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004694{
4695 struct http_txn *txn = &s->txn;
4696
4697 /* This connection is being tarpitted. The CLIENT side has
4698 * already set the connect expiration date to the right
4699 * timeout. We just have to check that the client is still
4700 * there and that the timeout has not expired.
4701 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004702 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004703 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004704 !tick_is_expired(req->analyse_exp, now_ms))
4705 return 0;
4706
4707 /* We will set the queue timer to the time spent, just for
4708 * logging purposes. We fake a 500 server error, so that the
4709 * attacker will not suspect his connection has been tarpitted.
4710 * It will not cause trouble to the logs because we can exclude
4711 * the tarpitted connections by filtering on the 'PT' status flags.
4712 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004713 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4714
4715 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004716 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004717 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004718
4719 req->analysers = 0;
4720 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004721
Willy Tarreau60b85b02008-11-30 23:28:40 +01004722 if (!(s->flags & SN_ERR_MASK))
4723 s->flags |= SN_ERR_PRXCOND;
4724 if (!(s->flags & SN_FINST_MASK))
4725 s->flags |= SN_FINST_T;
4726 return 0;
4727}
4728
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004729/* This function is an analyser which waits for the HTTP request body. It waits
4730 * for either the buffer to be full, or the full advertised contents to have
4731 * reached the buffer. It must only be called after the standard HTTP request
4732 * processing has occurred, because it expects the request to be parsed and will
4733 * look for the Expect header. It may send a 100-Continue interim response. It
4734 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4735 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4736 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004737 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004738int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004739{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004740 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004741 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004742
4743 /* We have to parse the HTTP request body to find any required data.
4744 * "balance url_param check_post" should have been the only way to get
4745 * into this. We were brought here after HTTP header analysis, so all
4746 * related structures are ready.
4747 */
4748
Willy Tarreau890988f2014-04-10 11:59:33 +02004749 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4750 /* This is the first call */
4751 if (msg->msg_state < HTTP_MSG_BODY)
4752 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004753
Willy Tarreau890988f2014-04-10 11:59:33 +02004754 if (msg->msg_state < HTTP_MSG_100_SENT) {
4755 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4756 * send an HTTP/1.1 100 Continue intermediate response.
4757 */
4758 if (msg->flags & HTTP_MSGF_VER_11) {
4759 struct hdr_ctx ctx;
4760 ctx.idx = 0;
4761 /* Expect is allowed in 1.1, look for it */
4762 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4763 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004764 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004765 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004766 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004767 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004768 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004769
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004770 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004771 * req->buf->p still points to the beginning of the message. We
4772 * must save the body in msg->next because it survives buffer
4773 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004775 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004776
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004777 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004778 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4779 else
4780 msg->msg_state = HTTP_MSG_DATA;
4781 }
4782
Willy Tarreau890988f2014-04-10 11:59:33 +02004783 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4784 /* We're in content-length mode, we just have to wait for enough data. */
4785 if (req->buf->i - msg->sov < msg->body_len)
4786 goto missing_data;
4787
4788 /* OK we have everything we need now */
4789 goto http_end;
4790 }
4791
4792 /* OK here we're parsing a chunked-encoded message */
4793
Willy Tarreau522d6c02009-12-06 18:49:18 +01004794 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004795 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004796 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004797 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004798 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004799 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004800
Willy Tarreau115acb92009-12-26 13:56:06 +01004801 if (!ret)
4802 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004803 else if (ret < 0) {
4804 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004805 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004806 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004807 }
4808
Willy Tarreaud98cf932009-12-27 22:54:55 +01004809 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004810 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004811 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004812 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004813 if (msg->msg_state == HTTP_MSG_TRAILERS)
4814 goto http_end;
4815
Willy Tarreau226071e2014-04-10 11:55:45 +02004816 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004817 goto http_end;
4818
4819 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004820 /* we get here if we need to wait for more data. If the buffer is full,
4821 * we have the maximum we can expect.
4822 */
4823 if (buffer_full(req->buf, global.tune.maxrewrite))
4824 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004825
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004826 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004827 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004828 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004829
4830 if (!(s->flags & SN_ERR_MASK))
4831 s->flags |= SN_ERR_CLITO;
4832 if (!(s->flags & SN_FINST_MASK))
4833 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004834 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004835 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004836
4837 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004838 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004839 /* Not enough data. We'll re-use the http-request
4840 * timeout here. Ideally, we should set the timeout
4841 * relative to the accept() date. We just set the
4842 * request timeout once at the beginning of the
4843 * request.
4844 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004845 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004846 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004847 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004848 return 0;
4849 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004850
4851 http_end:
4852 /* The situation will not evolve, so let's give up on the analysis. */
4853 s->logs.tv_request = now; /* update the request timer to reflect full request */
4854 req->analysers &= ~an_bit;
4855 req->analyse_exp = TICK_ETERNITY;
4856 return 1;
4857
4858 return_bad_req: /* let's centralize all bad requests */
4859 txn->req.msg_state = HTTP_MSG_ERROR;
4860 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004861 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004862
Willy Tarreau79ebac62010-06-07 13:47:49 +02004863 if (!(s->flags & SN_ERR_MASK))
4864 s->flags |= SN_ERR_PRXCOND;
4865 if (!(s->flags & SN_FINST_MASK))
4866 s->flags |= SN_FINST_R;
4867
Willy Tarreau522d6c02009-12-06 18:49:18 +01004868 return_err_msg:
4869 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004870 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004871 if (s->listener->counters)
4872 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004873 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004874}
4875
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004876/* send a server's name with an outgoing request over an established connection.
4877 * Note: this function is designed to be called once the request has been scheduled
4878 * for being forwarded. This is the reason why it rewinds the buffer before
4879 * proceeding.
4880 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004881int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004882
4883 struct hdr_ctx ctx;
4884
Mark Lamourinec2247f02012-01-04 13:02:01 -05004885 char *hdr_name = be->server_id_hdr_name;
4886 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004887 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004888 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004889 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004890
William Lallemandd9e90662012-01-30 17:27:17 +01004891 ctx.idx = 0;
4892
Willy Tarreau211cdec2014-04-17 20:18:08 +02004893 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004894 if (old_o) {
4895 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004896 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004897 txn->req.next += old_o;
4898 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004899 }
4900
Willy Tarreau9b28e032012-10-12 23:49:43 +02004901 old_i = chn->buf->i;
4902 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 -05004903 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004904 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004905 }
4906
4907 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004908 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004909 memcpy(hdr_val, hdr_name, hdr_name_len);
4910 hdr_val += hdr_name_len;
4911 *hdr_val++ = ':';
4912 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004913 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4914 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004915
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004916 if (old_o) {
4917 /* If this was a forwarded request, we must readjust the amount of
4918 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004919 * variations. Note that the current state is >= HTTP_MSG_BODY,
4920 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004921 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004922 old_o += chn->buf->i - old_i;
4923 b_adv(chn->buf, old_o);
4924 txn->req.next -= old_o;
4925 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004926 }
4927
Mark Lamourinec2247f02012-01-04 13:02:01 -05004928 return 0;
4929}
4930
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931/* Terminate current transaction and prepare a new one. This is very tricky
4932 * right now but it works.
4933 */
4934void http_end_txn_clean_session(struct session *s)
4935{
Willy Tarreau068621e2013-12-23 15:11:25 +01004936 int prev_status = s->txn.status;
4937
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 /* FIXME: We need a more portable way of releasing a backend's and a
4939 * server's connections. We need a safer way to reinitialize buffer
4940 * flags. We also need a more accurate method for computing per-request
4941 * data.
4942 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004943
Willy Tarreau4213a112013-12-15 10:25:42 +01004944 /* unless we're doing keep-alive, we want to quickly close the connection
4945 * to the server.
4946 */
4947 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004948 !si_conn_ready(s->req.cons)) {
4949 s->req.cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4950 si_shutr(s->req.cons);
4951 si_shutw(s->req.cons);
Willy Tarreau4213a112013-12-15 10:25:42 +01004952 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004954 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004955 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004956 if (unlikely(s->srv_conn))
4957 sess_change_server(s, NULL);
4958 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959
4960 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4961 session_process_counters(s);
4962
4963 if (s->txn.status) {
4964 int n;
4965
4966 n = s->txn.status / 100;
4967 if (n < 1 || n > 5)
4968 n = 0;
4969
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004970 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004971 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004972 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004973 s->fe->fe_counters.p.http.comp_rsp++;
4974 }
Willy Tarreau24657792010-02-26 10:30:28 +01004975 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004976 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004977 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004978 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004979 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004980 s->be->be_counters.p.http.comp_rsp++;
4981 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982 }
4983
4984 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004985 s->logs.bytes_in -= s->req.buf->i;
4986 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987
4988 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004989 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 !(s->flags & SN_MONITOR) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004991 (!(s->fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004992 s->do_log(s);
4993 }
4994
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004995 /* stop tracking content-based counters */
4996 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004997 session_update_time_stats(s);
4998
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 s->logs.accept_date = date; /* user-visible date for logging */
5000 s->logs.tv_accept = now; /* corrected date for internal use */
5001 tv_zero(&s->logs.tv_request);
5002 s->logs.t_queue = -1;
5003 s->logs.t_connect = -1;
5004 s->logs.t_data = -1;
5005 s->logs.t_close = 0;
5006 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5007 s->logs.srv_queue_size = 0; /* we will get this number soon */
5008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005009 s->logs.bytes_in = s->req.total = s->req.buf->i;
5010 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005011
5012 if (s->pend_pos)
5013 pendconn_free(s->pend_pos);
5014
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005015 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016 if (s->flags & SN_CURR_SESS) {
5017 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005018 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005020 if (may_dequeue_tasks(objt_server(s->target), s->be))
5021 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 }
5023
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005024 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025
Willy Tarreau4213a112013-12-15 10:25:42 +01005026 /* only release our endpoint if we don't intend to reuse the
5027 * connection.
5028 */
5029 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005030 !si_conn_ready(s->req.cons)) {
5031 si_release_endpoint(s->req.cons);
Willy Tarreau4213a112013-12-15 10:25:42 +01005032 }
5033
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005034 s->req.cons->state = s->req.cons->prev_state = SI_ST_INI;
5035 s->req.cons->err_type = SI_ET_NONE;
5036 s->req.cons->conn_retries = 0; /* used for logging too */
5037 s->req.cons->exp = TICK_ETERNITY;
Willy Tarreaua5f5d8d2014-11-28 11:26:07 +01005038 s->req.cons->flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005039 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);
5040 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 +02005041 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 +01005042 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02005043 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005044
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 s->txn.meth = 0;
5046 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005047 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005048
5049 if (prev_status == 401 || prev_status == 407) {
5050 /* In HTTP keep-alive mode, if we receive a 401, we still have
5051 * a chance of being able to send the visitor again to the same
5052 * server over the same connection. This is required by some
5053 * broken protocols such as NTLM, and anyway whenever there is
5054 * an opportunity for sending the challenge to the proper place,
5055 * it's better to do it (at least it helps with debugging).
5056 */
5057 s->txn.flags |= TX_PREFER_LAST;
5058 }
5059
Willy Tarreauee55dc02010-06-01 10:56:34 +02005060 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005061 s->req.cons->flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062
Willy Tarreau96e31212011-05-30 18:10:30 +02005063 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005064 s->req.flags |= CF_NEVER_WAIT;
5065 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005066 }
5067
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 /* if the request buffer is not empty, it means we're
5069 * about to process another request, so send pending
5070 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005071 * Just don't do this if the buffer is close to be full,
5072 * because the request will wait for it to flush a little
5073 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005075 if (s->req.buf->i) {
5076 if (s->res.buf->o &&
5077 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5078 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5079 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005080 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005081
5082 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005083 channel_auto_read(&s->req);
5084 channel_auto_close(&s->req);
5085 channel_auto_read(&s->res);
5086 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087
Willy Tarreau27375622013-12-17 00:00:28 +01005088 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005089 si_idle_conn(s->req.cons);
Willy Tarreau27375622013-12-17 00:00:28 +01005090
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005091 s->req.analysers = s->listener->analysers;
5092 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093}
5094
5095
5096/* This function updates the request state machine according to the response
5097 * state machine and buffer flags. It returns 1 if it changes anything (flag
5098 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5099 * it is only used to find when a request/response couple is complete. Both
5100 * this function and its equivalent should loop until both return zero. It
5101 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5102 */
5103int http_sync_req_state(struct session *s)
5104{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005105 struct channel *chn = &s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005107 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 unsigned int old_state = txn->req.msg_state;
5109
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5111 return 0;
5112
5113 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005114 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005115 * We can shut the read side unless we want to abort_on_close,
5116 * or we have a POST request. The issue with POST requests is
5117 * that some browsers still send a CRLF after the request, and
5118 * this CRLF must be read so that it does not remain in the kernel
5119 * buffers, otherwise a close could cause an RST on some systems
5120 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005121 * Note that if we're using keep-alive on the client side, we'd
5122 * rather poll now and keep the polling enabled for the whole
5123 * session's life than enabling/disabling it between each
5124 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005125 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005126 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5127 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5128 !(s->be->options & PR_O_ABRT_CLOSE) &&
5129 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131
Willy Tarreau40f151a2012-12-20 12:10:09 +01005132 /* if the server closes the connection, we want to immediately react
5133 * and close the socket to save packets and syscalls.
5134 */
5135 chn->cons->flags |= SI_FL_NOHALF;
5136
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5138 goto wait_other_side;
5139
5140 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5141 /* The server has not finished to respond, so we
5142 * don't want to move in order not to upset it.
5143 */
5144 goto wait_other_side;
5145 }
5146
5147 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5148 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005149 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005151 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152 goto wait_other_side;
5153 }
5154
5155 /* When we get here, it means that both the request and the
5156 * response have finished receiving. Depending on the connection
5157 * mode, we'll have to wait for the last bytes to leave in either
5158 * direction, and sometimes for a close to be effective.
5159 */
5160
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005161 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5162 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005163 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5164 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005165 }
5166 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5167 /* Option forceclose is set, or either side wants to close,
5168 * let's enforce it now that we're not expecting any new
5169 * data to come. The caller knows the session is complete
5170 * once both states are CLOSED.
5171 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005172 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5173 channel_shutr_now(chn);
5174 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005175 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005176 }
5177 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005178 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5179 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005180 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5182 channel_auto_read(chn);
5183 txn->req.msg_state = HTTP_MSG_TUNNEL;
5184 chn->flags |= CF_NEVER_WAIT;
5185 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 }
5187
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005188 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005190 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005191
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005192 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193 txn->req.msg_state = HTTP_MSG_CLOSING;
5194 goto http_msg_closing;
5195 }
5196 else {
5197 txn->req.msg_state = HTTP_MSG_CLOSED;
5198 goto http_msg_closed;
5199 }
5200 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005201 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202 }
5203
5204 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5205 http_msg_closing:
5206 /* nothing else to forward, just waiting for the output buffer
5207 * to be empty and for the shutw_now to take effect.
5208 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005209 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 txn->req.msg_state = HTTP_MSG_CLOSED;
5211 goto http_msg_closed;
5212 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005213 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214 txn->req.msg_state = HTTP_MSG_ERROR;
5215 goto wait_other_side;
5216 }
5217 }
5218
5219 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5220 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005221 /* see above in MSG_DONE why we only do this in these states */
5222 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5223 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5224 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005225 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005226 goto wait_other_side;
5227 }
5228
5229 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005230 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231}
5232
5233
5234/* This function updates the response state machine according to the request
5235 * state machine and buffer flags. It returns 1 if it changes anything (flag
5236 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5237 * it is only used to find when a request/response couple is complete. Both
5238 * this function and its equivalent should loop until both return zero. It
5239 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5240 */
5241int http_sync_res_state(struct session *s)
5242{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005243 struct channel *chn = &s->res;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005245 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 unsigned int old_state = txn->rsp.msg_state;
5247
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5249 return 0;
5250
5251 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5252 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005253 * still monitor the server connection for a possible close
5254 * while the request is being uploaded, so we don't disable
5255 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005257 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258
5259 if (txn->req.msg_state == HTTP_MSG_ERROR)
5260 goto wait_other_side;
5261
5262 if (txn->req.msg_state < HTTP_MSG_DONE) {
5263 /* The client seems to still be sending data, probably
5264 * because we got an error response during an upload.
5265 * We have the choice of either breaking the connection
5266 * or letting it pass through. Let's do the later.
5267 */
5268 goto wait_other_side;
5269 }
5270
5271 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5272 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005273 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005275 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 goto wait_other_side;
5277 }
5278
5279 /* When we get here, it means that both the request and the
5280 * response have finished receiving. Depending on the connection
5281 * mode, we'll have to wait for the last bytes to leave in either
5282 * direction, and sometimes for a close to be effective.
5283 */
5284
5285 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5286 /* Server-close mode : shut read and wait for the request
5287 * side to close its output buffer. The caller will detect
5288 * when we're in DONE and the other is in CLOSED and will
5289 * catch that for the final cleanup.
5290 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005291 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5292 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005293 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005294 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5295 /* Option forceclose is set, or either side wants to close,
5296 * let's enforce it now that we're not expecting any new
5297 * data to come. The caller knows the session is complete
5298 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005300 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5301 channel_shutr_now(chn);
5302 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005303 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 }
5305 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005306 /* The last possible modes are keep-alive and tunnel. Tunnel will
5307 * need to forward remaining data. Keep-alive will need to monitor
5308 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005310 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005311 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005312 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5313 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 }
5315
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005316 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5320 goto http_msg_closing;
5321 }
5322 else {
5323 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5324 goto http_msg_closed;
5325 }
5326 }
5327 goto wait_other_side;
5328 }
5329
5330 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5331 http_msg_closing:
5332 /* nothing else to forward, just waiting for the output buffer
5333 * to be empty and for the shutw_now to take effect.
5334 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5337 goto http_msg_closed;
5338 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005339 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005341 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005342 if (objt_server(s->target))
5343 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 goto wait_other_side;
5345 }
5346 }
5347
5348 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5349 http_msg_closed:
5350 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005351 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005352 channel_auto_close(chn);
5353 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 goto wait_other_side;
5355 }
5356
5357 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005358 /* We force the response to leave immediately if we're waiting for the
5359 * other side, since there is no pending shutdown to push it out.
5360 */
5361 if (!channel_is_empty(chn))
5362 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364}
5365
5366
5367/* Resync the request and response state machines. Return 1 if either state
5368 * changes.
5369 */
5370int http_resync_states(struct session *s)
5371{
5372 struct http_txn *txn = &s->txn;
5373 int old_req_state = txn->req.msg_state;
5374 int old_res_state = txn->rsp.msg_state;
5375
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 http_sync_req_state(s);
5377 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 if (!http_sync_res_state(s))
5379 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 if (!http_sync_req_state(s))
5381 break;
5382 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005383
Willy Tarreau610ecce2010-01-04 21:15:02 +01005384 /* OK, both state machines agree on a compatible state.
5385 * There are a few cases we're interested in :
5386 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5387 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5388 * directions, so let's simply disable both analysers.
5389 * - HTTP_MSG_CLOSED on the response only means we must abort the
5390 * request.
5391 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5392 * with server-close mode means we've completed one request and we
5393 * must re-initialize the server connection.
5394 */
5395
5396 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5397 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5398 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5399 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005400 s->req.analysers = 0;
5401 channel_auto_close(&s->req);
5402 channel_auto_read(&s->req);
5403 s->res.analysers = 0;
5404 channel_auto_close(&s->res);
5405 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005407 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005408 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005409 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005410 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005411 s->res.analysers = 0;
5412 channel_auto_close(&s->res);
5413 channel_auto_read(&s->res);
5414 s->req.analysers = 0;
5415 channel_abort(&s->req);
5416 channel_auto_close(&s->req);
5417 channel_auto_read(&s->req);
5418 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005420 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5421 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005423 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5424 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5425 /* server-close/keep-alive: terminate this transaction,
5426 * possibly killing the server connection and reinitialize
5427 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005428 */
5429 http_end_txn_clean_session(s);
5430 }
5431
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 return txn->req.msg_state != old_req_state ||
5433 txn->rsp.msg_state != old_res_state;
5434}
5435
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436/* This function is an analyser which forwards request body (including chunk
5437 * sizes if any). It is called as soon as we must forward, even if we forward
5438 * zero byte. The only situation where it must not be called is when we're in
5439 * tunnel mode and we want to forward till the close. It's used both to forward
5440 * remaining data and to resync after end of body. It expects the msg_state to
5441 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5442 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005443 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005444 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005446int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447{
5448 struct http_txn *txn = &s->txn;
5449 struct http_msg *msg = &s->txn.req;
5450
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005451 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5452 return 0;
5453
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005454 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005455 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005456 /* Output closed while we were sending data. We must abort and
5457 * wake the other side up.
5458 */
5459 msg->msg_state = HTTP_MSG_ERROR;
5460 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005461 return 1;
5462 }
5463
Willy Tarreaud98cf932009-12-27 22:54:55 +01005464 /* Note that we don't have to send 100-continue back because we don't
5465 * need the data to complete our job, and it's up to the server to
5466 * decide whether to return 100, 417 or anything else in return of
5467 * an "Expect: 100-continue" header.
5468 */
5469
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005470 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005471 /* we have msg->sov which points to the first byte of message
5472 * body, and req->buf.p still points to the beginning of the
5473 * message. We forward the headers now, as we don't need them
5474 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005475 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005476 b_adv(req->buf, msg->sov);
5477 msg->next -= msg->sov;
5478 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005479
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005480 /* The previous analysers guarantee that the state is somewhere
5481 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5482 * msg->next are always correct.
5483 */
5484 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5485 if (msg->flags & HTTP_MSGF_TE_CHNK)
5486 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5487 else
5488 msg->msg_state = HTTP_MSG_DATA;
5489 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005490 }
5491
Willy Tarreau7ba23542014-04-17 21:50:00 +02005492 /* Some post-connect processing might want us to refrain from starting to
5493 * forward data. Currently, the only reason for this is "balance url_param"
5494 * whichs need to parse/process the request after we've enabled forwarding.
5495 */
5496 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005497 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005498 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005499 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005500 goto missing_data;
5501 }
5502 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5503 }
5504
Willy Tarreau80a92c02014-03-12 10:41:13 +01005505 /* in most states, we should abort in case of early close */
5506 channel_auto_close(req);
5507
Willy Tarreauefdf0942014-04-24 20:08:57 +02005508 if (req->to_forward) {
5509 /* We can't process the buffer's contents yet */
5510 req->flags |= CF_WAKE_WRITE;
5511 goto missing_data;
5512 }
5513
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005515 if (msg->msg_state == HTTP_MSG_DATA) {
5516 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005517 /* we may have some pending data starting at req->buf->p */
5518 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005519 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005520 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005521 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005522 msg->next += msg->chunk_len;
5523 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005524
5525 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005526 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005527 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005529 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005530 }
5531 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005532 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005533 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534 * TRAILERS state.
5535 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005536 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537
Willy Tarreau54d23df2012-10-25 19:04:45 +02005538 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005540 else if (ret < 0) {
5541 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005542 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005543 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005544 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005545 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005547 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005548 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005549 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005550 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551
5552 if (ret == 0)
5553 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005554 else if (ret < 0) {
5555 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005556 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005557 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005558 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005559 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005560 /* we're in MSG_CHUNK_SIZE now */
5561 }
5562 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005563 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005564
5565 if (ret == 0)
5566 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005567 else if (ret < 0) {
5568 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005569 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005570 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005572 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573 /* we're in HTTP_MSG_DONE now */
5574 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005576 int old_state = msg->msg_state;
5577
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005579
5580 /* we may have some pending data starting at req->buf->p
5581 * such as last chunk of data or trailers.
5582 */
5583 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005584 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005585 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005586 msg->next = 0;
5587
Willy Tarreau4fe41902010-06-07 22:27:41 +02005588 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005589 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5590 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005591 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 if (http_resync_states(s)) {
5593 /* some state changes occurred, maybe the analyser
5594 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005595 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005596 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005597 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005598 /* request errors are most likely due to
5599 * the server aborting the transfer.
5600 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005601 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005602 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005603 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005604 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005606 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005607 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005608 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005609
5610 /* If "option abortonclose" is set on the backend, we
5611 * want to monitor the client's connection and forward
5612 * any shutdown notification to the server, which will
5613 * decide whether to close or to go on processing the
5614 * request.
5615 */
5616 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005617 channel_auto_read(req);
5618 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005619 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005620 else if (s->txn.meth == HTTP_METH_POST) {
5621 /* POST requests may require to read extra CRLF
5622 * sent by broken browsers and which could cause
5623 * an RST to be sent upon close on some systems
5624 * (eg: Linux).
5625 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005626 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005627 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005628
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005630 }
5631 }
5632
Willy Tarreaud98cf932009-12-27 22:54:55 +01005633 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005634 /* we may have some pending data starting at req->buf->p */
5635 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005636 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005637 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5638
Willy Tarreaubed410e2014-04-22 08:19:34 +02005639 msg->next = 0;
5640 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5641
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005642 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005643 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005644 if (!(s->flags & SN_ERR_MASK))
5645 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005646 if (!(s->flags & SN_FINST_MASK)) {
5647 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5648 s->flags |= SN_FINST_H;
5649 else
5650 s->flags |= SN_FINST_D;
5651 }
5652
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005653 s->fe->fe_counters.cli_aborts++;
5654 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005655 if (objt_server(s->target))
5656 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005657
5658 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005659 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005660
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005661 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005662 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005663 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005664
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005665 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005666 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005667 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005668 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005669 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005670
Willy Tarreau5c620922011-05-11 19:56:11 +02005671 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005672 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005673 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005674 * modes are already handled by the stream sock layer. We must not do
5675 * this in content-length mode because it could present the MSG_MORE
5676 * flag with the last block of forwarded data, which would cause an
5677 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005678 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005679 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005680 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005681
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005682 return 0;
5683
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005685 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005686 if (s->listener->counters)
5687 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005688
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005689 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005690 /* we may have some pending data starting at req->buf->p */
5691 b_adv(req->buf, msg->next);
5692 msg->next = 0;
5693
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005694 txn->req.msg_state = HTTP_MSG_ERROR;
5695 if (txn->status) {
5696 /* Note: we don't send any error if some data were already sent */
5697 stream_int_retnclose(req->prod, NULL);
5698 } else {
5699 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005700 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005701 }
5702 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005703 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005704
5705 if (!(s->flags & SN_ERR_MASK))
5706 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707 if (!(s->flags & SN_FINST_MASK)) {
5708 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5709 s->flags |= SN_FINST_H;
5710 else
5711 s->flags |= SN_FINST_D;
5712 }
5713 return 0;
5714
5715 aborted_xfer:
5716 txn->req.msg_state = HTTP_MSG_ERROR;
5717 if (txn->status) {
5718 /* Note: we don't send any error if some data were already sent */
5719 stream_int_retnclose(req->prod, NULL);
5720 } else {
5721 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005722 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005723 }
5724 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005725 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005726
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005727 s->fe->fe_counters.srv_aborts++;
5728 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005729 if (objt_server(s->target))
5730 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005731
5732 if (!(s->flags & SN_ERR_MASK))
5733 s->flags |= SN_ERR_SRVCL;
5734 if (!(s->flags & SN_FINST_MASK)) {
5735 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5736 s->flags |= SN_FINST_H;
5737 else
5738 s->flags |= SN_FINST_D;
5739 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 return 0;
5741}
5742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005743/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5744 * processing can continue on next analysers, or zero if it either needs more
5745 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005746 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747 * when it has nothing left to do, and may remove any analyser when it wants to
5748 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005749 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005750int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005751{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752 struct http_txn *txn = &s->txn;
5753 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005754 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005755 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005756 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005757 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005758
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005759 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005760 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005761 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005762 rep,
5763 rep->rex, rep->wex,
5764 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005765 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005766 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 /*
5769 * Now parse the partial (or complete) lines.
5770 * We will check the response syntax, and also join multi-line
5771 * headers. An index of all the lines will be elaborated while
5772 * parsing.
5773 *
5774 * For the parsing, we use a 28 states FSM.
5775 *
5776 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005777 * rep->buf->p = beginning of response
5778 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5779 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005780 * msg->eol = end of current header or line (LF or CRLF)
5781 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 */
5783
Willy Tarreau628c40c2014-04-24 19:11:26 +02005784 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005785 /* There's a protected area at the end of the buffer for rewriting
5786 * purposes. We don't want to start to parse the request if the
5787 * protected area is affected, because we may have to move processed
5788 * data later, which is much more complicated.
5789 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005790 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005791 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005792 /* some data has still not left the buffer, wake us once that's done */
5793 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5794 goto abort_response;
5795 channel_dont_close(rep);
5796 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005797 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005798 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005799 }
5800
Willy Tarreau379357a2013-06-08 12:55:46 +02005801 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5802 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5803 buffer_slow_realign(rep->buf);
5804
Willy Tarreau9b28e032012-10-12 23:49:43 +02005805 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005806 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005807 }
5808
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809 /* 1: we might have to print this header in debug mode */
5810 if (unlikely((global.mode & MODE_DEBUG) &&
5811 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005812 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005813 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814
Willy Tarreau9b28e032012-10-12 23:49:43 +02005815 sol = rep->buf->p;
5816 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 sol += hdr_idx_first_pos(&txn->hdr_idx);
5820 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005821
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005822 while (cur_idx) {
5823 eol = sol + txn->hdr_idx.v[cur_idx].len;
5824 debug_hdr("srvhdr", s, sol, eol);
5825 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5826 cur_idx = txn->hdr_idx.v[cur_idx].next;
5827 }
5828 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005830 /*
5831 * Now we quickly check if we have found a full valid response.
5832 * If not so, we check the FD and buffer states before leaving.
5833 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005834 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005835 * responses are checked first.
5836 *
5837 * Depending on whether the client is still there or not, we
5838 * may send an error response back or not. Note that normally
5839 * we should only check for HTTP status there, and check I/O
5840 * errors somewhere else.
5841 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005842
Willy Tarreau655dce92009-11-08 13:10:58 +01005843 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005844 /* Invalid response */
5845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5846 /* we detected a parsing error. We want to archive this response
5847 * in the dedicated proxy area for later troubleshooting.
5848 */
5849 hdr_response_bad:
5850 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005851 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005852
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005853 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005854 if (objt_server(s->target)) {
5855 objt_server(s->target)->counters.failed_resp++;
5856 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005857 }
Willy Tarreau64648412010-03-05 10:41:54 +01005858 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005859 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 rep->analysers = 0;
5861 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005862 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005863 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005864 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865
5866 if (!(s->flags & SN_ERR_MASK))
5867 s->flags |= SN_ERR_PRXCOND;
5868 if (!(s->flags & SN_FINST_MASK))
5869 s->flags |= SN_FINST_H;
5870
5871 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005875 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005876 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005877 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005878 goto hdr_response_bad;
5879 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005880
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005882 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005884 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005885 else if (txn->flags & TX_NOT_FIRST)
5886 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005887
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005888 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005889 if (objt_server(s->target)) {
5890 objt_server(s->target)->counters.failed_resp++;
5891 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005892 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005893
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005894 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 rep->analysers = 0;
5896 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005897 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005898 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005899 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005900
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 if (!(s->flags & SN_ERR_MASK))
5902 s->flags |= SN_ERR_SRVCL;
5903 if (!(s->flags & SN_FINST_MASK))
5904 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005905 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005906 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005907
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005908 /* read timeout : return a 504 to the client. */
5909 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005910 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005911 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005912 else if (txn->flags & TX_NOT_FIRST)
5913 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005914
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005915 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005916 if (objt_server(s->target)) {
5917 objt_server(s->target)->counters.failed_resp++;
5918 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005919 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005920
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005921 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005922 rep->analysers = 0;
5923 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005924 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005925 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005926 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005927
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928 if (!(s->flags & SN_ERR_MASK))
5929 s->flags |= SN_ERR_SRVTO;
5930 if (!(s->flags & SN_FINST_MASK))
5931 s->flags |= SN_FINST_H;
5932 return 0;
5933 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005934
Willy Tarreauf003d372012-11-26 13:35:37 +01005935 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005936 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005937 s->fe->fe_counters.cli_aborts++;
5938 s->be->be_counters.cli_aborts++;
5939 if (objt_server(s->target))
5940 objt_server(s->target)->counters.cli_aborts++;
5941
5942 rep->analysers = 0;
5943 channel_auto_close(rep);
5944
5945 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005946 channel_truncate(rep);
Willy Tarreauf003d372012-11-26 13:35:37 +01005947 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5948
5949 if (!(s->flags & SN_ERR_MASK))
5950 s->flags |= SN_ERR_CLICL;
5951 if (!(s->flags & SN_FINST_MASK))
5952 s->flags |= SN_FINST_H;
5953
5954 /* process_session() will take care of the error */
5955 return 0;
5956 }
5957
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005958 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005959 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005960 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005961 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005962 else if (txn->flags & TX_NOT_FIRST)
5963 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005964
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005965 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005966 if (objt_server(s->target)) {
5967 objt_server(s->target)->counters.failed_resp++;
5968 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005969 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005970
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005971 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 rep->analysers = 0;
5973 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005974 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005975 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005976 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005977
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 if (!(s->flags & SN_ERR_MASK))
5979 s->flags |= SN_ERR_SRVCL;
5980 if (!(s->flags & SN_FINST_MASK))
5981 s->flags |= SN_FINST_H;
5982 return 0;
5983 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005986 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005987 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005988 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005989 else if (txn->flags & TX_NOT_FIRST)
5990 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005991
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005992 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005994 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995
5996 if (!(s->flags & SN_ERR_MASK))
5997 s->flags |= SN_ERR_CLICL;
5998 if (!(s->flags & SN_FINST_MASK))
5999 s->flags |= SN_FINST_H;
6000
6001 /* process_session() will take care of the error */
6002 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006003 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006004
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006005 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006006 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 return 0;
6008 }
6009
6010 /* More interesting part now : we know that we have a complete
6011 * response which at least looks like HTTP. We have an indicator
6012 * of each header's length, so we can parse them quickly.
6013 */
6014
6015 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006016 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006017
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006018 /*
6019 * 1: get the status code
6020 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006021 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 if (n < 1 || n > 5)
6023 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006024 /* when the client triggers a 4xx from the server, it's most often due
6025 * to a missing object or permission. These events should be tracked
6026 * because if they happen often, it may indicate a brute force or a
6027 * vulnerability scan.
6028 */
6029 if (n == 4)
6030 session_inc_http_err_ctr(s);
6031
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006032 if (objt_server(s->target))
6033 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034
Willy Tarreau5b154472009-12-21 20:11:07 +01006035 /* check if the response is HTTP/1.1 or above */
6036 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006037 ((rep->buf->p[5] > '1') ||
6038 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006039 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006040
6041 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006042 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 +01006043
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006044 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006045 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006046
Willy Tarreau9b28e032012-10-12 23:49:43 +02006047 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048
Willy Tarreau39650402010-03-15 19:44:39 +01006049 /* Adjust server's health based on status code. Note: status codes 501
6050 * and 505 are triggered on demand by client request, so we must not
6051 * count them as server failures.
6052 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006053 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006054 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006055 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006056 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006057 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006058 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 /*
6061 * 2: check for cacheability.
6062 */
6063
6064 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006065 case 100:
6066 /*
6067 * We may be facing a 100-continue response, in which case this
6068 * is not the right response, and we're waiting for the next one.
6069 * Let's allow this response to go to the client and wait for the
6070 * next one.
6071 */
6072 hdr_idx_init(&txn->hdr_idx);
6073 msg->next -= channel_forward(rep, msg->next);
6074 msg->msg_state = HTTP_MSG_RPBEFORE;
6075 txn->status = 0;
6076 s->logs.t_data = -1; /* was not a response yet */
6077 goto next_one;
6078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006079 case 200:
6080 case 203:
6081 case 206:
6082 case 300:
6083 case 301:
6084 case 410:
6085 /* RFC2616 @13.4:
6086 * "A response received with a status code of
6087 * 200, 203, 206, 300, 301 or 410 MAY be stored
6088 * by a cache (...) unless a cache-control
6089 * directive prohibits caching."
6090 *
6091 * RFC2616 @9.5: POST method :
6092 * "Responses to this method are not cacheable,
6093 * unless the response includes appropriate
6094 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006095 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006096 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006097 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6099 break;
6100 default:
6101 break;
6102 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 /*
6105 * 3: we may need to capture headers
6106 */
6107 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006108 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006109 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006110 txn->rsp.cap, s->fe->rsp_cap);
6111
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006112 /* 4: determine the transfer-length.
6113 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6114 * the presence of a message-body in a RESPONSE and its transfer length
6115 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006116 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006117 * All responses to the HEAD request method MUST NOT include a
6118 * message-body, even though the presence of entity-header fields
6119 * might lead one to believe they do. All 1xx (informational), 204
6120 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6121 * message-body. All other responses do include a message-body,
6122 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006123 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006124 * 1. Any response which "MUST NOT" include a message-body (such as the
6125 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6126 * always terminated by the first empty line after the header fields,
6127 * regardless of the entity-header fields present in the message.
6128 *
6129 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6130 * the "chunked" transfer-coding (Section 6.2) is used, the
6131 * transfer-length is defined by the use of this transfer-coding.
6132 * If a Transfer-Encoding header field is present and the "chunked"
6133 * transfer-coding is not present, the transfer-length is defined by
6134 * the sender closing the connection.
6135 *
6136 * 3. If a Content-Length header field is present, its decimal value in
6137 * OCTETs represents both the entity-length and the transfer-length.
6138 * If a message is received with both a Transfer-Encoding header
6139 * field and a Content-Length header field, the latter MUST be ignored.
6140 *
6141 * 4. If the message uses the media type "multipart/byteranges", and
6142 * the transfer-length is not otherwise specified, then this self-
6143 * delimiting media type defines the transfer-length. This media
6144 * type MUST NOT be used unless the sender knows that the recipient
6145 * can parse it; the presence in a request of a Range header with
6146 * multiple byte-range specifiers from a 1.1 client implies that the
6147 * client can parse multipart/byteranges responses.
6148 *
6149 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150 */
6151
6152 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006153 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006154 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155 * FIXME: should we parse anyway and return an error on chunked encoding ?
6156 */
6157 if (txn->meth == HTTP_METH_HEAD ||
6158 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006159 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006160 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006161 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006162 goto skip_content_length;
6163 }
6164
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006165 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006166 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006167 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006168 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006169 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006170 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6171 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006172 /* bad transfer-encoding (chunked followed by something else) */
6173 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006174 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006175 break;
6176 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006177 }
6178
6179 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6180 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006181 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006182 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006183 signed long long cl;
6184
Willy Tarreauad14f752011-09-02 20:33:27 +02006185 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006186 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006187 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006188 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006189
Willy Tarreauad14f752011-09-02 20:33:27 +02006190 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006191 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006192 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006193 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006194
Willy Tarreauad14f752011-09-02 20:33:27 +02006195 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006196 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006197 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006198 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006199
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006200 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006201 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006202 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006203 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006204
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006205 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006206 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006207 }
6208
William Lallemand82fe75c2012-10-23 10:25:10 +02006209 if (s->fe->comp || s->be->comp)
6210 select_compression_response_header(s, rep->buf);
6211
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006212skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006213 /* Now we have to check if we need to modify the Connection header.
6214 * This is more difficult on the response than it is on the request,
6215 * because we can have two different HTTP versions and we don't know
6216 * how the client will interprete a response. For instance, let's say
6217 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6218 * HTTP/1.1 response without any header. Maybe it will bound itself to
6219 * HTTP/1.0 because it only knows about it, and will consider the lack
6220 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6221 * the lack of header as a keep-alive. Thus we will use two flags
6222 * indicating how a request MAY be understood by the client. In case
6223 * of multiple possibilities, we'll fix the header to be explicit. If
6224 * ambiguous cases such as both close and keepalive are seen, then we
6225 * will fall back to explicit close. Note that we won't take risks with
6226 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006227 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006228 */
6229
Willy Tarreaudc008c52010-02-01 16:20:08 +01006230 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6231 txn->status == 101)) {
6232 /* Either we've established an explicit tunnel, or we're
6233 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006234 * to understand the next protocols. We have to switch to tunnel
6235 * mode, so that we transfer the request and responses then let
6236 * this protocol pass unmodified. When we later implement specific
6237 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006238 * header which contains information about that protocol for
6239 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006240 */
6241 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6242 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006243 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6244 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006245 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6246 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006247 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006248
Willy Tarreau70dffda2014-01-30 03:07:23 +01006249 /* this situation happens when combining pretend-keepalive with httpclose. */
6250 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006251 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6252 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006253 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6254
Willy Tarreau60466522010-01-18 19:08:45 +01006255 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006256 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006257 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6258 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006259
Willy Tarreau60466522010-01-18 19:08:45 +01006260 /* now adjust header transformations depending on current state */
6261 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6262 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6263 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006264 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006265 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006266 }
Willy Tarreau60466522010-01-18 19:08:45 +01006267 else { /* SCL / KAL */
6268 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006269 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006270 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006271 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006272
Willy Tarreau60466522010-01-18 19:08:45 +01006273 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006274 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006275
Willy Tarreau60466522010-01-18 19:08:45 +01006276 /* Some keep-alive responses are converted to Server-close if
6277 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006278 */
Willy Tarreau60466522010-01-18 19:08:45 +01006279 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6280 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006281 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006282 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006283 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006284 }
6285
Willy Tarreau7959a552013-09-23 16:44:27 +02006286 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006287 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006288
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006289 /* end of job, return OK */
6290 rep->analysers &= ~an_bit;
6291 rep->analyse_exp = TICK_ETERNITY;
6292 channel_auto_close(rep);
6293 return 1;
6294
6295 abort_keep_alive:
6296 /* A keep-alive request to the server failed on a network error.
6297 * The client is required to retry. We need to close without returning
6298 * any other information so that the client retries.
6299 */
6300 txn->status = 0;
6301 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006302 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006303 channel_auto_close(rep);
6304 s->logs.logwait = 0;
6305 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006306 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006307 channel_truncate(rep);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006308 stream_int_retnclose(rep->cons, NULL);
6309 return 0;
6310}
6311
6312/* This function performs all the processing enabled for the current response.
6313 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006314 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006315 * other functions. It works like process_request (see indications above).
6316 */
6317int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6318{
6319 struct http_txn *txn = &s->txn;
6320 struct http_msg *msg = &txn->rsp;
6321 struct proxy *cur_proxy;
6322 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006323 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006324
6325 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6326 now_ms, __FUNCTION__,
6327 s,
6328 rep,
6329 rep->rex, rep->wex,
6330 rep->flags,
6331 rep->buf->i,
6332 rep->analysers);
6333
6334 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6335 return 0;
6336
Willy Tarreau70730dd2014-04-24 18:06:27 +02006337 /* The stats applet needs to adjust the Connection header but we don't
6338 * apply any filter there.
6339 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006340 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6341 rep->analysers &= ~an_bit;
6342 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006343 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006344 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006345
Willy Tarreau58975672014-04-24 21:13:57 +02006346 /*
6347 * We will have to evaluate the filters.
6348 * As opposed to version 1.2, now they will be evaluated in the
6349 * filters order and not in the header order. This means that
6350 * each filter has to be validated among all headers.
6351 *
6352 * Filters are tried with ->be first, then with ->fe if it is
6353 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006354 *
6355 * Maybe we are in resume condiion. In this case I choose the
6356 * "struct proxy" which contains the rule list matching the resume
6357 * pointer. If none of theses "struct proxy" match, I initialise
6358 * the process with the first one.
6359 *
6360 * In fact, I check only correspondance betwwen the current list
6361 * pointer and the ->fe rule list. If it doesn't match, I initialize
6362 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006363 */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006364 if (s->current_rule_list == &s->fe->http_res_rules)
6365 cur_proxy = s->fe;
6366 else
6367 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006368 while (1) {
6369 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006370
Willy Tarreau58975672014-04-24 21:13:57 +02006371 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006372 if (ret == HTTP_RULE_RES_CONT)
6373 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006374
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006375 /* we need to be called again. */
6376 if (ret == HTTP_RULE_RES_YIELD) {
6377 channel_dont_close(rep);
6378 return 0;
6379 }
6380
Willy Tarreau58975672014-04-24 21:13:57 +02006381 /* try headers filters */
6382 if (rule_set->rsp_exp != NULL) {
6383 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6384 return_bad_resp:
6385 if (objt_server(s->target)) {
6386 objt_server(s->target)->counters.failed_resp++;
6387 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006388 }
Willy Tarreau58975672014-04-24 21:13:57 +02006389 s->be->be_counters.failed_resp++;
6390 return_srv_prx_502:
6391 rep->analysers = 0;
6392 txn->status = 502;
6393 s->logs.t_data = -1; /* was not a valid response */
6394 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006395 channel_truncate(rep);
Willy Tarreau58975672014-04-24 21:13:57 +02006396 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6397 if (!(s->flags & SN_ERR_MASK))
6398 s->flags |= SN_ERR_PRXCOND;
6399 if (!(s->flags & SN_FINST_MASK))
6400 s->flags |= SN_FINST_H;
6401 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006402 }
Willy Tarreau58975672014-04-24 21:13:57 +02006403 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006404
Willy Tarreau58975672014-04-24 21:13:57 +02006405 /* has the response been denied ? */
6406 if (txn->flags & TX_SVDENY) {
6407 if (objt_server(s->target))
6408 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006409
Willy Tarreau58975672014-04-24 21:13:57 +02006410 s->be->be_counters.denied_resp++;
6411 s->fe->fe_counters.denied_resp++;
6412 if (s->listener->counters)
6413 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 goto return_srv_prx_502;
6416 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 /* add response headers from the rule sets in the same order */
6419 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006420 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006421 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006422 if (wl->cond) {
6423 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6424 ret = acl_pass(ret);
6425 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6426 ret = !ret;
6427 if (!ret)
6428 continue;
6429 }
6430 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6431 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006432 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006433
Willy Tarreau58975672014-04-24 21:13:57 +02006434 /* check whether we're already working on the frontend */
6435 if (cur_proxy == s->fe)
6436 break;
6437 cur_proxy = s->fe;
6438 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006439
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006440 /* After this point, this anayzer can't return yield, so we can
6441 * remove the bit corresponding to this analyzer from the list.
6442 *
6443 * Note that the intermediate returns and goto found previously
6444 * reset the analyzers.
6445 */
6446 rep->analysers &= ~an_bit;
6447 rep->analyse_exp = TICK_ETERNITY;
6448
Willy Tarreau58975672014-04-24 21:13:57 +02006449 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006450 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006451 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006452
Willy Tarreau58975672014-04-24 21:13:57 +02006453 /*
6454 * Now check for a server cookie.
6455 */
6456 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6457 (s->be->options & PR_O_CHK_CACHE))
6458 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006459
Willy Tarreau58975672014-04-24 21:13:57 +02006460 /*
6461 * Check for cache-control or pragma headers if required.
6462 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006463 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 +02006464 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006465
Willy Tarreau58975672014-04-24 21:13:57 +02006466 /*
6467 * Add server cookie in the response if needed
6468 */
6469 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6470 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6471 (!(s->flags & SN_DIRECT) ||
6472 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6473 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6474 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6475 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6476 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6477 !(s->flags & SN_IGNORE_PRST)) {
6478 /* the server is known, it's not the one the client requested, or the
6479 * cookie's last seen date needs to be refreshed. We have to
6480 * insert a set-cookie here, except if we want to insert only on POST
6481 * requests and this one isn't. Note that servers which don't have cookies
6482 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483 */
Willy Tarreau58975672014-04-24 21:13:57 +02006484 if (!objt_server(s->target)->cookie) {
6485 chunk_printf(&trash,
6486 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6487 s->be->cookie_name);
6488 }
6489 else {
6490 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006491
Willy Tarreau58975672014-04-24 21:13:57 +02006492 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6493 /* emit last_date, which is mandatory */
6494 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6495 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6496 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006497
Willy Tarreau58975672014-04-24 21:13:57 +02006498 if (s->be->cookie_maxlife) {
6499 /* emit first_date, which is either the original one or
6500 * the current date.
6501 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006502 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006503 s30tob64(txn->cookie_first_date ?
6504 txn->cookie_first_date >> 2 :
6505 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006506 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006507 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006508 }
Willy Tarreau58975672014-04-24 21:13:57 +02006509 chunk_appendf(&trash, "; path=/");
6510 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006511
Willy Tarreau58975672014-04-24 21:13:57 +02006512 if (s->be->cookie_domain)
6513 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006514
Willy Tarreau58975672014-04-24 21:13:57 +02006515 if (s->be->ck_opts & PR_CK_HTTPONLY)
6516 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006517
Willy Tarreau58975672014-04-24 21:13:57 +02006518 if (s->be->ck_opts & PR_CK_SECURE)
6519 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006520
Willy Tarreau58975672014-04-24 21:13:57 +02006521 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6522 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006523
Willy Tarreau58975672014-04-24 21:13:57 +02006524 txn->flags &= ~TX_SCK_MASK;
6525 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6526 /* the server did not change, only the date was updated */
6527 txn->flags |= TX_SCK_UPDATED;
6528 else
6529 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006530
Willy Tarreau58975672014-04-24 21:13:57 +02006531 /* Here, we will tell an eventual cache on the client side that we don't
6532 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6533 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6534 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006535 */
Willy Tarreau58975672014-04-24 21:13:57 +02006536 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006537
Willy Tarreau58975672014-04-24 21:13:57 +02006538 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6541 "Cache-control: private", 22) < 0))
6542 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006543 }
Willy Tarreau58975672014-04-24 21:13:57 +02006544 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545
Willy Tarreau58975672014-04-24 21:13:57 +02006546 /*
6547 * Check if result will be cacheable with a cookie.
6548 * We'll block the response if security checks have caught
6549 * nasty things such as a cacheable cookie.
6550 */
6551 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6552 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6553 (s->be->options & PR_O_CHK_CACHE)) {
6554 /* we're in presence of a cacheable response containing
6555 * a set-cookie header. We'll block it as requested by
6556 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 */
Willy Tarreau58975672014-04-24 21:13:57 +02006558 if (objt_server(s->target))
6559 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006560
Willy Tarreau58975672014-04-24 21:13:57 +02006561 s->be->be_counters.denied_resp++;
6562 s->fe->fe_counters.denied_resp++;
6563 if (s->listener->counters)
6564 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565
Willy Tarreau58975672014-04-24 21:13:57 +02006566 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6567 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6568 send_log(s->be, LOG_ALERT,
6569 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6570 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6571 goto return_srv_prx_502;
6572 }
Willy Tarreau03945942009-12-22 16:50:27 +01006573
Willy Tarreau70730dd2014-04-24 18:06:27 +02006574 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006575 /*
6576 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6577 * If an "Upgrade" token is found, the header is left untouched in order
6578 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006579 * if anything but "Upgrade" is present in the Connection header. We don't
6580 * want to touch any 101 response either since it's switching to another
6581 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006582 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006583 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006584 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6585 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6586 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6587 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006588
Willy Tarreau58975672014-04-24 21:13:57 +02006589 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6590 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6591 /* we want a keep-alive response here. Keep-alive header
6592 * required if either side is not 1.1.
6593 */
6594 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6595 want_flags |= TX_CON_KAL_SET;
6596 }
6597 else {
6598 /* we want a close response here. Close header required if
6599 * the server is 1.1, regardless of the client.
6600 */
6601 if (msg->flags & HTTP_MSGF_VER_11)
6602 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006603 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604
Willy Tarreau58975672014-04-24 21:13:57 +02006605 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6606 http_change_connection_header(txn, msg, want_flags);
6607 }
6608
6609 skip_header_mangling:
6610 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6611 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6612 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613
Willy Tarreau58975672014-04-24 21:13:57 +02006614 /* if the user wants to log as soon as possible, without counting
6615 * bytes from the server, then this is the right moment. We have
6616 * to temporarily assign bytes_out to log what we currently have.
6617 */
6618 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6619 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6620 s->logs.bytes_out = txn->rsp.eoh;
6621 s->do_log(s);
6622 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006623 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006624 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006625}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626
Willy Tarreaud98cf932009-12-27 22:54:55 +01006627/* This function is an analyser which forwards response body (including chunk
6628 * sizes if any). It is called as soon as we must forward, even if we forward
6629 * zero byte. The only situation where it must not be called is when we're in
6630 * tunnel mode and we want to forward till the close. It's used both to forward
6631 * remaining data and to resync after end of body. It expects the msg_state to
6632 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6633 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006634 *
6635 * It is capable of compressing response data both in content-length mode and
6636 * in chunked mode. The state machines follows different flows depending on
6637 * whether content-length and chunked modes are used, since there are no
6638 * trailers in content-length :
6639 *
6640 * chk-mode cl-mode
6641 * ,----- BODY -----.
6642 * / \
6643 * V size > 0 V chk-mode
6644 * .--> SIZE -------------> DATA -------------> CRLF
6645 * | | size == 0 | last byte |
6646 * | v final crlf v inspected |
6647 * | TRAILERS -----------> DONE |
6648 * | |
6649 * `----------------------------------------------'
6650 *
6651 * Compression only happens in the DATA state, and must be flushed in final
6652 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6653 * is performed at once on final states for all bytes parsed, or when leaving
6654 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006655 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006656int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006657{
6658 struct http_txn *txn = &s->txn;
6659 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006660 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006661 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006662 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006663
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006664 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6665 return 0;
6666
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006667 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006668 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006669 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006670 /* Output closed while we were sending data. We must abort and
6671 * wake the other side up.
6672 */
6673 msg->msg_state = HTTP_MSG_ERROR;
6674 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006675 return 1;
6676 }
6677
Willy Tarreau4fe41902010-06-07 22:27:41 +02006678 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006679 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006680
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006681 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006682 /* we have msg->sov which points to the first byte of message
6683 * body, and res->buf.p still points to the beginning of the
6684 * message. We forward the headers now, as we don't need them
6685 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006686 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006687 b_adv(res->buf, msg->sov);
6688 msg->next -= msg->sov;
6689 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006690
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006691 /* The previous analysers guarantee that the state is somewhere
6692 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6693 * msg->next are always correct.
6694 */
6695 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6696 if (msg->flags & HTTP_MSGF_TE_CHNK)
6697 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6698 else
6699 msg->msg_state = HTTP_MSG_DATA;
6700 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006701 }
6702
Willy Tarreauefdf0942014-04-24 20:08:57 +02006703 if (res->to_forward) {
6704 /* We can't process the buffer's contents yet */
6705 res->flags |= CF_WAKE_WRITE;
6706 goto missing_data;
6707 }
6708
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006709 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6710 /* We need a compression buffer in the DATA state to put the
6711 * output of compressed data, and in CRLF state to let the
6712 * TRAILERS state finish the job of removing the trailing CRLF.
6713 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006714 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006715 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006716 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006717 goto aborted_xfer; /* no memory */
6718 }
6719
6720 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006721 if (ret < 0) {
6722 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006723 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006724 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006725 compressing = 1;
6726 }
6727
Willy Tarreaud98cf932009-12-27 22:54:55 +01006728 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006729 switch (msg->msg_state - HTTP_MSG_DATA) {
6730 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006731 /* we may have some pending data starting at res->buf->p */
6732 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006733 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006734 if (ret < 0)
6735 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006736
Willy Tarreaud5a67832014-04-21 10:54:27 +02006737 if (msg->chunk_len) {
6738 /* input empty or output full */
6739 if (res->buf->i > msg->next)
6740 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006741 goto missing_data;
6742 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006743 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006744 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006745 if (msg->chunk_len > res->buf->i - msg->next) {
6746 /* output full */
6747 res->flags |= CF_WAKE_WRITE;
6748 goto missing_data;
6749 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006750 msg->next += msg->chunk_len;
6751 msg->chunk_len = 0;
6752 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006753
6754 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006755 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006756 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006757 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006758 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006759 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006760 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006761 /* fall through for HTTP_MSG_CHUNK_CRLF */
6762
6763 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6764 /* we want the CRLF after the data */
6765
6766 ret = http_skip_chunk_crlf(msg);
6767 if (ret == 0)
6768 goto missing_data;
6769 else if (ret < 0) {
6770 if (msg->err_pos >= 0)
6771 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6772 goto return_bad_res;
6773 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006774 /* we're in MSG_CHUNK_SIZE now, fall through */
6775
6776 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006777 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006778 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006779 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006780 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006781
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006782 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006783 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006784 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006785 else if (ret < 0) {
6786 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006787 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006788 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006789 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006790 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006791 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006792
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006793 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006794 if (unlikely(compressing)) {
6795 /* we need to flush output contents before syncing FSMs */
6796 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6797 compressing = 0;
6798 }
6799
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006800 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006801 if (ret == 0)
6802 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006803 else if (ret < 0) {
6804 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006805 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006806 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006807 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006808 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006809
6810 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006811 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006812 if (unlikely(compressing)) {
6813 /* we need to flush output contents before syncing FSMs */
6814 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6815 compressing = 0;
6816 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006817
Willy Tarreauc623c172014-04-18 09:53:50 +02006818 /* we may have some pending data starting at res->buf->p
6819 * such as a last chunk of data or trailers.
6820 */
6821 b_adv(res->buf, msg->next);
6822 msg->next = 0;
6823
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006824 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006825 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006826 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6827 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006828 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006829
Willy Tarreau610ecce2010-01-04 21:15:02 +01006830 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006831 /* some state changes occurred, maybe the analyser
6832 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006833 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006834 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006835 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006836 /* response errors are most likely due to
6837 * the client aborting the transfer.
6838 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006839 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006840 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006841 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006842 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006843 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006844 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006845 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006846 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006848 }
6849 }
6850
Willy Tarreaud98cf932009-12-27 22:54:55 +01006851 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006852 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006853 if (unlikely(compressing)) {
6854 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006855 compressing = 0;
6856 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006857
Willy Tarreauc623c172014-04-18 09:53:50 +02006858 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6859 b_adv(res->buf, msg->next);
6860 msg->next = 0;
6861 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6862 }
6863
Willy Tarreauf003d372012-11-26 13:35:37 +01006864 if (res->flags & CF_SHUTW)
6865 goto aborted_xfer;
6866
6867 /* stop waiting for data if the input is closed before the end. If the
6868 * client side was already closed, it means that the client has aborted,
6869 * so we don't want to count this as a server abort. Otherwise it's a
6870 * server abort.
6871 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006872 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006873 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006874 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006875 if (!(s->flags & SN_ERR_MASK))
6876 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006877 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006878 if (objt_server(s->target))
6879 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006880 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006881 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006882
Willy Tarreau40dba092010-03-04 18:14:51 +01006883 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006884 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006885 goto return_bad_res;
6886
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006887 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006888 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006889 * Similarly, with keep-alive on the client side, we don't want to forward a
6890 * close.
6891 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006892 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006893 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6894 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006895 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006896
Willy Tarreau5c620922011-05-11 19:56:11 +02006897 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006898 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006899 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006900 * modes are already handled by the stream sock layer. We must not do
6901 * this in content-length mode because it could present the MSG_MORE
6902 * flag with the last block of forwarded data, which would cause an
6903 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006904 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006905 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006906 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006907
Willy Tarreaud98cf932009-12-27 22:54:55 +01006908 /* the session handler will take care of timeouts and errors */
6909 return 0;
6910
Willy Tarreau40dba092010-03-04 18:14:51 +01006911 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006912 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006913 if (objt_server(s->target))
6914 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006915
6916 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006917 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006918 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006919 compressing = 0;
6920 }
6921
Willy Tarreauc623c172014-04-18 09:53:50 +02006922 /* we may have some pending data starting at res->buf->p */
6923 if (s->comp_algo == NULL) {
6924 b_adv(res->buf, msg->next);
6925 msg->next = 0;
6926 }
6927
Willy Tarreaud98cf932009-12-27 22:54:55 +01006928 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006929 /* don't send any error message as we're in the body */
6930 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006931 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006932 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006933 if (objt_server(s->target))
6934 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006935
6936 if (!(s->flags & SN_ERR_MASK))
6937 s->flags |= SN_ERR_PRXCOND;
6938 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006939 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006940 return 0;
6941
6942 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006943 if (unlikely(compressing)) {
6944 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6945 compressing = 0;
6946 }
6947
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006948 txn->rsp.msg_state = HTTP_MSG_ERROR;
6949 /* don't send any error message as we're in the body */
6950 stream_int_retnclose(res->cons, NULL);
6951 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006952 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006953
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006954 s->fe->fe_counters.cli_aborts++;
6955 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006956 if (objt_server(s->target))
6957 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006958
6959 if (!(s->flags & SN_ERR_MASK))
6960 s->flags |= SN_ERR_CLICL;
6961 if (!(s->flags & SN_FINST_MASK))
6962 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006963 return 0;
6964}
6965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966/* Iterate the same filter through all request headers.
6967 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968 * Since it can manage the switch to another backend, it updates the per-proxy
6969 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006970 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006971int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006973 char *cur_ptr, *cur_end, *cur_next;
6974 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006975 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006977 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006978
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979 last_hdr = 0;
6980
Willy Tarreau9b28e032012-10-12 23:49:43 +02006981 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982 old_idx = 0;
6983
6984 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006985 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006986 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006987 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 (exp->action == ACT_ALLOW ||
6989 exp->action == ACT_DENY ||
6990 exp->action == ACT_TARPIT))
6991 return 0;
6992
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006993 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 if (!cur_idx)
6995 break;
6996
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006997 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998 cur_ptr = cur_next;
6999 cur_end = cur_ptr + cur_hdr->len;
7000 cur_next = cur_end + cur_hdr->cr + 1;
7001
7002 /* Now we have one header between cur_ptr and cur_end,
7003 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004 */
7005
Willy Tarreau15a53a42015-01-21 13:39:42 +01007006 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007007 switch (exp->action) {
7008 case ACT_SETBE:
7009 /* It is not possible to jump a second time.
7010 * FIXME: should we return an HTTP/500 here so that
7011 * the admin knows there's a problem ?
7012 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007013 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007014 break;
7015
7016 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007017 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007018 last_hdr = 1;
7019 break;
7020
7021 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007023 last_hdr = 1;
7024 break;
7025
7026 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007027 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 break;
7030
7031 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 break;
7035
7036 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007037 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7038 if (trash.len < 0)
7039 return -1;
7040
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007041 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007042 /* FIXME: if the user adds a newline in the replacement, the
7043 * index will not be recalculated for now, and the new line
7044 * will not be counted as a new header.
7045 */
7046
7047 cur_end += delta;
7048 cur_next += delta;
7049 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007050 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 break;
7052
7053 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007054 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 cur_next += delta;
7056
Willy Tarreaufa355d42009-11-29 18:12:29 +01007057 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007058 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7059 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 cur_hdr->len = 0;
7061 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007062 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007063 break;
7064
7065 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066 }
7067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 /* keep the link from this header to next one in case of later
7069 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 old_idx = cur_idx;
7072 }
7073 return 0;
7074}
7075
7076
7077/* Apply the filter to the request line.
7078 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7079 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080 * Since it can manage the switch to another backend, it updates the per-proxy
7081 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007083int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007085 char *cur_ptr, *cur_end;
7086 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007087 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007088 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089
Willy Tarreau3d300592007-03-18 18:34:41 +01007090 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007091 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007092 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 (exp->action == ACT_ALLOW ||
7094 exp->action == ACT_DENY ||
7095 exp->action == ACT_TARPIT))
7096 return 0;
7097 else if (exp->action == ACT_REMOVE)
7098 return 0;
7099
7100 done = 0;
7101
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007103 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104
7105 /* Now we have the request line between cur_ptr and cur_end */
7106
Willy Tarreau15a53a42015-01-21 13:39:42 +01007107 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007108 switch (exp->action) {
7109 case ACT_SETBE:
7110 /* It is not possible to jump a second time.
7111 * FIXME: should we return an HTTP/500 here so that
7112 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007114 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007115 break;
7116
7117 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007118 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007119 done = 1;
7120 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007123 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 done = 1;
7125 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007128 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 done = 1;
7130 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007133 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 done = 1;
7135 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007138 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7139 if (trash.len < 0)
7140 return -1;
7141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007142 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 /* FIXME: if the user adds a newline in the replacement, the
7144 * index will not be recalculated for now, and the new line
7145 * will not be counted as a new header.
7146 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007147
Willy Tarreaufa355d42009-11-29 18:12:29 +01007148 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007150 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 HTTP_MSG_RQMETH,
7152 cur_ptr, cur_end + 1,
7153 NULL, NULL);
7154 if (unlikely(!cur_end))
7155 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007157 /* we have a full request and we know that we have either a CR
7158 * or an LF at <ptr>.
7159 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007160 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7161 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007162 /* there is no point trying this regex on headers */
7163 return 1;
7164 }
7165 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007166 return done;
7167}
Willy Tarreau97de6242006-12-27 17:18:38 +01007168
Willy Tarreau58f10d72006-12-04 02:26:12 +01007169
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007172 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 * unparsable request. Since it can manage the switch to another backend, it
7175 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007177int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007179 struct http_txn *txn = &s->txn;
7180 struct hdr_exp *exp;
7181
7182 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007183 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 /*
7186 * The interleaving of transformations and verdicts
7187 * makes it difficult to decide to continue or stop
7188 * the evaluation.
7189 */
7190
Willy Tarreau6c123b12010-01-28 20:22:06 +01007191 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7192 break;
7193
Willy Tarreau3d300592007-03-18 18:34:41 +01007194 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007196 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007197 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007198
7199 /* if this filter had a condition, evaluate it now and skip to
7200 * next filter if the condition does not match.
7201 */
7202 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007203 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007204 ret = acl_pass(ret);
7205 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7206 ret = !ret;
7207
7208 if (!ret)
7209 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007210 }
7211
7212 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007213 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214 if (unlikely(ret < 0))
7215 return -1;
7216
7217 if (likely(ret == 0)) {
7218 /* The filter did not match the request, it can be
7219 * iterated through all headers.
7220 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007221 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7222 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007224 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007225 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226}
7227
7228
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007231 * Try to retrieve the server associated to the appsession.
7232 * If the server is found, it's assigned to the session.
7233 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007234void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7235 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007236 appsess *asession = NULL;
7237 char *sessid_temp = NULL;
7238
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007239 if (len > s->be->appsession_len) {
7240 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007241 }
7242
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007243 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007244 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007245 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007246 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007247 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007248 }
7249
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007250 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007251 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007252 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007253 return;
7254 }
7255
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007256 memcpy(txn->sessid, buf, len);
7257 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007258 }
7259
7260 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7261 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007262 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007263 return;
7264 }
7265
Cyril Bontéb21570a2009-11-29 20:04:48 +01007266 memcpy(sessid_temp, buf, len);
7267 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007268
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007269 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007270 /* free previously allocated memory */
7271 pool_free2(apools.sessid, sessid_temp);
7272
7273 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007274 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7275 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007276 asession->request_count++;
7277
7278 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007279 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007280
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007281 while (srv) {
7282 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007283 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007284 (s->be->options & PR_O_PERSIST) ||
7285 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007286 /* we found the server and it's usable */
7287 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007288 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007289 s->flags |= SN_DIRECT | SN_ASSIGNED;
7290 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007291
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007292 break;
7293 } else {
7294 txn->flags &= ~TX_CK_MASK;
7295 txn->flags |= TX_CK_DOWN;
7296 }
7297 }
7298 srv = srv->next;
7299 }
7300 }
7301 }
7302}
7303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007304/* Find the end of a cookie value contained between <s> and <e>. It works the
7305 * same way as with headers above except that the semi-colon also ends a token.
7306 * See RFC2965 for more information. Note that it requires a valid header to
7307 * return a valid result.
7308 */
7309char *find_cookie_value_end(char *s, const char *e)
7310{
7311 int quoted, qdpair;
7312
7313 quoted = qdpair = 0;
7314 for (; s < e; s++) {
7315 if (qdpair) qdpair = 0;
7316 else if (quoted) {
7317 if (*s == '\\') qdpair = 1;
7318 else if (*s == '"') quoted = 0;
7319 }
7320 else if (*s == '"') quoted = 1;
7321 else if (*s == ',' || *s == ';') return s;
7322 }
7323 return s;
7324}
7325
7326/* Delete a value in a header between delimiters <from> and <next> in buffer
7327 * <buf>. The number of characters displaced is returned, and the pointer to
7328 * the first delimiter is updated if required. The function tries as much as
7329 * possible to respect the following principles :
7330 * - replace <from> delimiter by the <next> one unless <from> points to a
7331 * colon, in which case <next> is simply removed
7332 * - set exactly one space character after the new first delimiter, unless
7333 * there are not enough characters in the block being moved to do so.
7334 * - remove unneeded spaces before the previous delimiter and after the new
7335 * one.
7336 *
7337 * It is the caller's responsibility to ensure that :
7338 * - <from> points to a valid delimiter or the colon ;
7339 * - <next> points to a valid delimiter or the final CR/LF ;
7340 * - there are non-space chars before <from> ;
7341 * - there is a CR/LF at or after <next>.
7342 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007343int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007344{
7345 char *prev = *from;
7346
7347 if (*prev == ':') {
7348 /* We're removing the first value, preserve the colon and add a
7349 * space if possible.
7350 */
7351 if (!http_is_crlf[(unsigned char)*next])
7352 next++;
7353 prev++;
7354 if (prev < next)
7355 *prev++ = ' ';
7356
7357 while (http_is_spht[(unsigned char)*next])
7358 next++;
7359 } else {
7360 /* Remove useless spaces before the old delimiter. */
7361 while (http_is_spht[(unsigned char)*(prev-1)])
7362 prev--;
7363 *from = prev;
7364
7365 /* copy the delimiter and if possible a space if we're
7366 * not at the end of the line.
7367 */
7368 if (!http_is_crlf[(unsigned char)*next]) {
7369 *prev++ = *next++;
7370 if (prev + 1 < next)
7371 *prev++ = ' ';
7372 while (http_is_spht[(unsigned char)*next])
7373 next++;
7374 }
7375 }
7376 return buffer_replace2(buf, prev, next, NULL, 0);
7377}
7378
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007379/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007380 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007381 * desirable to call it only when needed. This code is quite complex because
7382 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7383 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007385void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007386{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007387 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007388 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007390 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7391 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007394 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007395 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007397 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007398 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007399 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007400
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007401 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402 hdr_beg = hdr_next;
7403 hdr_end = hdr_beg + cur_hdr->len;
7404 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 /* We have one full header between hdr_beg and hdr_end, and the
7407 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408 * "Cookie:" headers.
7409 */
7410
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007411 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007412 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007413 old_idx = cur_idx;
7414 continue;
7415 }
7416
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007417 del_from = NULL; /* nothing to be deleted */
7418 preserve_hdr = 0; /* assume we may kill the whole header */
7419
Willy Tarreau58f10d72006-12-04 02:26:12 +01007420 /* Now look for cookies. Conforming to RFC2109, we have to support
7421 * attributes whose name begin with a '$', and associate them with
7422 * the right cookie, if we want to delete this cookie.
7423 * So there are 3 cases for each cookie read :
7424 * 1) it's a special attribute, beginning with a '$' : ignore it.
7425 * 2) it's a server id cookie that we *MAY* want to delete : save
7426 * some pointers on it (last semi-colon, beginning of cookie...)
7427 * 3) it's an application cookie : we *MAY* have to delete a previous
7428 * "special" cookie.
7429 * At the end of loop, if a "special" cookie remains, we may have to
7430 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007431 * *MUST* delete it.
7432 *
7433 * Note: RFC2965 is unclear about the processing of spaces around
7434 * the equal sign in the ATTR=VALUE form. A careful inspection of
7435 * the RFC explicitly allows spaces before it, and not within the
7436 * tokens (attrs or values). An inspection of RFC2109 allows that
7437 * too but section 10.1.3 lets one think that spaces may be allowed
7438 * after the equal sign too, resulting in some (rare) buggy
7439 * implementations trying to do that. So let's do what servers do.
7440 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7441 * allowed quoted strings in values, with any possible character
7442 * after a backslash, including control chars and delimitors, which
7443 * causes parsing to become ambiguous. Browsers also allow spaces
7444 * within values even without quotes.
7445 *
7446 * We have to keep multiple pointers in order to support cookie
7447 * removal at the beginning, middle or end of header without
7448 * corrupting the header. All of these headers are valid :
7449 *
7450 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7451 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7452 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7453 * | | | | | | | | |
7454 * | | | | | | | | hdr_end <--+
7455 * | | | | | | | +--> next
7456 * | | | | | | +----> val_end
7457 * | | | | | +-----------> val_beg
7458 * | | | | +--------------> equal
7459 * | | | +----------------> att_end
7460 * | | +---------------------> att_beg
7461 * | +--------------------------> prev
7462 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 */
7464
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007465 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7466 /* Iterate through all cookies on this line */
7467
7468 /* find att_beg */
7469 att_beg = prev + 1;
7470 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7471 att_beg++;
7472
7473 /* find att_end : this is the first character after the last non
7474 * space before the equal. It may be equal to hdr_end.
7475 */
7476 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478 while (equal < hdr_end) {
7479 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 if (http_is_spht[(unsigned char)*equal++])
7482 continue;
7483 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484 }
7485
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007486 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7487 * is between <att_beg> and <equal>, both may be identical.
7488 */
7489
7490 /* look for end of cookie if there is an equal sign */
7491 if (equal < hdr_end && *equal == '=') {
7492 /* look for the beginning of the value */
7493 val_beg = equal + 1;
7494 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7495 val_beg++;
7496
7497 /* find the end of the value, respecting quotes */
7498 next = find_cookie_value_end(val_beg, hdr_end);
7499
7500 /* make val_end point to the first white space or delimitor after the value */
7501 val_end = next;
7502 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7503 val_end--;
7504 } else {
7505 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508 /* We have nothing to do with attributes beginning with '$'. However,
7509 * they will automatically be removed if a header before them is removed,
7510 * since they're supposed to be linked together.
7511 */
7512 if (*att_beg == '$')
7513 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007514
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 /* Ignore cookies with no equal sign */
7516 if (equal == next) {
7517 /* This is not our cookie, so we must preserve it. But if we already
7518 * scheduled another cookie for removal, we cannot remove the
7519 * complete header, but we can remove the previous block itself.
7520 */
7521 preserve_hdr = 1;
7522 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007523 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 val_end += delta;
7525 next += delta;
7526 hdr_end += delta;
7527 hdr_next += delta;
7528 cur_hdr->len += delta;
7529 http_msg_move_end(&txn->req, delta);
7530 prev = del_from;
7531 del_from = NULL;
7532 }
7533 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007535
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007536 /* if there are spaces around the equal sign, we need to
7537 * strip them otherwise we'll get trouble for cookie captures,
7538 * or even for rewrites. Since this happens extremely rarely,
7539 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007540 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7542 int stripped_before = 0;
7543 int stripped_after = 0;
7544
7545 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007546 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 equal += stripped_before;
7548 val_beg += stripped_before;
7549 }
7550
7551 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007552 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 val_beg += stripped_after;
7554 stripped_before += stripped_after;
7555 }
7556
7557 val_end += stripped_before;
7558 next += stripped_before;
7559 hdr_end += stripped_before;
7560 hdr_next += stripped_before;
7561 cur_hdr->len += stripped_before;
7562 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 /* First, let's see if we want to capture this cookie. We check
7567 * that we don't already have a client side cookie, because we
7568 * can only capture one. Also as an optimisation, we ignore
7569 * cookies shorter than the declared name.
7570 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007571 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7572 (val_end - att_beg >= s->fe->capture_namelen) &&
7573 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 int log_len = val_end - att_beg;
7575
7576 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7577 Alert("HTTP logging : out of memory.\n");
7578 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007579 if (log_len > s->fe->capture_len)
7580 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 memcpy(txn->cli_cookie, att_beg, log_len);
7582 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585
Willy Tarreaubca99692010-10-06 19:25:55 +02007586 /* Persistence cookies in passive, rewrite or insert mode have the
7587 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007589 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007590 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007591 * For cookies in prefix mode, the form is :
7592 *
7593 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007595 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7596 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7597 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7601 * have the server ID between val_beg and delim, and the original cookie between
7602 * delim+1 and val_end. Otherwise, delim==val_end :
7603 *
7604 * Cookie: NAME=SRV; # in all but prefix modes
7605 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7606 * | || || | |+-> next
7607 * | || || | +--> val_end
7608 * | || || +---------> delim
7609 * | || |+------------> val_beg
7610 * | || +-------------> att_end = equal
7611 * | |+-----------------> att_beg
7612 * | +------------------> prev
7613 * +-------------------------> hdr_beg
7614 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007615
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007616 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007617 for (delim = val_beg; delim < val_end; delim++)
7618 if (*delim == COOKIE_DELIM)
7619 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007620 } else {
7621 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007623 /* Now check if the cookie contains a date field, which would
7624 * appear after a vertical bar ('|') just after the server name
7625 * and before the delimiter.
7626 */
7627 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7628 if (vbar1) {
7629 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007630 * right is the last seen date. It is a base64 encoded
7631 * 30-bit value representing the UNIX date since the
7632 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007633 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007634 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007635 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007636 if (val_end - vbar1 >= 5) {
7637 val = b64tos30(vbar1);
7638 if (val > 0)
7639 txn->cookie_last_date = val << 2;
7640 }
7641 /* look for a second vertical bar */
7642 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7643 if (vbar1 && (val_end - vbar1 > 5)) {
7644 val = b64tos30(vbar1 + 1);
7645 if (val > 0)
7646 txn->cookie_first_date = val << 2;
7647 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007648 }
7649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650
Willy Tarreauf64d1412010-10-07 20:06:11 +02007651 /* if the cookie has an expiration date and the proxy wants to check
7652 * it, then we do that now. We first check if the cookie is too old,
7653 * then only if it has expired. We detect strict overflow because the
7654 * time resolution here is not great (4 seconds). Cookies with dates
7655 * in the future are ignored if their offset is beyond one day. This
7656 * allows an admin to fix timezone issues without expiring everyone
7657 * and at the same time avoids keeping unwanted side effects for too
7658 * long.
7659 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007660 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7661 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007662 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007663 txn->flags &= ~TX_CK_MASK;
7664 txn->flags |= TX_CK_OLD;
7665 delim = val_beg; // let's pretend we have not found the cookie
7666 txn->cookie_first_date = 0;
7667 txn->cookie_last_date = 0;
7668 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007669 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7670 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007671 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007672 txn->flags &= ~TX_CK_MASK;
7673 txn->flags |= TX_CK_EXPIRED;
7674 delim = val_beg; // let's pretend we have not found the cookie
7675 txn->cookie_first_date = 0;
7676 txn->cookie_last_date = 0;
7677 }
7678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 /* Here, we'll look for the first running server which supports the cookie.
7680 * This allows to share a same cookie between several servers, for example
7681 * to dedicate backup servers to specific servers only.
7682 * However, to prevent clients from sticking to cookie-less backup server
7683 * when they have incidentely learned an empty cookie, we simply ignore
7684 * empty cookies and mark them as invalid.
7685 * The same behaviour is applied when persistence must be ignored.
7686 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007687 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007688 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 while (srv) {
7691 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7692 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007693 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007694 (s->be->options & PR_O_PERSIST) ||
7695 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 /* we found the server and we can use it */
7697 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007698 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007699 s->flags |= SN_DIRECT | SN_ASSIGNED;
7700 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 break;
7702 } else {
7703 /* we found a server, but it's down,
7704 * mark it as such and go on in case
7705 * another one is available.
7706 */
7707 txn->flags &= ~TX_CK_MASK;
7708 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 srv = srv->next;
7712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713
Willy Tarreauf64d1412010-10-07 20:06:11 +02007714 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007715 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007717 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007718 txn->flags |= TX_CK_UNUSED;
7719 else
7720 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 /* depending on the cookie mode, we may have to either :
7724 * - delete the complete cookie if we're in insert+indirect mode, so that
7725 * the server never sees it ;
7726 * - remove the server id from the cookie value, and tag the cookie as an
7727 * application cookie so that it does not get accidentely removed later,
7728 * if we're in cookie prefix mode
7729 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007730 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007732
Willy Tarreau9b28e032012-10-12 23:49:43 +02007733 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 val_end += delta;
7735 next += delta;
7736 hdr_end += delta;
7737 hdr_next += delta;
7738 cur_hdr->len += delta;
7739 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007740
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007741 del_from = NULL;
7742 preserve_hdr = 1; /* we want to keep this cookie */
7743 }
7744 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007745 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 del_from = prev;
7747 }
7748 } else {
7749 /* This is not our cookie, so we must preserve it. But if we already
7750 * scheduled another cookie for removal, we cannot remove the
7751 * complete header, but we can remove the previous block itself.
7752 */
7753 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007756 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007757 if (att_beg >= del_from)
7758 att_beg += delta;
7759 if (att_end >= del_from)
7760 att_end += delta;
7761 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 val_end += delta;
7763 next += delta;
7764 hdr_end += delta;
7765 hdr_next += delta;
7766 cur_hdr->len += delta;
7767 http_msg_move_end(&txn->req, delta);
7768 prev = del_from;
7769 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007770 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007774 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 int cmp_len, value_len;
7776 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007777
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007778 if (s->be->options2 & PR_O2_AS_PFX) {
7779 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7780 value_begin = att_beg + s->be->appsession_name_len;
7781 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 } else {
7783 cmp_len = att_end - att_beg;
7784 value_begin = val_beg;
7785 value_len = val_end - val_beg;
7786 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007787
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007789 if (cmp_len == s->be->appsession_name_len &&
7790 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7791 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007793 }
7794
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 /* continue with next cookie on this header line */
7796 att_beg = next;
7797 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* There are no more cookies on this line.
7800 * We may still have one (or several) marked for deletion at the
7801 * end of the line. We must do this now in two ways :
7802 * - if some cookies must be preserved, we only delete from the
7803 * mark to the end of line ;
7804 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007805 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007806 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007807 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007809 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811 cur_hdr->len += delta;
7812 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007814
7815 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007816 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7817 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007818 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007819 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007820 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007821 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007822 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007823 }
7824
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007825 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007826 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007827 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007828}
7829
7830
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831/* Iterate the same filter through all response headers contained in <rtr>.
7832 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7833 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007834int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836 char *cur_ptr, *cur_end, *cur_next;
7837 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007838 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007840 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
7842 last_hdr = 0;
7843
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 old_idx = 0;
7846
7847 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007848 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007850 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 (exp->action == ACT_ALLOW ||
7852 exp->action == ACT_DENY))
7853 return 0;
7854
7855 cur_idx = txn->hdr_idx.v[old_idx].next;
7856 if (!cur_idx)
7857 break;
7858
7859 cur_hdr = &txn->hdr_idx.v[cur_idx];
7860 cur_ptr = cur_next;
7861 cur_end = cur_ptr + cur_hdr->len;
7862 cur_next = cur_end + cur_hdr->cr + 1;
7863
7864 /* Now we have one header between cur_ptr and cur_end,
7865 * and the next header starts at cur_next.
7866 */
7867
Willy Tarreau15a53a42015-01-21 13:39:42 +01007868 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869 switch (exp->action) {
7870 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007871 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 last_hdr = 1;
7873 break;
7874
7875 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007876 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 last_hdr = 1;
7878 break;
7879
7880 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007881 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7882 if (trash.len < 0)
7883 return -1;
7884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007885 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886 /* FIXME: if the user adds a newline in the replacement, the
7887 * index will not be recalculated for now, and the new line
7888 * will not be counted as a new header.
7889 */
7890
7891 cur_end += delta;
7892 cur_next += delta;
7893 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007894 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895 break;
7896
7897 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 cur_next += delta;
7900
Willy Tarreaufa355d42009-11-29 18:12:29 +01007901 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7903 txn->hdr_idx.used--;
7904 cur_hdr->len = 0;
7905 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007906 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 break;
7908
7909 }
7910 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911
7912 /* keep the link from this header to next one in case of later
7913 * removal of next header.
7914 */
7915 old_idx = cur_idx;
7916 }
7917 return 0;
7918}
7919
7920
7921/* Apply the filter to the status line in the response buffer <rtr>.
7922 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7923 * or -1 if a replacement resulted in an invalid status line.
7924 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007925int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 char *cur_ptr, *cur_end;
7928 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007929 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007930 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931
7932
Willy Tarreau3d300592007-03-18 18:34:41 +01007933 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007935 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 (exp->action == ACT_ALLOW ||
7937 exp->action == ACT_DENY))
7938 return 0;
7939 else if (exp->action == ACT_REMOVE)
7940 return 0;
7941
7942 done = 0;
7943
Willy Tarreau9b28e032012-10-12 23:49:43 +02007944 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007945 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946
7947 /* Now we have the status line between cur_ptr and cur_end */
7948
Willy Tarreau15a53a42015-01-21 13:39:42 +01007949 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 switch (exp->action) {
7951 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007952 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 done = 1;
7954 break;
7955
7956 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007957 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 done = 1;
7959 break;
7960
7961 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007962 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7963 if (trash.len < 0)
7964 return -1;
7965
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007966 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 /* FIXME: if the user adds a newline in the replacement, the
7968 * index will not be recalculated for now, and the new line
7969 * will not be counted as a new header.
7970 */
7971
Willy Tarreaufa355d42009-11-29 18:12:29 +01007972 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007974 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007975 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 cur_ptr, cur_end + 1,
7977 NULL, NULL);
7978 if (unlikely(!cur_end))
7979 return -1;
7980
7981 /* we have a full respnse and we know that we have either a CR
7982 * or an LF at <ptr>.
7983 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007984 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007985 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 /* there is no point trying this regex on headers */
7987 return 1;
7988 }
7989 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990 return done;
7991}
7992
7993
7994
7995/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007996 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7998 * unparsable response.
7999 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02008000int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008002 struct http_txn *txn = &s->txn;
8003 struct hdr_exp *exp;
8004
8005 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006 int ret;
8007
8008 /*
8009 * The interleaving of transformations and verdicts
8010 * makes it difficult to decide to continue or stop
8011 * the evaluation.
8012 */
8013
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008014 if (txn->flags & TX_SVDENY)
8015 break;
8016
Willy Tarreau3d300592007-03-18 18:34:41 +01008017 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8019 exp->action == ACT_PASS)) {
8020 exp = exp->next;
8021 continue;
8022 }
8023
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008024 /* if this filter had a condition, evaluate it now and skip to
8025 * next filter if the condition does not match.
8026 */
8027 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008028 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008029 ret = acl_pass(ret);
8030 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8031 ret = !ret;
8032 if (!ret)
8033 continue;
8034 }
8035
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008037 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 if (unlikely(ret < 0))
8039 return -1;
8040
8041 if (likely(ret == 0)) {
8042 /* The filter did not match the response, it can be
8043 * iterated through all headers.
8044 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008045 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8046 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008047 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 }
8049 return 0;
8050}
8051
8052
Willy Tarreaua15645d2007-03-18 16:22:39 +01008053/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008054 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008055 * desirable to call it only when needed. This function is also used when we
8056 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008058void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008060 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01008061 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008062 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 char *hdr_beg, *hdr_end, *hdr_next;
8065 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 /* Iterate through the headers.
8068 * we start with the start line.
8069 */
8070 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008071 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072
8073 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8074 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008075 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076
8077 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008078 hdr_beg = hdr_next;
8079 hdr_end = hdr_beg + cur_hdr->len;
8080 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081
Willy Tarreau24581ba2010-08-31 22:39:35 +02008082 /* We have one full header between hdr_beg and hdr_end, and the
8083 * next header starts at hdr_next. We're only interested in
8084 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 */
8086
Willy Tarreau24581ba2010-08-31 22:39:35 +02008087 is_cookie2 = 0;
8088 prev = hdr_beg + 10;
8089 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008090 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008091 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8092 if (!val) {
8093 old_idx = cur_idx;
8094 continue;
8095 }
8096 is_cookie2 = 1;
8097 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098 }
8099
Willy Tarreau24581ba2010-08-31 22:39:35 +02008100 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8101 * <prev> points to the colon.
8102 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008103 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104
Willy Tarreau24581ba2010-08-31 22:39:35 +02008105 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8106 * check-cache is enabled) and we are not interested in checking
8107 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008108 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008109 if (s->be->cookie_name == NULL &&
8110 s->be->appsession_name == NULL &&
8111 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112 return;
8113
Willy Tarreau24581ba2010-08-31 22:39:35 +02008114 /* OK so now we know we have to process this response cookie.
8115 * The format of the Set-Cookie header is slightly different
8116 * from the format of the Cookie header in that it does not
8117 * support the comma as a cookie delimiter (thus the header
8118 * cannot be folded) because the Expires attribute described in
8119 * the original Netscape's spec may contain an unquoted date
8120 * with a comma inside. We have to live with this because
8121 * many browsers don't support Max-Age and some browsers don't
8122 * support quoted strings. However the Set-Cookie2 header is
8123 * clean.
8124 *
8125 * We have to keep multiple pointers in order to support cookie
8126 * removal at the beginning, middle or end of header without
8127 * corrupting the header (in case of set-cookie2). A special
8128 * pointer, <scav> points to the beginning of the set-cookie-av
8129 * fields after the first semi-colon. The <next> pointer points
8130 * either to the end of line (set-cookie) or next unquoted comma
8131 * (set-cookie2). All of these headers are valid :
8132 *
8133 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8134 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8135 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8136 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8137 * | | | | | | | | | |
8138 * | | | | | | | | +-> next hdr_end <--+
8139 * | | | | | | | +------------> scav
8140 * | | | | | | +--------------> val_end
8141 * | | | | | +--------------------> val_beg
8142 * | | | | +----------------------> equal
8143 * | | | +------------------------> att_end
8144 * | | +----------------------------> att_beg
8145 * | +------------------------------> prev
8146 * +-----------------------------------------> hdr_beg
8147 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148
Willy Tarreau24581ba2010-08-31 22:39:35 +02008149 for (; prev < hdr_end; prev = next) {
8150 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 /* find att_beg */
8153 att_beg = prev + 1;
8154 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8155 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156
Willy Tarreau24581ba2010-08-31 22:39:35 +02008157 /* find att_end : this is the first character after the last non
8158 * space before the equal. It may be equal to hdr_end.
8159 */
8160 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161
Willy Tarreau24581ba2010-08-31 22:39:35 +02008162 while (equal < hdr_end) {
8163 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8164 break;
8165 if (http_is_spht[(unsigned char)*equal++])
8166 continue;
8167 att_end = equal;
8168 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169
Willy Tarreau24581ba2010-08-31 22:39:35 +02008170 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8171 * is between <att_beg> and <equal>, both may be identical.
8172 */
8173
8174 /* look for end of cookie if there is an equal sign */
8175 if (equal < hdr_end && *equal == '=') {
8176 /* look for the beginning of the value */
8177 val_beg = equal + 1;
8178 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8179 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180
Willy Tarreau24581ba2010-08-31 22:39:35 +02008181 /* find the end of the value, respecting quotes */
8182 next = find_cookie_value_end(val_beg, hdr_end);
8183
8184 /* make val_end point to the first white space or delimitor after the value */
8185 val_end = next;
8186 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8187 val_end--;
8188 } else {
8189 /* <equal> points to next comma, semi-colon or EOL */
8190 val_beg = val_end = next = equal;
8191 }
8192
8193 if (next < hdr_end) {
8194 /* Set-Cookie2 supports multiple cookies, and <next> points to
8195 * a colon or semi-colon before the end. So skip all attr-value
8196 * pairs and look for the next comma. For Set-Cookie, since
8197 * commas are permitted in values, skip to the end.
8198 */
8199 if (is_cookie2)
8200 next = find_hdr_value_end(next, hdr_end);
8201 else
8202 next = hdr_end;
8203 }
8204
8205 /* Now everything is as on the diagram above */
8206
8207 /* Ignore cookies with no equal sign */
8208 if (equal == val_end)
8209 continue;
8210
8211 /* If there are spaces around the equal sign, we need to
8212 * strip them otherwise we'll get trouble for cookie captures,
8213 * or even for rewrites. Since this happens extremely rarely,
8214 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8217 int stripped_before = 0;
8218 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219
Willy Tarreau24581ba2010-08-31 22:39:35 +02008220 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008221 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 equal += stripped_before;
8223 val_beg += stripped_before;
8224 }
8225
8226 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008227 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 val_beg += stripped_after;
8229 stripped_before += stripped_after;
8230 }
8231
8232 val_end += stripped_before;
8233 next += stripped_before;
8234 hdr_end += stripped_before;
8235 hdr_next += stripped_before;
8236 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008237 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 }
8239
8240 /* First, let's see if we want to capture this cookie. We check
8241 * that we don't already have a server side cookie, because we
8242 * can only capture one. Also as an optimisation, we ignore
8243 * cookies shorter than the declared name.
8244 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008245 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008246 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008247 (val_end - att_beg >= s->fe->capture_namelen) &&
8248 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008250 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251 Alert("HTTP logging : out of memory.\n");
8252 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008253 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008254 if (log_len > s->fe->capture_len)
8255 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008256 memcpy(txn->srv_cookie, att_beg, log_len);
8257 txn->srv_cookie[log_len] = 0;
8258 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 }
8260
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008261 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008263 if (!(s->flags & SN_IGNORE_PRST) &&
8264 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8265 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008266 /* assume passive cookie by default */
8267 txn->flags &= ~TX_SCK_MASK;
8268 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269
8270 /* If the cookie is in insert mode on a known server, we'll delete
8271 * this occurrence because we'll insert another one later.
8272 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 * a direct access.
8274 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008275 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008276 /* The "preserve" flag was set, we don't want to touch the
8277 * server's cookie.
8278 */
8279 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008280 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8281 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008282 /* this cookie must be deleted */
8283 if (*prev == ':' && next == hdr_end) {
8284 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008285 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8287 txn->hdr_idx.used--;
8288 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008289 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008290 hdr_next += delta;
8291 http_msg_move_end(&txn->rsp, delta);
8292 /* note: while both invalid now, <next> and <hdr_end>
8293 * are still equal, so the for() will stop as expected.
8294 */
8295 } else {
8296 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008297 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 next = prev;
8299 hdr_end += delta;
8300 hdr_next += delta;
8301 cur_hdr->len += delta;
8302 http_msg_move_end(&txn->rsp, delta);
8303 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008304 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008305 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008308 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310 * with this server since we know it.
8311 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008312 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 next += delta;
8314 hdr_end += delta;
8315 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008317 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318
Willy Tarreauf1348312010-10-07 15:54:11 +02008319 txn->flags &= ~TX_SCK_MASK;
8320 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008321 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008322 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008326 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 next += delta;
8328 hdr_end += delta;
8329 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008330 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008331 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008332
Willy Tarreau827aee92011-03-10 16:55:02 +01008333 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008334 txn->flags &= ~TX_SCK_MASK;
8335 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336 }
8337 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008338 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008339 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008340 int cmp_len, value_len;
8341 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008343 if (s->be->options2 & PR_O2_AS_PFX) {
8344 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8345 value_begin = att_beg + s->be->appsession_name_len;
8346 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008347 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008348 cmp_len = att_end - att_beg;
8349 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008350 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008351 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008352
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008353 if ((cmp_len == s->be->appsession_name_len) &&
8354 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008355 /* free a possibly previously allocated memory */
8356 pool_free2(apools.sessid, txn->sessid);
8357
Cyril Bontéb21570a2009-11-29 20:04:48 +01008358 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008359 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008360 Alert("Not enough Memory process_srv():asession->sessid: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");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008362 return;
8363 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008364 memcpy(txn->sessid, value_begin, value_len);
8365 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008367 }
8368 /* that's done for this cookie, check the next one on the same
8369 * line when next != hdr_end (only if is_cookie2).
8370 */
8371 }
8372 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008374 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008375
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008376 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008377 appsess *asession = NULL;
8378 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008379 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008380 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008381 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008382 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8383 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008384 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008385 return;
8386 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008387 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8388
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008389 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8390 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008391 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8392 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008393 return;
8394 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008395 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8396 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008397
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008398 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008399 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008400 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008401 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8402 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008403 return;
8404 }
8405 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008406 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008407
8408 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008409 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008410 }
8411
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008412 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008413 asession->request_count++;
8414 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415}
8416
8417
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008419 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008420 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008421void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 char *p1, *p2;
8425
8426 char *cur_ptr, *cur_end, *cur_next;
8427 int cur_idx;
8428
Willy Tarreau5df51872007-11-25 16:20:08 +01008429 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 return;
8431
8432 /* Iterate through the headers.
8433 * we start with the start line.
8434 */
8435 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008436 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437
8438 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8439 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008440 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441
8442 cur_hdr = &txn->hdr_idx.v[cur_idx];
8443 cur_ptr = cur_next;
8444 cur_end = cur_ptr + cur_hdr->len;
8445 cur_next = cur_end + cur_hdr->cr + 1;
8446
8447 /* We have one full header between cur_ptr and cur_end, and the
8448 * next header starts at cur_next. We're only interested in
8449 * "Cookie:" headers.
8450 */
8451
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008452 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8453 if (val) {
8454 if ((cur_end - (cur_ptr + val) >= 8) &&
8455 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8456 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8457 return;
8458 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 }
8460
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008461 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8462 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008463 continue;
8464
8465 /* OK, right now we know we have a cache-control header at cur_ptr */
8466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008467 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468
8469 if (p1 >= cur_end) /* no more info */
8470 continue;
8471
8472 /* p1 is at the beginning of the value */
8473 p2 = p1;
8474
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008475 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 p2++;
8477
8478 /* we have a complete value between p1 and p2 */
8479 if (p2 < cur_end && *p2 == '=') {
8480 /* we have something of the form no-cache="set-cookie" */
8481 if ((cur_end - p1 >= 21) &&
8482 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8483 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008484 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 continue;
8486 }
8487
8488 /* OK, so we know that either p2 points to the end of string or to a comma */
8489 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008490 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8492 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8493 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008494 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 return;
8496 }
8497
8498 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008499 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 continue;
8501 }
8502 }
8503}
8504
8505
Willy Tarreau58f10d72006-12-04 02:26:12 +01008506/*
8507 * Try to retrieve a known appsession in the URI, then the associated server.
8508 * If the server is found, it's assigned to the session.
8509 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008510void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008511{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008512 char *end_params, *first_param, *cur_param, *next_param;
8513 char separator;
8514 int value_len;
8515
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008516 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008517
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008518 if (s->be->appsession_name == NULL ||
8519 (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 +01008520 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008521 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008522
Cyril Bontéb21570a2009-11-29 20:04:48 +01008523 first_param = NULL;
8524 switch (mode) {
8525 case PR_O2_AS_M_PP:
8526 first_param = memchr(begin, ';', len);
8527 break;
8528 case PR_O2_AS_M_QS:
8529 first_param = memchr(begin, '?', len);
8530 break;
8531 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008532
Cyril Bontéb21570a2009-11-29 20:04:48 +01008533 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008534 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008536
Cyril Bontéb21570a2009-11-29 20:04:48 +01008537 switch (mode) {
8538 case PR_O2_AS_M_PP:
8539 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8540 end_params = (char *) begin + len;
8541 }
8542 separator = ';';
8543 break;
8544 case PR_O2_AS_M_QS:
8545 end_params = (char *) begin + len;
8546 separator = '&';
8547 break;
8548 default:
8549 /* unknown mode, shouldn't happen */
8550 return;
8551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008552
Cyril Bontéb21570a2009-11-29 20:04:48 +01008553 cur_param = next_param = end_params;
8554 while (cur_param > first_param) {
8555 cur_param--;
8556 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8557 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008558 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8559 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8560 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008561 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008562 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8563 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008564 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008565 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008566 }
8567 break;
8568 }
8569 next_param = cur_param;
8570 }
8571 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008572#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008573 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008574 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008575#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008576}
8577
Willy Tarreaub2513902006-12-17 14:52:38 +01008578/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008579 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008580 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008581 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008582 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008583 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008584 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008585 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008586 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008587int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008588{
8589 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008590 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008591 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008592
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008593 if (!uri_auth)
8594 return 0;
8595
Cyril Bonté70be45d2010-10-12 00:14:35 +02008596 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008597 return 0;
8598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008599 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008600 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008601 return 0;
8602
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008603 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008604 return 0;
8605
Willy Tarreaub2513902006-12-17 14:52:38 +01008606 return 1;
8607}
8608
Willy Tarreau4076a152009-04-02 15:18:36 +02008609/*
8610 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008611 * By default it tries to report the error position as msg->err_pos. However if
8612 * this one is not set, it will then report msg->next, which is the last known
8613 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008614 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008615 */
8616void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008617 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008618 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008619{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008620 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008621 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008622
Willy Tarreau9b28e032012-10-12 23:49:43 +02008623 es->len = MIN(chn->buf->i, sizeof(es->buf));
8624 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008625 len1 = MIN(len1, es->len);
8626 len2 = es->len - len1; /* remaining data if buffer wraps */
8627
Willy Tarreau9b28e032012-10-12 23:49:43 +02008628 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008629 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008631
Willy Tarreau4076a152009-04-02 15:18:36 +02008632 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008633 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008634 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008635 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008636
Willy Tarreau4076a152009-04-02 15:18:36 +02008637 es->when = date; // user-visible date
8638 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008639 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008640 es->oe = other_end;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008641 if (objt_conn(s->req.prod->end))
8642 es->src = __objt_conn(s->req.prod->end)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008643 else
8644 memset(&es->src, 0, sizeof(es->src));
8645
Willy Tarreau078272e2010-12-12 12:46:33 +01008646 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008647 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008648 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008649 es->s_flags = s->flags;
8650 es->t_flags = s->txn.flags;
8651 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008652 es->b_out = chn->buf->o;
8653 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008654 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008655 es->m_clen = msg->chunk_len;
8656 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008657}
Willy Tarreaub2513902006-12-17 14:52:38 +01008658
Willy Tarreau294c4732011-12-16 21:35:50 +01008659/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8660 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8661 * performed over the whole headers. Otherwise it must contain a valid header
8662 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8663 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8664 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8665 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008666 * -1. The value fetch stops at commas, so this function is suited for use with
8667 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008668 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008669 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008670unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008671 struct hdr_idx *idx, int occ,
8672 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008673{
Willy Tarreau294c4732011-12-16 21:35:50 +01008674 struct hdr_ctx local_ctx;
8675 char *ptr_hist[MAX_HDR_HISTORY];
8676 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008677 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008678 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008679
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 if (!ctx) {
8681 local_ctx.idx = 0;
8682 ctx = &local_ctx;
8683 }
8684
Willy Tarreaubce70882009-09-07 11:51:47 +02008685 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008686 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008687 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008688 occ--;
8689 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008690 *vptr = ctx->line + ctx->val;
8691 *vlen = ctx->vlen;
8692 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008693 }
8694 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008695 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 }
8697
8698 /* negative occurrence, we scan all the list then walk back */
8699 if (-occ > MAX_HDR_HISTORY)
8700 return 0;
8701
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008703 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8705 len_hist[hist_ptr] = ctx->vlen;
8706 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008707 hist_ptr = 0;
8708 found++;
8709 }
8710 if (-occ > found)
8711 return 0;
8712 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008713 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8714 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8715 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008716 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008717 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 if (hist_ptr >= MAX_HDR_HISTORY)
8719 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008720 *vptr = ptr_hist[hist_ptr];
8721 *vlen = len_hist[hist_ptr];
8722 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008723}
8724
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008725/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8726 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8727 * performed over the whole headers. Otherwise it must contain a valid header
8728 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8729 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8730 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8731 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8732 * -1. This function differs from http_get_hdr() in that it only returns full
8733 * line header values and does not stop at commas.
8734 * The return value is 0 if nothing was found, or non-zero otherwise.
8735 */
8736unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8737 struct hdr_idx *idx, int occ,
8738 struct hdr_ctx *ctx, char **vptr, int *vlen)
8739{
8740 struct hdr_ctx local_ctx;
8741 char *ptr_hist[MAX_HDR_HISTORY];
8742 int len_hist[MAX_HDR_HISTORY];
8743 unsigned int hist_ptr;
8744 int found;
8745
8746 if (!ctx) {
8747 local_ctx.idx = 0;
8748 ctx = &local_ctx;
8749 }
8750
8751 if (occ >= 0) {
8752 /* search from the beginning */
8753 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8754 occ--;
8755 if (occ <= 0) {
8756 *vptr = ctx->line + ctx->val;
8757 *vlen = ctx->vlen;
8758 return 1;
8759 }
8760 }
8761 return 0;
8762 }
8763
8764 /* negative occurrence, we scan all the list then walk back */
8765 if (-occ > MAX_HDR_HISTORY)
8766 return 0;
8767
8768 found = hist_ptr = 0;
8769 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8770 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8771 len_hist[hist_ptr] = ctx->vlen;
8772 if (++hist_ptr >= MAX_HDR_HISTORY)
8773 hist_ptr = 0;
8774 found++;
8775 }
8776 if (-occ > found)
8777 return 0;
8778 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8779 * find occurrence -occ, so we have to check [hist_ptr+occ].
8780 */
8781 hist_ptr += occ;
8782 if (hist_ptr >= MAX_HDR_HISTORY)
8783 hist_ptr -= MAX_HDR_HISTORY;
8784 *vptr = ptr_hist[hist_ptr];
8785 *vlen = len_hist[hist_ptr];
8786 return 1;
8787}
8788
Willy Tarreaubaaee002006-06-26 02:48:02 +02008789/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008790 * Print a debug line with a header. Always stop at the first CR or LF char,
8791 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8792 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008793 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008794void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008795{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008796 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008797 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008798 dir,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008799 objt_conn(s->req.prod->end) ? (unsigned short)objt_conn(s->req.prod->end)->t.sock.fd : -1,
8800 objt_conn(s->req.cons->end) ? (unsigned short)objt_conn(s->req.cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008801
8802 for (max = 0; start + max < end; max++)
8803 if (start[max] == '\r' || start[max] == '\n')
8804 break;
8805
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008806 UBOUND(max, trash.size - trash.len - 3);
8807 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8808 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008809 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008810}
8811
Willy Tarreau0937bc42009-12-22 15:03:09 +01008812/*
8813 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8814 * the required fields are properly allocated and that we only need to (re)init
8815 * them. This should be used before processing any new request.
8816 */
8817void http_init_txn(struct session *s)
8818{
8819 struct http_txn *txn = &s->txn;
8820 struct proxy *fe = s->fe;
8821
8822 txn->flags = 0;
8823 txn->status = -1;
8824
Willy Tarreauf64d1412010-10-07 20:06:11 +02008825 txn->cookie_first_date = 0;
8826 txn->cookie_last_date = 0;
8827
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008828 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008829 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008830 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008831 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008832 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008833 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008834 txn->req.chunk_len = 0LL;
8835 txn->req.body_len = 0LL;
8836 txn->rsp.chunk_len = 0LL;
8837 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008838 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8839 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008840 txn->req.chn = &s->req;
8841 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008842
8843 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844
8845 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8846 if (fe->options2 & PR_O2_REQBUG_OK)
8847 txn->req.err_pos = -1; /* let buggy requests pass */
8848
Willy Tarreau46023632010-01-07 22:51:47 +01008849 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008850 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8851
Willy Tarreau46023632010-01-07 22:51:47 +01008852 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008853 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8854
8855 if (txn->hdr_idx.v)
8856 hdr_idx_init(&txn->hdr_idx);
8857}
8858
8859/* to be used at the end of a transaction */
8860void http_end_txn(struct session *s)
8861{
8862 struct http_txn *txn = &s->txn;
8863
Willy Tarreau75195602014-03-11 15:48:55 +01008864 /* release any possible compression context */
8865 if (s->flags & SN_COMP_READY)
8866 s->comp_algo->end(&s->comp_ctx);
8867 s->comp_algo = NULL;
8868 s->flags &= ~SN_COMP_READY;
8869
Willy Tarreau0937bc42009-12-22 15:03:09 +01008870 /* these ones will have been dynamically allocated */
8871 pool_free2(pool2_requri, txn->uri);
8872 pool_free2(pool2_capture, txn->cli_cookie);
8873 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008874 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008875 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008876
William Lallemanda73203e2012-03-12 12:48:57 +01008877 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008878 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008879 txn->uri = NULL;
8880 txn->srv_cookie = NULL;
8881 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008882
8883 if (txn->req.cap) {
8884 struct cap_hdr *h;
8885 for (h = s->fe->req_cap; h; h = h->next)
8886 pool_free2(h->pool, txn->req.cap[h->index]);
8887 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8888 }
8889
8890 if (txn->rsp.cap) {
8891 struct cap_hdr *h;
8892 for (h = s->fe->rsp_cap; h; h = h->next)
8893 pool_free2(h->pool, txn->rsp.cap[h->index]);
8894 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8895 }
8896
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897}
8898
8899/* to be used at the end of a transaction to prepare a new one */
8900void http_reset_txn(struct session *s)
8901{
8902 http_end_txn(s);
8903 http_init_txn(s);
8904
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008905 /* reinitialise the current rule list pointer to NULL. We are sure that
8906 * any rulelist match the NULL pointer.
8907 */
8908 s->current_rule_list = NULL;
8909
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008911 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008912 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008913 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008914 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008915 /* re-init store persistence */
8916 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008917 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008918
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008920
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008921 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922
Willy Tarreau739cfba2010-01-25 23:11:14 +01008923 /* We must trim any excess data from the response buffer, because we
8924 * may have blocked an invalid response from a server that we don't
8925 * want to accidentely forward once we disable the analysers, nor do
8926 * we want those data to come along with next response. A typical
8927 * example of such data would be from a buggy server responding to
8928 * a HEAD with some data, or sending more than the advertised
8929 * content-length.
8930 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008931 if (unlikely(s->res.buf->i))
8932 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008933
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008934 s->req.rto = s->fe->timeout.client;
8935 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008936
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008937 s->res.rto = TICK_ETERNITY;
8938 s->res.wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008939
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008940 s->req.rex = TICK_ETERNITY;
8941 s->req.wex = TICK_ETERNITY;
8942 s->req.analyse_exp = TICK_ETERNITY;
8943 s->res.rex = TICK_ETERNITY;
8944 s->res.wex = TICK_ETERNITY;
8945 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008946}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008947
Sasha Pachev218f0642014-06-16 12:05:59 -06008948void free_http_res_rules(struct list *r)
8949{
8950 struct http_res_rule *tr, *pr;
8951
8952 list_for_each_entry_safe(pr, tr, r, list) {
8953 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008954 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008955 free(pr);
8956 }
8957}
8958
8959void free_http_req_rules(struct list *r)
8960{
Willy Tarreauff011f22011-01-06 17:51:27 +01008961 struct http_req_rule *tr, *pr;
8962
8963 list_for_each_entry_safe(pr, tr, r, list) {
8964 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008965 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008966 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008967
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008968 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008969 free(pr);
8970 }
8971}
8972
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008973/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008974struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8975{
8976 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008977 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008978 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008979 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008980
8981 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8982 if (!rule) {
8983 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008984 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008985 }
8986
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008987 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008988 rule->action = HTTP_REQ_ACT_ALLOW;
8989 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008990 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008991 rule->action = HTTP_REQ_ACT_DENY;
8992 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008993 } else if (!strcmp(args[0], "tarpit")) {
8994 rule->action = HTTP_REQ_ACT_TARPIT;
8995 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008996 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008997 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008998 cur_arg = 1;
8999
9000 while(*args[cur_arg]) {
9001 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009002 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009003 cur_arg+=2;
9004 continue;
9005 } else
9006 break;
9007 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009008 } else if (!strcmp(args[0], "set-nice")) {
9009 rule->action = HTTP_REQ_ACT_SET_NICE;
9010 cur_arg = 1;
9011
9012 if (!*args[cur_arg] ||
9013 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9014 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9015 file, linenum, args[0]);
9016 goto out_err;
9017 }
9018 rule->arg.nice = atoi(args[cur_arg]);
9019 if (rule->arg.nice < -1024)
9020 rule->arg.nice = -1024;
9021 else if (rule->arg.nice > 1024)
9022 rule->arg.nice = 1024;
9023 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009024 } else if (!strcmp(args[0], "set-tos")) {
9025#ifdef IP_TOS
9026 char *err;
9027 rule->action = HTTP_REQ_ACT_SET_TOS;
9028 cur_arg = 1;
9029
9030 if (!*args[cur_arg] ||
9031 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9032 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9033 file, linenum, args[0]);
9034 goto out_err;
9035 }
9036
9037 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9038 if (err && *err != '\0') {
9039 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9040 file, linenum, err, args[0]);
9041 goto out_err;
9042 }
9043 cur_arg++;
9044#else
9045 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9046 goto out_err;
9047#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009048 } else if (!strcmp(args[0], "set-mark")) {
9049#ifdef SO_MARK
9050 char *err;
9051 rule->action = HTTP_REQ_ACT_SET_MARK;
9052 cur_arg = 1;
9053
9054 if (!*args[cur_arg] ||
9055 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9056 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9057 file, linenum, args[0]);
9058 goto out_err;
9059 }
9060
9061 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9062 if (err && *err != '\0') {
9063 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9064 file, linenum, err, args[0]);
9065 goto out_err;
9066 }
9067 cur_arg++;
9068 global.last_checks |= LSTCHK_NETADM;
9069#else
9070 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9071 goto out_err;
9072#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009073 } else if (!strcmp(args[0], "set-log-level")) {
9074 rule->action = HTTP_REQ_ACT_SET_LOGL;
9075 cur_arg = 1;
9076
9077 if (!*args[cur_arg] ||
9078 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9079 bad_log_level:
9080 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9081 file, linenum, args[0]);
9082 goto out_err;
9083 }
9084 if (strcmp(args[cur_arg], "silent") == 0)
9085 rule->arg.loglevel = -1;
9086 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9087 goto bad_log_level;
9088 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009089 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9090 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9091 cur_arg = 1;
9092
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009093 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9094 (*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 +01009095 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9096 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009097 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009098 }
9099
9100 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9101 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9102 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009103
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009104 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009105 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009106 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9107 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009108 free(proxy->conf.lfs_file);
9109 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9110 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009111 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009112 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9113 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009114 cur_arg = 1;
9115
9116 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009117 (*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 -06009118 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9119 file, linenum, args[0]);
9120 goto out_err;
9121 }
9122
9123 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9124 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9125 LIST_INIT(&rule->arg.hdr_add.fmt);
9126
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009127 error = NULL;
9128 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9129 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9130 args[cur_arg + 1], error);
9131 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009132 goto out_err;
9133 }
9134
9135 proxy->conf.args.ctx = ARGC_HRQ;
9136 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9137 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9138 file, linenum);
9139
9140 free(proxy->conf.lfs_file);
9141 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9142 proxy->conf.lfs_line = proxy->conf.args.line;
9143 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009144 } else if (strcmp(args[0], "del-header") == 0) {
9145 rule->action = HTTP_REQ_ACT_DEL_HDR;
9146 cur_arg = 1;
9147
9148 if (!*args[cur_arg] ||
9149 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9150 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9151 file, linenum, args[0]);
9152 goto out_err;
9153 }
9154
9155 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9156 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9157
9158 proxy->conf.args.ctx = ARGC_HRQ;
9159 free(proxy->conf.lfs_file);
9160 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9161 proxy->conf.lfs_line = proxy->conf.args.line;
9162 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009163 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9164 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009165 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009166 struct sample_expr *expr;
9167 unsigned int where;
9168 char *err = NULL;
9169
9170 cur_arg = 1;
9171 proxy->conf.args.ctx = ARGC_TRK;
9172
9173 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9174 if (!expr) {
9175 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], err);
9177 free(err);
9178 goto out_err;
9179 }
9180
9181 where = 0;
9182 if (proxy->cap & PR_CAP_FE)
9183 where |= SMP_VAL_FE_HRQ_HDR;
9184 if (proxy->cap & PR_CAP_BE)
9185 where |= SMP_VAL_BE_HRQ_HDR;
9186
9187 if (!(expr->fetch->val & where)) {
9188 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9189 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9190 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9191 args[cur_arg-1], sample_src_names(expr->fetch->use));
9192 free(expr);
9193 goto out_err;
9194 }
9195
9196 if (strcmp(args[cur_arg], "table") == 0) {
9197 cur_arg++;
9198 if (!args[cur_arg]) {
9199 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9200 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9201 free(expr);
9202 goto out_err;
9203 }
9204 /* we copy the table name for now, it will be resolved later */
9205 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9206 cur_arg++;
9207 }
9208 rule->act_prm.trk_ctr.expr = expr;
9209 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009210 } else if (strcmp(args[0], "redirect") == 0) {
9211 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009212 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009213
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009214 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009215 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9216 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9217 goto out_err;
9218 }
9219
9220 /* this redirect rule might already contain a parsed condition which
9221 * we'll pass to the http-request rule.
9222 */
9223 rule->action = HTTP_REQ_ACT_REDIR;
9224 rule->arg.redir = redir;
9225 rule->cond = redir->cond;
9226 redir->cond = NULL;
9227 cur_arg = 2;
9228 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009229 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9230 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9231 rule->action = HTTP_REQ_ACT_ADD_ACL;
9232 /*
9233 * '+ 8' for 'add-acl('
9234 * '- 9' for 'add-acl(' + trailing ')'
9235 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009236 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009237
9238 cur_arg = 1;
9239
9240 if (!*args[cur_arg] ||
9241 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9242 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9243 file, linenum, args[0]);
9244 goto out_err;
9245 }
9246
9247 LIST_INIT(&rule->arg.map.key);
9248 proxy->conf.args.ctx = ARGC_HRQ;
9249 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9250 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9251 file, linenum);
9252 free(proxy->conf.lfs_file);
9253 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9254 proxy->conf.lfs_line = proxy->conf.args.line;
9255 cur_arg += 1;
9256 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9257 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9258 rule->action = HTTP_REQ_ACT_DEL_ACL;
9259 /*
9260 * '+ 8' for 'del-acl('
9261 * '- 9' for 'del-acl(' + trailing ')'
9262 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009263 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009264
9265 cur_arg = 1;
9266
9267 if (!*args[cur_arg] ||
9268 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9269 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9270 file, linenum, args[0]);
9271 goto out_err;
9272 }
9273
9274 LIST_INIT(&rule->arg.map.key);
9275 proxy->conf.args.ctx = ARGC_HRQ;
9276 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9277 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9278 file, linenum);
9279 free(proxy->conf.lfs_file);
9280 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9281 proxy->conf.lfs_line = proxy->conf.args.line;
9282 cur_arg += 1;
9283 } else if (strncmp(args[0], "del-map", 7) == 0) {
9284 /* http-request del-map(<reference (map name)>) <key pattern> */
9285 rule->action = HTTP_REQ_ACT_DEL_MAP;
9286 /*
9287 * '+ 8' for 'del-map('
9288 * '- 9' for 'del-map(' + trailing ')'
9289 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009290 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009291
9292 cur_arg = 1;
9293
9294 if (!*args[cur_arg] ||
9295 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9296 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9297 file, linenum, args[0]);
9298 goto out_err;
9299 }
9300
9301 LIST_INIT(&rule->arg.map.key);
9302 proxy->conf.args.ctx = ARGC_HRQ;
9303 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9304 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9305 file, linenum);
9306 free(proxy->conf.lfs_file);
9307 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9308 proxy->conf.lfs_line = proxy->conf.args.line;
9309 cur_arg += 1;
9310 } else if (strncmp(args[0], "set-map", 7) == 0) {
9311 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9312 rule->action = HTTP_REQ_ACT_SET_MAP;
9313 /*
9314 * '+ 8' for 'set-map('
9315 * '- 9' for 'set-map(' + trailing ')'
9316 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009317 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009318
9319 cur_arg = 1;
9320
9321 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9322 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9323 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9324 file, linenum, args[0]);
9325 goto out_err;
9326 }
9327
9328 LIST_INIT(&rule->arg.map.key);
9329 LIST_INIT(&rule->arg.map.value);
9330 proxy->conf.args.ctx = ARGC_HRQ;
9331
9332 /* key pattern */
9333 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9334 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9335 file, linenum);
9336
9337 /* value pattern */
9338 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9339 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9340 file, linenum);
9341 free(proxy->conf.lfs_file);
9342 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9343 proxy->conf.lfs_line = proxy->conf.args.line;
9344
9345 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009346 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9347 char *errmsg = NULL;
9348 cur_arg = 1;
9349 /* try in the module list */
9350 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9351 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9352 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9353 free(errmsg);
9354 goto out_err;
9355 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009356 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009357 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 +01009358 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009359 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009360 }
9361
9362 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9363 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009364 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009365
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009366 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9367 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9368 file, linenum, args[0], errmsg);
9369 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009370 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009371 }
9372 rule->cond = cond;
9373 }
9374 else if (*args[cur_arg]) {
9375 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9376 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9377 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009378 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009379 }
9380
9381 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009382 out_err:
9383 free(rule);
9384 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009385}
9386
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009387/* parse an "http-respose" rule */
9388struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9389{
9390 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009391 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009392 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009393 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009394
9395 rule = calloc(1, sizeof(*rule));
9396 if (!rule) {
9397 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9398 goto out_err;
9399 }
9400
9401 if (!strcmp(args[0], "allow")) {
9402 rule->action = HTTP_RES_ACT_ALLOW;
9403 cur_arg = 1;
9404 } else if (!strcmp(args[0], "deny")) {
9405 rule->action = HTTP_RES_ACT_DENY;
9406 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009407 } else if (!strcmp(args[0], "set-nice")) {
9408 rule->action = HTTP_RES_ACT_SET_NICE;
9409 cur_arg = 1;
9410
9411 if (!*args[cur_arg] ||
9412 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9413 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9414 file, linenum, args[0]);
9415 goto out_err;
9416 }
9417 rule->arg.nice = atoi(args[cur_arg]);
9418 if (rule->arg.nice < -1024)
9419 rule->arg.nice = -1024;
9420 else if (rule->arg.nice > 1024)
9421 rule->arg.nice = 1024;
9422 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009423 } else if (!strcmp(args[0], "set-tos")) {
9424#ifdef IP_TOS
9425 char *err;
9426 rule->action = HTTP_RES_ACT_SET_TOS;
9427 cur_arg = 1;
9428
9429 if (!*args[cur_arg] ||
9430 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9431 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9432 file, linenum, args[0]);
9433 goto out_err;
9434 }
9435
9436 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9437 if (err && *err != '\0') {
9438 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9439 file, linenum, err, args[0]);
9440 goto out_err;
9441 }
9442 cur_arg++;
9443#else
9444 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9445 goto out_err;
9446#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009447 } else if (!strcmp(args[0], "set-mark")) {
9448#ifdef SO_MARK
9449 char *err;
9450 rule->action = HTTP_RES_ACT_SET_MARK;
9451 cur_arg = 1;
9452
9453 if (!*args[cur_arg] ||
9454 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9455 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9456 file, linenum, args[0]);
9457 goto out_err;
9458 }
9459
9460 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9461 if (err && *err != '\0') {
9462 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9463 file, linenum, err, args[0]);
9464 goto out_err;
9465 }
9466 cur_arg++;
9467 global.last_checks |= LSTCHK_NETADM;
9468#else
9469 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9470 goto out_err;
9471#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009472 } else if (!strcmp(args[0], "set-log-level")) {
9473 rule->action = HTTP_RES_ACT_SET_LOGL;
9474 cur_arg = 1;
9475
9476 if (!*args[cur_arg] ||
9477 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9478 bad_log_level:
9479 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9480 file, linenum, args[0]);
9481 goto out_err;
9482 }
9483 if (strcmp(args[cur_arg], "silent") == 0)
9484 rule->arg.loglevel = -1;
9485 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9486 goto bad_log_level;
9487 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009488 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9489 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9490 cur_arg = 1;
9491
9492 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9493 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9494 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9495 file, linenum, args[0]);
9496 goto out_err;
9497 }
9498
9499 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9500 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9501 LIST_INIT(&rule->arg.hdr_add.fmt);
9502
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009503 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009504 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009505 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9506 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009507 free(proxy->conf.lfs_file);
9508 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9509 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009510 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009511 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009512 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009513 cur_arg = 1;
9514
9515 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009516 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9517 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009518 file, linenum, args[0]);
9519 goto out_err;
9520 }
9521
9522 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9523 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9524 LIST_INIT(&rule->arg.hdr_add.fmt);
9525
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009526 error = NULL;
9527 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9528 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9529 args[cur_arg + 1], error);
9530 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009531 goto out_err;
9532 }
9533
9534 proxy->conf.args.ctx = ARGC_HRQ;
9535 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9536 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9537 file, linenum);
9538
9539 free(proxy->conf.lfs_file);
9540 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9541 proxy->conf.lfs_line = proxy->conf.args.line;
9542 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009543 } else if (strcmp(args[0], "del-header") == 0) {
9544 rule->action = HTTP_RES_ACT_DEL_HDR;
9545 cur_arg = 1;
9546
9547 if (!*args[cur_arg] ||
9548 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9549 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9550 file, linenum, args[0]);
9551 goto out_err;
9552 }
9553
9554 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9555 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9556
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009557 proxy->conf.args.ctx = ARGC_HRS;
9558 free(proxy->conf.lfs_file);
9559 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9560 proxy->conf.lfs_line = proxy->conf.args.line;
9561 cur_arg += 1;
9562 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9563 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9564 rule->action = HTTP_RES_ACT_ADD_ACL;
9565 /*
9566 * '+ 8' for 'add-acl('
9567 * '- 9' for 'add-acl(' + trailing ')'
9568 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009569 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009570
9571 cur_arg = 1;
9572
9573 if (!*args[cur_arg] ||
9574 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9575 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9576 file, linenum, args[0]);
9577 goto out_err;
9578 }
9579
9580 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009581 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009582 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9583 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9584 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009585 free(proxy->conf.lfs_file);
9586 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9587 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009588
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009589 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009590 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9591 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9592 rule->action = HTTP_RES_ACT_DEL_ACL;
9593 /*
9594 * '+ 8' for 'del-acl('
9595 * '- 9' for 'del-acl(' + trailing ')'
9596 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009597 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009598
9599 cur_arg = 1;
9600
9601 if (!*args[cur_arg] ||
9602 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9603 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9604 file, linenum, args[0]);
9605 goto out_err;
9606 }
9607
9608 LIST_INIT(&rule->arg.map.key);
9609 proxy->conf.args.ctx = ARGC_HRS;
9610 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9611 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9612 file, linenum);
9613 free(proxy->conf.lfs_file);
9614 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9615 proxy->conf.lfs_line = proxy->conf.args.line;
9616 cur_arg += 1;
9617 } else if (strncmp(args[0], "del-map", 7) == 0) {
9618 /* http-response del-map(<reference (map name)>) <key pattern> */
9619 rule->action = HTTP_RES_ACT_DEL_MAP;
9620 /*
9621 * '+ 8' for 'del-map('
9622 * '- 9' for 'del-map(' + trailing ')'
9623 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009624 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009625
9626 cur_arg = 1;
9627
9628 if (!*args[cur_arg] ||
9629 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9630 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9631 file, linenum, args[0]);
9632 goto out_err;
9633 }
9634
9635 LIST_INIT(&rule->arg.map.key);
9636 proxy->conf.args.ctx = ARGC_HRS;
9637 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9638 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9639 file, linenum);
9640 free(proxy->conf.lfs_file);
9641 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9642 proxy->conf.lfs_line = proxy->conf.args.line;
9643 cur_arg += 1;
9644 } else if (strncmp(args[0], "set-map", 7) == 0) {
9645 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9646 rule->action = HTTP_RES_ACT_SET_MAP;
9647 /*
9648 * '+ 8' for 'set-map('
9649 * '- 9' for 'set-map(' + trailing ')'
9650 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009651 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009652
9653 cur_arg = 1;
9654
9655 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9656 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9657 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9658 file, linenum, args[0]);
9659 goto out_err;
9660 }
9661
9662 LIST_INIT(&rule->arg.map.key);
9663 LIST_INIT(&rule->arg.map.value);
9664
9665 proxy->conf.args.ctx = ARGC_HRS;
9666
9667 /* key pattern */
9668 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9669 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9670 file, linenum);
9671
9672 /* value pattern */
9673 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9674 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9675 file, linenum);
9676
9677 free(proxy->conf.lfs_file);
9678 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9679 proxy->conf.lfs_line = proxy->conf.args.line;
9680
9681 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009682 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9683 char *errmsg = NULL;
9684 cur_arg = 1;
9685 /* try in the module list */
9686 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9687 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9688 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9689 free(errmsg);
9690 goto out_err;
9691 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009692 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009693 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 +02009694 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9695 goto out_err;
9696 }
9697
9698 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9699 struct acl_cond *cond;
9700 char *errmsg = NULL;
9701
9702 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9703 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9704 file, linenum, args[0], errmsg);
9705 free(errmsg);
9706 goto out_err;
9707 }
9708 rule->cond = cond;
9709 }
9710 else if (*args[cur_arg]) {
9711 Alert("parsing [%s:%d]: 'http-response %s' expects"
9712 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9713 file, linenum, args[0], args[cur_arg]);
9714 goto out_err;
9715 }
9716
9717 return rule;
9718 out_err:
9719 free(rule);
9720 return NULL;
9721}
9722
Willy Tarreau4baae242012-12-27 12:00:31 +01009723/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009724 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9725 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009726 */
9727struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009728 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009729{
9730 struct redirect_rule *rule;
9731 int cur_arg;
9732 int type = REDIRECT_TYPE_NONE;
9733 int code = 302;
9734 const char *destination = NULL;
9735 const char *cookie = NULL;
9736 int cookie_set = 0;
9737 unsigned int flags = REDIRECT_FLAG_NONE;
9738 struct acl_cond *cond = NULL;
9739
9740 cur_arg = 0;
9741 while (*(args[cur_arg])) {
9742 if (strcmp(args[cur_arg], "location") == 0) {
9743 if (!*args[cur_arg + 1])
9744 goto missing_arg;
9745
9746 type = REDIRECT_TYPE_LOCATION;
9747 cur_arg++;
9748 destination = args[cur_arg];
9749 }
9750 else if (strcmp(args[cur_arg], "prefix") == 0) {
9751 if (!*args[cur_arg + 1])
9752 goto missing_arg;
9753
9754 type = REDIRECT_TYPE_PREFIX;
9755 cur_arg++;
9756 destination = args[cur_arg];
9757 }
9758 else if (strcmp(args[cur_arg], "scheme") == 0) {
9759 if (!*args[cur_arg + 1])
9760 goto missing_arg;
9761
9762 type = REDIRECT_TYPE_SCHEME;
9763 cur_arg++;
9764 destination = args[cur_arg];
9765 }
9766 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9767 if (!*args[cur_arg + 1])
9768 goto missing_arg;
9769
9770 cur_arg++;
9771 cookie = args[cur_arg];
9772 cookie_set = 1;
9773 }
9774 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9775 if (!*args[cur_arg + 1])
9776 goto missing_arg;
9777
9778 cur_arg++;
9779 cookie = args[cur_arg];
9780 cookie_set = 0;
9781 }
9782 else if (strcmp(args[cur_arg], "code") == 0) {
9783 if (!*args[cur_arg + 1])
9784 goto missing_arg;
9785
9786 cur_arg++;
9787 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009788 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009789 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009790 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009791 args[cur_arg - 1], args[cur_arg]);
9792 return NULL;
9793 }
9794 }
9795 else if (!strcmp(args[cur_arg],"drop-query")) {
9796 flags |= REDIRECT_FLAG_DROP_QS;
9797 }
9798 else if (!strcmp(args[cur_arg],"append-slash")) {
9799 flags |= REDIRECT_FLAG_APPEND_SLASH;
9800 }
9801 else if (strcmp(args[cur_arg], "if") == 0 ||
9802 strcmp(args[cur_arg], "unless") == 0) {
9803 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9804 if (!cond) {
9805 memprintf(errmsg, "error in condition: %s", *errmsg);
9806 return NULL;
9807 }
9808 break;
9809 }
9810 else {
9811 memprintf(errmsg,
9812 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9813 args[cur_arg]);
9814 return NULL;
9815 }
9816 cur_arg++;
9817 }
9818
9819 if (type == REDIRECT_TYPE_NONE) {
9820 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9821 return NULL;
9822 }
9823
9824 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9825 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009826 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009827
9828 if (!use_fmt) {
9829 /* old-style static redirect rule */
9830 rule->rdr_str = strdup(destination);
9831 rule->rdr_len = strlen(destination);
9832 }
9833 else {
9834 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009835
9836 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9837 * if prefix == "/", we don't want to add anything, otherwise it
9838 * makes it hard for the user to configure a self-redirection.
9839 */
Godbachd9722032014-12-18 15:44:58 +08009840 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009841 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009842 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009843 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9844 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009845 free(curproxy->conf.lfs_file);
9846 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9847 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009848 }
9849 }
9850
Willy Tarreau4baae242012-12-27 12:00:31 +01009851 if (cookie) {
9852 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9853 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9854 */
9855 rule->cookie_len = strlen(cookie);
9856 if (cookie_set) {
9857 rule->cookie_str = malloc(rule->cookie_len + 10);
9858 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9859 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9860 rule->cookie_len += 9;
9861 } else {
9862 rule->cookie_str = malloc(rule->cookie_len + 21);
9863 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9864 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9865 rule->cookie_len += 20;
9866 }
9867 }
9868 rule->type = type;
9869 rule->code = code;
9870 rule->flags = flags;
9871 LIST_INIT(&rule->list);
9872 return rule;
9873
9874 missing_arg:
9875 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9876 return NULL;
9877}
9878
Willy Tarreau8797c062007-05-07 00:55:35 +02009879/************************************************************************/
9880/* The code below is dedicated to ACL parsing and matching */
9881/************************************************************************/
9882
9883
Willy Tarreau14174bc2012-04-16 14:34:04 +02009884/* This function ensures that the prerequisites for an L7 fetch are ready,
9885 * which means that a request or response is ready. If some data is missing,
9886 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009887 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9888 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009889 *
9890 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009891 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9892 * decide whether or not an HTTP message is present ;
9893 * 0 if the requested data cannot be fetched or if it is certain that
9894 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009895 * 1 if an HTTP message is ready
9896 */
9897static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009898smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009899 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009900{
9901 struct http_txn *txn = l7;
9902 struct http_msg *msg = &txn->req;
9903
9904 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9905 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9906 */
9907
9908 if (unlikely(!s || !txn))
9909 return 0;
9910
9911 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009912 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009913
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009914 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009915 /* If the buffer does not leave enough free space at the end,
9916 * we must first realign it.
9917 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009918 if (s->req.buf->p > s->req.buf->data &&
9919 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9920 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009921
Willy Tarreau14174bc2012-04-16 14:34:04 +02009922 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009923 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009924 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009925
9926 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009927 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009928 http_msg_analyzer(msg, &txn->hdr_idx);
9929
9930 /* Still no valid request ? */
9931 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009932 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009933 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009934 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009935 }
9936 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009937 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009938 return 0;
9939 }
9940
9941 /* OK we just got a valid HTTP request. We have some minor
9942 * preparation to perform so that further checks can rely
9943 * on HTTP tests.
9944 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009945
9946 /* If the request was parsed but was too large, we must absolutely
9947 * return an error so that it is not processed. At the moment this
9948 * cannot happen, but if the parsers are to change in the future,
9949 * we want this check to be maintained.
9950 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009951 if (unlikely(s->req.buf->i + s->req.buf->p >
9952 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009953 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009954 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009955 return 1;
9956 }
9957
Willy Tarreau9b28e032012-10-12 23:49:43 +02009958 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009959 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9960 s->flags |= SN_REDIRECTABLE;
9961
Willy Tarreau506d0502013-07-06 13:29:24 +02009962 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9963 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009964 }
9965
Willy Tarreau506d0502013-07-06 13:29:24 +02009966 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009967 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009968 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009969
9970 /* otherwise everything's ready for the request */
9971 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009972 else {
9973 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009974 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9975 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009976 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009977 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009978 }
9979
9980 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009981 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009982 return 1;
9983}
Willy Tarreau8797c062007-05-07 00:55:35 +02009984
Willy Tarreau6c616e02014-06-25 16:56:41 +02009985/* Note: these functinos *do* modify the sample. Even in case of success, at
9986 * least the type and uint value are modified.
9987 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009988#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009989 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 +02009990
Willy Tarreau24e32d82012-04-23 23:55:44 +02009991#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009992 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 +02009993
Willy Tarreau8797c062007-05-07 00:55:35 +02009994
9995/* 1. Check on METHOD
9996 * We use the pre-parsed method if it is known, and store its number as an
9997 * integer. If it is unknown, we use the pointer and the length.
9998 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009999static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010000{
10001 int len, meth;
10002
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010003 len = strlen(text);
10004 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010005
10006 pattern->val.i = meth;
10007 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010008 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010009 pattern->len = len;
10010 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010011 else {
10012 pattern->ptr.str = NULL;
10013 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010014 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010015 return 1;
10016}
10017
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010018/* This function fetches the method of current HTTP request and stores
10019 * it in the global pattern struct as a chunk. There are two possibilities :
10020 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10021 * in <len> and <ptr> is NULL ;
10022 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10023 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010024 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010025 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010026static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010027smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010028 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010029{
10030 int meth;
10031 struct http_txn *txn = l7;
10032
Willy Tarreau24e32d82012-04-23 23:55:44 +020010033 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010034
Willy Tarreau8797c062007-05-07 00:55:35 +020010035 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010036 smp->type = SMP_T_METH;
10037 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010039 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10040 /* ensure the indexes are not affected */
10041 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010042 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010043 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10044 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010045 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010046 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010047 return 1;
10048}
10049
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010050/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010051static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010052{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010053 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010054 struct pattern_list *lst;
10055 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010056
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010057 list_for_each_entry(lst, &expr->patterns, list) {
10058 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010059
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010060 /* well-known method */
10061 if (pattern->val.i != HTTP_METH_OTHER) {
10062 if (smp->data.meth.meth == pattern->val.i)
10063 return pattern;
10064 else
10065 continue;
10066 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010067
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010068 /* Other method, we must compare the strings */
10069 if (pattern->len != smp->data.meth.str.len)
10070 continue;
10071
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010072 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010073 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10074 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010075 return pattern;
10076 }
10077 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010078}
10079
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010080static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010081smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010082 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010083{
10084 struct http_txn *txn = l7;
10085 char *ptr;
10086 int len;
10087
Willy Tarreauc0239e02012-04-16 14:42:55 +020010088 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010089
Willy Tarreau8797c062007-05-07 00:55:35 +020010090 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010091 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010092
10093 while ((len-- > 0) && (*ptr++ != '/'));
10094 if (len <= 0)
10095 return 0;
10096
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010097 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010098 smp->data.str.str = ptr;
10099 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010100
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010101 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010102 return 1;
10103}
10104
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010105static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010106smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010107 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010108{
10109 struct http_txn *txn = l7;
10110 char *ptr;
10111 int len;
10112
Willy Tarreauc0239e02012-04-16 14:42:55 +020010113 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010114
Willy Tarreauf26b2522012-12-14 08:33:14 +010010115 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10116 return 0;
10117
Willy Tarreau8797c062007-05-07 00:55:35 +020010118 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010119 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010120
10121 while ((len-- > 0) && (*ptr++ != '/'));
10122 if (len <= 0)
10123 return 0;
10124
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010125 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010126 smp->data.str.str = ptr;
10127 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010128
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010129 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010130 return 1;
10131}
10132
10133/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010134static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010135smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010136 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010137{
10138 struct http_txn *txn = l7;
10139 char *ptr;
10140 int len;
10141
Willy Tarreauc0239e02012-04-16 14:42:55 +020010142 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010143
Willy Tarreauf26b2522012-12-14 08:33:14 +010010144 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10145 return 0;
10146
Willy Tarreau8797c062007-05-07 00:55:35 +020010147 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010148 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010149
Willy Tarreauf853c462012-04-23 18:53:56 +020010150 smp->type = SMP_T_UINT;
10151 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010152 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010153 return 1;
10154}
10155
10156/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010157static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010158smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010159 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010160{
10161 struct http_txn *txn = l7;
10162
Willy Tarreauc0239e02012-04-16 14:42:55 +020010163 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010164
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010165 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010166 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010167 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010168 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010169 return 1;
10170}
10171
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010172static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010173smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010174 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010175{
10176 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010177 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010178
Willy Tarreauc0239e02012-04-16 14:42:55 +020010179 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010180
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010181 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 +020010182 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010183 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010184
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010185 smp->type = SMP_T_IPV4;
10186 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010187 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010188 return 1;
10189}
10190
10191static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010192smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010193 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010194{
10195 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010196 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010197
Willy Tarreauc0239e02012-04-16 14:42:55 +020010198 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010199
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010200 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 +020010201 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10202 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010203
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010204 smp->type = SMP_T_UINT;
10205 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010206 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010207 return 1;
10208}
10209
Willy Tarreau185b5c42012-04-26 15:11:51 +020010210/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10211 * Accepts an optional argument of type string containing the header field name,
10212 * and an optional argument of type signed or unsigned integer to request an
10213 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010214 * headers are considered from the first one. It does not stop on commas and
10215 * returns full lines instead (useful for User-Agent or Date for example).
10216 */
10217static int
10218smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010219 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010220{
10221 struct http_txn *txn = l7;
10222 struct hdr_idx *idx = &txn->hdr_idx;
10223 struct hdr_ctx *ctx = smp->ctx.a[0];
10224 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10225 int occ = 0;
10226 const char *name_str = NULL;
10227 int name_len = 0;
10228
10229 if (!ctx) {
10230 /* first call */
10231 ctx = &static_hdr_ctx;
10232 ctx->idx = 0;
10233 smp->ctx.a[0] = ctx;
10234 }
10235
10236 if (args) {
10237 if (args[0].type != ARGT_STR)
10238 return 0;
10239 name_str = args[0].data.str.str;
10240 name_len = args[0].data.str.len;
10241
10242 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10243 occ = args[1].data.uint;
10244 }
10245
10246 CHECK_HTTP_MESSAGE_FIRST();
10247
10248 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10249 /* search for header from the beginning */
10250 ctx->idx = 0;
10251
10252 if (!occ && !(opt & SMP_OPT_ITERATE))
10253 /* no explicit occurrence and single fetch => last header by default */
10254 occ = -1;
10255
10256 if (!occ)
10257 /* prepare to report multiple occurrences for ACL fetches */
10258 smp->flags |= SMP_F_NOT_LAST;
10259
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010260 smp->type = SMP_T_STR;
10261 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010262 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10263 return 1;
10264
10265 smp->flags &= ~SMP_F_NOT_LAST;
10266 return 0;
10267}
10268
10269/* 6. Check on HTTP header count. The number of occurrences is returned.
10270 * Accepts exactly 1 argument of type string. It does not stop on commas and
10271 * returns full lines instead (useful for User-Agent or Date for example).
10272 */
10273static int
10274smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010275 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010276{
10277 struct http_txn *txn = l7;
10278 struct hdr_idx *idx = &txn->hdr_idx;
10279 struct hdr_ctx ctx;
10280 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10281 int cnt;
10282
10283 if (!args || args->type != ARGT_STR)
10284 return 0;
10285
10286 CHECK_HTTP_MESSAGE_FIRST();
10287
10288 ctx.idx = 0;
10289 cnt = 0;
10290 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10291 cnt++;
10292
10293 smp->type = SMP_T_UINT;
10294 smp->data.uint = cnt;
10295 smp->flags = SMP_F_VOL_HDR;
10296 return 1;
10297}
10298
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010299static int
10300smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010301 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010302{
10303 struct http_txn *txn = l7;
10304 struct hdr_idx *idx = &txn->hdr_idx;
10305 struct hdr_ctx ctx;
10306 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10307 struct chunk *temp;
10308 char del = ',';
10309
10310 if (args && args->type == ARGT_STR)
10311 del = *args[0].data.str.str;
10312
10313 CHECK_HTTP_MESSAGE_FIRST();
10314
10315 temp = get_trash_chunk();
10316
10317 ctx.idx = 0;
10318 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10319 if (temp->len)
10320 temp->str[temp->len++] = del;
10321 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10322 temp->len += ctx.del;
10323 }
10324
10325 smp->type = SMP_T_STR;
10326 smp->data.str.str = temp->str;
10327 smp->data.str.len = temp->len;
10328 smp->flags = SMP_F_VOL_HDR;
10329 return 1;
10330}
10331
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010332/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10333 * Accepts an optional argument of type string containing the header field name,
10334 * and an optional argument of type signed or unsigned integer to request an
10335 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010336 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010337 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010338static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010339smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010340 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010341{
10342 struct http_txn *txn = l7;
10343 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010344 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010345 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010346 int occ = 0;
10347 const char *name_str = NULL;
10348 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010349
Willy Tarreaua890d072013-04-02 12:01:06 +020010350 if (!ctx) {
10351 /* first call */
10352 ctx = &static_hdr_ctx;
10353 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010354 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010355 }
10356
Willy Tarreau185b5c42012-04-26 15:11:51 +020010357 if (args) {
10358 if (args[0].type != ARGT_STR)
10359 return 0;
10360 name_str = args[0].data.str.str;
10361 name_len = args[0].data.str.len;
10362
10363 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10364 occ = args[1].data.uint;
10365 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010366
Willy Tarreaue333ec92012-04-16 16:26:40 +020010367 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010368
Willy Tarreau185b5c42012-04-26 15:11:51 +020010369 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010370 /* search for header from the beginning */
10371 ctx->idx = 0;
10372
Willy Tarreau185b5c42012-04-26 15:11:51 +020010373 if (!occ && !(opt & SMP_OPT_ITERATE))
10374 /* no explicit occurrence and single fetch => last header by default */
10375 occ = -1;
10376
10377 if (!occ)
10378 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010379 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010380
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010381 smp->type = SMP_T_STR;
10382 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010383 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 +020010384 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010385
Willy Tarreau37406352012-04-23 16:16:37 +020010386 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010387 return 0;
10388}
10389
Willy Tarreauc11416f2007-06-17 16:58:38 +020010390/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010391 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010392 */
10393static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010394smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010395 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010396{
10397 struct http_txn *txn = l7;
10398 struct hdr_idx *idx = &txn->hdr_idx;
10399 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010400 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010401 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010402
Willy Tarreau24e32d82012-04-23 23:55:44 +020010403 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010404 return 0;
10405
Willy Tarreaue333ec92012-04-16 16:26:40 +020010406 CHECK_HTTP_MESSAGE_FIRST();
10407
Willy Tarreau33a7e692007-06-10 19:45:56 +020010408 ctx.idx = 0;
10409 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010410 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010411 cnt++;
10412
Willy Tarreauf853c462012-04-23 18:53:56 +020010413 smp->type = SMP_T_UINT;
10414 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010415 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010416 return 1;
10417}
10418
Willy Tarreau185b5c42012-04-26 15:11:51 +020010419/* Fetch an HTTP header's integer value. The integer value is returned. It
10420 * takes a mandatory argument of type string and an optional one of type int
10421 * to designate a specific occurrence. It returns an unsigned integer, which
10422 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010423 */
10424static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010425smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010426 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010427{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010428 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010429
Willy Tarreauf853c462012-04-23 18:53:56 +020010430 if (ret > 0) {
10431 smp->type = SMP_T_UINT;
10432 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10433 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010434
Willy Tarreaud53e2422012-04-16 17:21:11 +020010435 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010436}
10437
Cyril Bonté69fa9922012-10-25 00:01:06 +020010438/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10439 * and an optional one of type int to designate a specific occurrence.
10440 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010441 */
10442static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010443smp_fetch_hdr_ip(struct proxy *px, struct session *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 Tarreau106f9792009-09-19 07:54:16 +020010445{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010446 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010447
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010448 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010449 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10450 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010451 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010452 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010453 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010454 if (smp->data.str.len < temp->size - 1) {
10455 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10456 temp->str[smp->data.str.len] = '\0';
10457 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10458 smp->type = SMP_T_IPV6;
10459 break;
10460 }
10461 }
10462 }
10463
Willy Tarreaud53e2422012-04-16 17:21:11 +020010464 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010465 if (!(smp->flags & SMP_F_NOT_LAST))
10466 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010467 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010468 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010469}
10470
Willy Tarreau737b0c12007-06-10 21:28:46 +020010471/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10472 * the first '/' after the possible hostname, and ends before the possible '?'.
10473 */
10474static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010475smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010476 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010477{
10478 struct http_txn *txn = l7;
10479 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010480
Willy Tarreauc0239e02012-04-16 14:42:55 +020010481 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010482
Willy Tarreau9b28e032012-10-12 23:49:43 +020010483 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010484 ptr = http_get_path(txn);
10485 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010486 return 0;
10487
10488 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010489 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010490 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010491
10492 while (ptr < end && *ptr != '?')
10493 ptr++;
10494
Willy Tarreauf853c462012-04-23 18:53:56 +020010495 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010496 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010497 return 1;
10498}
10499
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010500/* This produces a concatenation of the first occurrence of the Host header
10501 * followed by the path component if it begins with a slash ('/'). This means
10502 * that '*' will not be added, resulting in exactly the first Host entry.
10503 * If no Host header is found, then the path is returned as-is. The returned
10504 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010505 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010506 */
10507static int
10508smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010509 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010510{
10511 struct http_txn *txn = l7;
10512 char *ptr, *end, *beg;
10513 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010514 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010515
10516 CHECK_HTTP_MESSAGE_FIRST();
10517
10518 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010519 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010520 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010521
10522 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010523 temp = get_trash_chunk();
10524 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010525 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010526 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010527 smp->data.str.len = ctx.vlen;
10528
10529 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010530 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010531 beg = http_get_path(txn);
10532 if (!beg)
10533 beg = end;
10534
10535 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10536
10537 if (beg < ptr && *beg == '/') {
10538 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10539 smp->data.str.len += ptr - beg;
10540 }
10541
10542 smp->flags = SMP_F_VOL_1ST;
10543 return 1;
10544}
10545
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010546/* This produces a 32-bit hash of the concatenation of the first occurrence of
10547 * the Host header followed by the path component if it begins with a slash ('/').
10548 * This means that '*' will not be added, resulting in exactly the first Host
10549 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010550 * is hashed using the path hash followed by a full avalanche hash and provides a
10551 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010552 * high-traffic sites without having to store whole paths.
10553 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010554int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010555smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010556 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010557{
10558 struct http_txn *txn = l7;
10559 struct hdr_ctx ctx;
10560 unsigned int hash = 0;
10561 char *ptr, *beg, *end;
10562 int len;
10563
10564 CHECK_HTTP_MESSAGE_FIRST();
10565
10566 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010567 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010568 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10569 ptr = ctx.line + ctx.val;
10570 len = ctx.vlen;
10571 while (len--)
10572 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10573 }
10574
10575 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010576 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010577 beg = http_get_path(txn);
10578 if (!beg)
10579 beg = end;
10580
10581 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10582
10583 if (beg < ptr && *beg == '/') {
10584 while (beg < ptr)
10585 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10586 }
10587 hash = full_hash(hash);
10588
10589 smp->type = SMP_T_UINT;
10590 smp->data.uint = hash;
10591 smp->flags = SMP_F_VOL_1ST;
10592 return 1;
10593}
10594
Willy Tarreau4a550602012-12-09 14:53:32 +010010595/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010596 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10597 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10598 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010599 * that in environments where IPv6 is insignificant, truncating the output to
10600 * 8 bytes would still work.
10601 */
10602static int
10603smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010604 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010605{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010606 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010607 struct connection *cli_conn = objt_conn(l4->si[0].end);
10608
10609 if (!cli_conn)
10610 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010611
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010612 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010613 return 0;
10614
Willy Tarreau47ca5452012-12-23 20:22:19 +010010615 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010616 *(unsigned int *)temp->str = htonl(smp->data.uint);
10617 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010618
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010619 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010620 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010621 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010622 temp->len += 4;
10623 break;
10624 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010625 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010626 temp->len += 16;
10627 break;
10628 default:
10629 return 0;
10630 }
10631
10632 smp->data.str = *temp;
10633 smp->type = SMP_T_BIN;
10634 return 1;
10635}
10636
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010637/* Extracts the query string, which comes after the question mark '?'. If no
10638 * question mark is found, nothing is returned. Otherwise it returns a sample
10639 * of type string carrying the whole query string.
10640 */
10641static int
10642smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010643 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010644{
10645 struct http_txn *txn = l7;
10646 char *ptr, *end;
10647
10648 CHECK_HTTP_MESSAGE_FIRST();
10649
10650 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10651 end = ptr + txn->req.sl.rq.u_l;
10652
10653 /* look up the '?' */
10654 do {
10655 if (ptr == end)
10656 return 0;
10657 } while (*ptr++ != '?');
10658
10659 smp->type = SMP_T_STR;
10660 smp->data.str.str = ptr;
10661 smp->data.str.len = end - ptr;
10662 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10663 return 1;
10664}
10665
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010666static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010667smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010668 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010669{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010670 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10671 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10672 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010673
Willy Tarreau24e32d82012-04-23 23:55:44 +020010674 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010675
Willy Tarreauf853c462012-04-23 18:53:56 +020010676 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010677 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010678 return 1;
10679}
10680
Willy Tarreau7f18e522010-10-22 20:04:13 +020010681/* return a valid test if the current request is the first one on the connection */
10682static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010683smp_fetch_http_first_req(struct proxy *px, struct session *s, 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 Tarreau7f18e522010-10-22 20:04:13 +020010685{
10686 if (!s)
10687 return 0;
10688
Willy Tarreauf853c462012-04-23 18:53:56 +020010689 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010690 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010691 return 1;
10692}
10693
Willy Tarreau34db1082012-04-19 17:16:54 +020010694/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010695static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010696smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010697 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010698{
10699
Willy Tarreau24e32d82012-04-23 23:55:44 +020010700 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010701 return 0;
10702
Willy Tarreauc0239e02012-04-16 14:42:55 +020010703 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010704
Willy Tarreauc0239e02012-04-16 14:42:55 +020010705 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010706 return 0;
10707
Willy Tarreauf853c462012-04-23 18:53:56 +020010708 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010709 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010710 return 1;
10711}
Willy Tarreau8797c062007-05-07 00:55:35 +020010712
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010713/* Accepts exactly 1 argument of type userlist */
10714static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010715smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010716 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010717{
10718
10719 if (!args || args->type != ARGT_USR)
10720 return 0;
10721
10722 CHECK_HTTP_MESSAGE_FIRST();
10723
10724 if (!get_http_auth(l4))
10725 return 0;
10726
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010727 /* if the user does not belong to the userlist or has a wrong password,
10728 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010729 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010730 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010731 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10732 return 0;
10733
10734 /* pat_match_auth() will need the user list */
10735 smp->ctx.a[0] = args->data.usr;
10736
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010737 smp->type = SMP_T_STR;
10738 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010739 smp->data.str.str = l4->txn.auth.user;
10740 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010741
10742 return 1;
10743}
10744
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010745/* Try to find the next occurrence of a cookie name in a cookie header value.
10746 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10747 * the cookie value is returned into *value and *value_l, and the function
10748 * returns a pointer to the next pointer to search from if the value was found.
10749 * Otherwise if the cookie was not found, NULL is returned and neither value
10750 * nor value_l are touched. The input <hdr> string should first point to the
10751 * header's value, and the <hdr_end> pointer must point to the first character
10752 * not part of the value. <list> must be non-zero if value may represent a list
10753 * of values (cookie headers). This makes it faster to abort parsing when no
10754 * list is expected.
10755 */
10756static char *
10757extract_cookie_value(char *hdr, const char *hdr_end,
10758 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010759 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010760{
10761 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10762 char *next;
10763
10764 /* we search at least a cookie name followed by an equal, and more
10765 * generally something like this :
10766 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10767 */
10768 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10769 /* Iterate through all cookies on this line */
10770
10771 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10772 att_beg++;
10773
10774 /* find att_end : this is the first character after the last non
10775 * space before the equal. It may be equal to hdr_end.
10776 */
10777 equal = att_end = att_beg;
10778
10779 while (equal < hdr_end) {
10780 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10781 break;
10782 if (http_is_spht[(unsigned char)*equal++])
10783 continue;
10784 att_end = equal;
10785 }
10786
10787 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10788 * is between <att_beg> and <equal>, both may be identical.
10789 */
10790
10791 /* look for end of cookie if there is an equal sign */
10792 if (equal < hdr_end && *equal == '=') {
10793 /* look for the beginning of the value */
10794 val_beg = equal + 1;
10795 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10796 val_beg++;
10797
10798 /* find the end of the value, respecting quotes */
10799 next = find_cookie_value_end(val_beg, hdr_end);
10800
10801 /* make val_end point to the first white space or delimitor after the value */
10802 val_end = next;
10803 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10804 val_end--;
10805 } else {
10806 val_beg = val_end = next = equal;
10807 }
10808
10809 /* We have nothing to do with attributes beginning with '$'. However,
10810 * they will automatically be removed if a header before them is removed,
10811 * since they're supposed to be linked together.
10812 */
10813 if (*att_beg == '$')
10814 continue;
10815
10816 /* Ignore cookies with no equal sign */
10817 if (equal == next)
10818 continue;
10819
10820 /* Now we have the cookie name between att_beg and att_end, and
10821 * its value between val_beg and val_end.
10822 */
10823
10824 if (att_end - att_beg == cookie_name_l &&
10825 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10826 /* let's return this value and indicate where to go on from */
10827 *value = val_beg;
10828 *value_l = val_end - val_beg;
10829 return next + 1;
10830 }
10831
10832 /* Set-Cookie headers only have the name in the first attr=value part */
10833 if (!list)
10834 break;
10835 }
10836
10837 return NULL;
10838}
10839
William Lallemanda43ba4e2014-01-28 18:14:25 +010010840/* Fetch a captured HTTP request header. The index is the position of
10841 * the "capture" option in the configuration file
10842 */
10843static int
10844smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010845 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010846{
10847 struct proxy *fe = l4->fe;
10848 struct http_txn *txn = l7;
10849 int idx;
10850
10851 if (!args || args->type != ARGT_UINT)
10852 return 0;
10853
10854 idx = args->data.uint;
10855
10856 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10857 return 0;
10858
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010859 smp->type = SMP_T_STR;
10860 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010861 smp->data.str.str = txn->req.cap[idx];
10862 smp->data.str.len = strlen(txn->req.cap[idx]);
10863
10864 return 1;
10865}
10866
10867/* Fetch a captured HTTP response header. The index is the position of
10868 * the "capture" option in the configuration file
10869 */
10870static int
10871smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010872 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010873{
10874 struct proxy *fe = l4->fe;
10875 struct http_txn *txn = l7;
10876 int idx;
10877
10878 if (!args || args->type != ARGT_UINT)
10879 return 0;
10880
10881 idx = args->data.uint;
10882
10883 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10884 return 0;
10885
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010886 smp->type = SMP_T_STR;
10887 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010888 smp->data.str.str = txn->rsp.cap[idx];
10889 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10890
10891 return 1;
10892}
10893
William Lallemand65ad6e12014-01-31 15:08:02 +010010894/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10895static int
10896smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010897 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010898{
10899 struct chunk *temp;
10900 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010901 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010902
10903 if (!txn->uri)
10904 return 0;
10905
William Lallemand96a77852014-02-05 00:30:02 +010010906 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010907
William Lallemand96a77852014-02-05 00:30:02 +010010908 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10909 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010910
William Lallemand96a77852014-02-05 00:30:02 +010010911 temp = get_trash_chunk();
10912 temp->str = txn->uri;
10913 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010914 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010915 smp->type = SMP_T_STR;
10916 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010917
10918 return 1;
10919
10920}
10921
10922/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10923static int
10924smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010925 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010926{
10927 struct chunk *temp;
10928 struct http_txn *txn = l7;
10929 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010930
10931 if (!txn->uri)
10932 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010933
William Lallemand65ad6e12014-01-31 15:08:02 +010010934 ptr = txn->uri;
10935
10936 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10937 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010938
William Lallemand65ad6e12014-01-31 15:08:02 +010010939 if (!*ptr)
10940 return 0;
10941
10942 ptr++; /* skip the space */
10943
10944 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010945 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010946 if (!ptr)
10947 return 0;
10948 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10949 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010950
10951 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010952 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010953 smp->type = SMP_T_STR;
10954 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010955
10956 return 1;
10957}
10958
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010959/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10960 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10961 */
10962static int
10963smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010964 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010965{
10966 struct http_txn *txn = l7;
10967
10968 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10969 return 0;
10970
10971 if (txn->req.flags & HTTP_MSGF_VER_11)
10972 smp->data.str.str = "HTTP/1.1";
10973 else
10974 smp->data.str.str = "HTTP/1.0";
10975
10976 smp->data.str.len = 8;
10977 smp->type = SMP_T_STR;
10978 smp->flags = SMP_F_CONST;
10979 return 1;
10980
10981}
10982
10983/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10984 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10985 */
10986static int
10987smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010988 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010989{
10990 struct http_txn *txn = l7;
10991
10992 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10993 return 0;
10994
10995 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10996 smp->data.str.str = "HTTP/1.1";
10997 else
10998 smp->data.str.str = "HTTP/1.0";
10999
11000 smp->data.str.len = 8;
11001 smp->type = SMP_T_STR;
11002 smp->flags = SMP_F_CONST;
11003 return 1;
11004
11005}
11006
William Lallemand65ad6e12014-01-31 15:08:02 +010011007
Willy Tarreaue333ec92012-04-16 16:26:40 +020011008/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011009 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011010 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011011 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011012 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011013 * Accepts exactly 1 argument of type string. If the input options indicate
11014 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011015 * The returned sample is of type CSTR. Can be used to parse cookies in other
11016 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011017 */
William Lallemand07c8b242014-05-02 17:11:07 +020011018int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011019 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011020{
11021 struct http_txn *txn = l7;
11022 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011023 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011024 const struct http_msg *msg;
11025 const char *hdr_name;
11026 int hdr_name_len;
11027 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011028 int occ = 0;
11029 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011030
Willy Tarreau24e32d82012-04-23 23:55:44 +020011031 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011032 return 0;
11033
Willy Tarreaua890d072013-04-02 12:01:06 +020011034 if (!ctx) {
11035 /* first call */
11036 ctx = &static_hdr_ctx;
11037 ctx->idx = 0;
11038 smp->ctx.a[2] = ctx;
11039 }
11040
Willy Tarreaue333ec92012-04-16 16:26:40 +020011041 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011042
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011043 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011044 msg = &txn->req;
11045 hdr_name = "Cookie";
11046 hdr_name_len = 6;
11047 } else {
11048 msg = &txn->rsp;
11049 hdr_name = "Set-Cookie";
11050 hdr_name_len = 10;
11051 }
11052
Willy Tarreau28376d62012-04-26 21:26:10 +020011053 if (!occ && !(opt & SMP_OPT_ITERATE))
11054 /* no explicit occurrence and single fetch => last cookie by default */
11055 occ = -1;
11056
11057 /* OK so basically here, either we want only one value and it's the
11058 * last one, or we want to iterate over all of them and we fetch the
11059 * next one.
11060 */
11061
Willy Tarreau9b28e032012-10-12 23:49:43 +020011062 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011063 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011064 /* search for the header from the beginning, we must first initialize
11065 * the search parameters.
11066 */
Willy Tarreau37406352012-04-23 16:16:37 +020011067 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011068 ctx->idx = 0;
11069 }
11070
Willy Tarreau28376d62012-04-26 21:26:10 +020011071 smp->flags |= SMP_F_VOL_HDR;
11072
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011073 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011074 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11075 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011076 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11077 goto out;
11078
Willy Tarreau24e32d82012-04-23 23:55:44 +020011079 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011080 continue;
11081
Willy Tarreau37406352012-04-23 16:16:37 +020011082 smp->ctx.a[0] = ctx->line + ctx->val;
11083 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011084 }
11085
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011086 smp->type = SMP_T_STR;
11087 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011088 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011089 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011090 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011091 &smp->data.str.str,
11092 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011093 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011094 found = 1;
11095 if (occ >= 0) {
11096 /* one value was returned into smp->data.str.{str,len} */
11097 smp->flags |= SMP_F_NOT_LAST;
11098 return 1;
11099 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011100 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011101 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011102 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011103 /* all cookie headers and values were scanned. If we're looking for the
11104 * last occurrence, we may return it now.
11105 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011106 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011107 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011108 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011109}
11110
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011112 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011113 * multiple cookies may be parsed on the same line. The returned sample is of
11114 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115 */
11116static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010011117smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011118 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011119{
11120 struct http_txn *txn = l7;
11121 struct hdr_idx *idx = &txn->hdr_idx;
11122 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011123 const struct http_msg *msg;
11124 const char *hdr_name;
11125 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011126 int cnt;
11127 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011128 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011129
Willy Tarreau24e32d82012-04-23 23:55:44 +020011130 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011131 return 0;
11132
Willy Tarreaue333ec92012-04-16 16:26:40 +020011133 CHECK_HTTP_MESSAGE_FIRST();
11134
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011135 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011136 msg = &txn->req;
11137 hdr_name = "Cookie";
11138 hdr_name_len = 6;
11139 } else {
11140 msg = &txn->rsp;
11141 hdr_name = "Set-Cookie";
11142 hdr_name_len = 10;
11143 }
11144
Willy Tarreau9b28e032012-10-12 23:49:43 +020011145 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011146 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011147 ctx.idx = 0;
11148 cnt = 0;
11149
11150 while (1) {
11151 /* Note: val_beg == NULL every time we need to fetch a new header */
11152 if (!val_beg) {
11153 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11154 break;
11155
Willy Tarreau24e32d82012-04-23 23:55:44 +020011156 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011157 continue;
11158
11159 val_beg = ctx.line + ctx.val;
11160 val_end = val_beg + ctx.vlen;
11161 }
11162
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011163 smp->type = SMP_T_STR;
11164 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011165 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011166 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011167 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011168 &smp->data.str.str,
11169 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011170 cnt++;
11171 }
11172 }
11173
Willy Tarreaub169eba2013-12-16 15:14:43 +010011174 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011175 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011176 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011177 return 1;
11178}
11179
Willy Tarreau51539362012-05-08 12:46:28 +020011180/* Fetch an cookie's integer value. The integer value is returned. It
11181 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11182 */
11183static int
11184smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011185 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011186{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011187 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011188
11189 if (ret > 0) {
11190 smp->type = SMP_T_UINT;
11191 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11192 }
11193
11194 return ret;
11195}
11196
Willy Tarreau8797c062007-05-07 00:55:35 +020011197/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011198/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011199/************************************************************************/
11200
David Cournapeau16023ee2010-12-23 20:55:41 +090011201/*
11202 * Given a path string and its length, find the position of beginning of the
11203 * query string. Returns NULL if no query string is found in the path.
11204 *
11205 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11206 *
11207 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11208 */
bedis4c75cca2012-10-05 08:38:24 +020011209static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011210{
11211 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011212
bedis4c75cca2012-10-05 08:38:24 +020011213 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011214 return p ? p + 1 : NULL;
11215}
11216
bedis4c75cca2012-10-05 08:38:24 +020011217static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011218{
bedis4c75cca2012-10-05 08:38:24 +020011219 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011220}
11221
11222/*
11223 * Given a url parameter, find the starting position of the first occurence,
11224 * or NULL if the parameter is not found.
11225 *
11226 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11227 * the function will return query_string+8.
11228 */
11229static char*
11230find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011231 char* url_param_name, size_t url_param_name_l,
11232 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011233{
11234 char *pos, *last;
11235
11236 pos = query_string;
11237 last = query_string + query_string_l - url_param_name_l - 1;
11238
11239 while (pos <= last) {
11240 if (pos[url_param_name_l] == '=') {
11241 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11242 return pos;
11243 pos += url_param_name_l + 1;
11244 }
bedis4c75cca2012-10-05 08:38:24 +020011245 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011246 pos++;
11247 pos++;
11248 }
11249 return NULL;
11250}
11251
11252/*
11253 * Given a url parameter name, returns its value and size into *value and
11254 * *value_l respectively, and returns non-zero. If the parameter is not found,
11255 * zero is returned and value/value_l are not touched.
11256 */
11257static int
11258find_url_param_value(char* path, size_t path_l,
11259 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011260 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011261{
11262 char *query_string, *qs_end;
11263 char *arg_start;
11264 char *value_start, *value_end;
11265
bedis4c75cca2012-10-05 08:38:24 +020011266 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011267 if (!query_string)
11268 return 0;
11269
11270 qs_end = path + path_l;
11271 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011272 url_param_name, url_param_name_l,
11273 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011274 if (!arg_start)
11275 return 0;
11276
11277 value_start = arg_start + url_param_name_l + 1;
11278 value_end = value_start;
11279
bedis4c75cca2012-10-05 08:38:24 +020011280 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011281 value_end++;
11282
11283 *value = value_start;
11284 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011285 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011286}
11287
11288static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011289smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011290 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011291{
bedis4c75cca2012-10-05 08:38:24 +020011292 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011293 struct http_txn *txn = l7;
11294 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011295
bedis4c75cca2012-10-05 08:38:24 +020011296 if (!args || args[0].type != ARGT_STR ||
11297 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011298 return 0;
11299
11300 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011301
bedis4c75cca2012-10-05 08:38:24 +020011302 if (args[1].type)
11303 delim = *args[1].data.str.str;
11304
Willy Tarreau9b28e032012-10-12 23:49:43 +020011305 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011306 args->data.str.str, args->data.str.len,
11307 &smp->data.str.str, &smp->data.str.len,
11308 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011309 return 0;
11310
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011311 smp->type = SMP_T_STR;
11312 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011313 return 1;
11314}
11315
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011316/* Return the signed integer value for the specified url parameter (see url_param
11317 * above).
11318 */
11319static int
11320smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011321 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011322{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011323 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011324
11325 if (ret > 0) {
11326 smp->type = SMP_T_UINT;
11327 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11328 }
11329
11330 return ret;
11331}
11332
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011333/* This produces a 32-bit hash of the concatenation of the first occurrence of
11334 * the Host header followed by the path component if it begins with a slash ('/').
11335 * This means that '*' will not be added, resulting in exactly the first Host
11336 * entry. If no Host header is found, then the path is used. The resulting value
11337 * is hashed using the url hash followed by a full avalanche hash and provides a
11338 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11339 * high-traffic sites without having to store whole paths.
11340 * this differs from the base32 functions in that it includes the url parameters
11341 * as well as the path
11342 */
11343static int
11344smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011345 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011346{
11347 struct http_txn *txn = l7;
11348 struct hdr_ctx ctx;
11349 unsigned int hash = 0;
11350 char *ptr, *beg, *end;
11351 int len;
11352
11353 CHECK_HTTP_MESSAGE_FIRST();
11354
11355 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011356 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011357 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11358 ptr = ctx.line + ctx.val;
11359 len = ctx.vlen;
11360 while (len--)
11361 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11362 }
11363
11364 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011365 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 +000011366 beg = http_get_path(txn);
11367 if (!beg)
11368 beg = end;
11369
11370 for (ptr = beg; ptr < end ; ptr++);
11371
11372 if (beg < ptr && *beg == '/') {
11373 while (beg < ptr)
11374 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11375 }
11376 hash = full_hash(hash);
11377
11378 smp->type = SMP_T_UINT;
11379 smp->data.uint = hash;
11380 smp->flags = SMP_F_VOL_1ST;
11381 return 1;
11382}
11383
11384/* This concatenates the source address with the 32-bit hash of the Host and
11385 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11386 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11387 * on the source address length. The URL hash is stored before the address so
11388 * that in environments where IPv6 is insignificant, truncating the output to
11389 * 8 bytes would still work.
11390 */
11391static int
11392smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011393 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011394{
11395 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011396 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011397
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011398 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011399 return 0;
11400
11401 temp = get_trash_chunk();
11402 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11403 temp->len += sizeof(smp->data.uint);
11404
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011405 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011406 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011407 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011408 temp->len += 4;
11409 break;
11410 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011411 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011412 temp->len += 16;
11413 break;
11414 default:
11415 return 0;
11416 }
11417
11418 smp->data.str = *temp;
11419 smp->type = SMP_T_BIN;
11420 return 1;
11421}
11422
Willy Tarreau185b5c42012-04-26 15:11:51 +020011423/* This function is used to validate the arguments passed to any "hdr" fetch
11424 * keyword. These keywords support an optional positive or negative occurrence
11425 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11426 * is assumed that the types are already the correct ones. Returns 0 on error,
11427 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11428 * error message in case of error, that the caller is responsible for freeing.
11429 * The initial location must either be freeable or NULL.
11430 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011431int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011432{
11433 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011434 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011435 return 0;
11436 }
11437 return 1;
11438}
11439
Willy Tarreau276fae92013-07-25 14:36:01 +020011440/* takes an UINT value on input supposed to represent the time since EPOCH,
11441 * adds an optional offset found in args[0] and emits a string representing
11442 * the date in RFC-1123/5322 format.
11443 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011444static int sample_conv_http_date(struct session *session, const struct arg *args,
11445 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011446{
11447 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11448 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11449 struct chunk *temp;
11450 struct tm *tm;
11451 time_t curr_date = smp->data.uint;
11452
11453 /* add offset */
11454 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11455 curr_date += args[0].data.sint;
11456
11457 tm = gmtime(&curr_date);
11458
11459 temp = get_trash_chunk();
11460 temp->len = snprintf(temp->str, temp->size - temp->len,
11461 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11462 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11463 tm->tm_hour, tm->tm_min, tm->tm_sec);
11464
11465 smp->data.str = *temp;
11466 smp->type = SMP_T_STR;
11467 return 1;
11468}
11469
Thierry FOURNIERad903512014-04-11 17:51:01 +020011470/* Match language range with language tag. RFC2616 14.4:
11471 *
11472 * A language-range matches a language-tag if it exactly equals
11473 * the tag, or if it exactly equals a prefix of the tag such
11474 * that the first tag character following the prefix is "-".
11475 *
11476 * Return 1 if the strings match, else return 0.
11477 */
11478static inline int language_range_match(const char *range, int range_len,
11479 const char *tag, int tag_len)
11480{
11481 const char *end = range + range_len;
11482 const char *tend = tag + tag_len;
11483 while (range < end) {
11484 if (*range == '-' && tag == tend)
11485 return 1;
11486 if (*range != *tag || tag == tend)
11487 return 0;
11488 range++;
11489 tag++;
11490 }
11491 /* Return true only if the last char of the tag is matched. */
11492 return tag == tend;
11493}
11494
11495/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011496static int sample_conv_q_prefered(struct session *session, const struct arg *args,
11497 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011498{
11499 const char *al = smp->data.str.str;
11500 const char *end = al + smp->data.str.len;
11501 const char *token;
11502 int toklen;
11503 int qvalue;
11504 const char *str;
11505 const char *w;
11506 int best_q = 0;
11507
11508 /* Set the constant to the sample, because the output of the
11509 * function will be peek in the constant configuration string.
11510 */
11511 smp->flags |= SMP_F_CONST;
11512 smp->data.str.size = 0;
11513 smp->data.str.str = "";
11514 smp->data.str.len = 0;
11515
11516 /* Parse the accept language */
11517 while (1) {
11518
11519 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011520 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011521 al++;
11522 if (al >= end)
11523 break;
11524
11525 /* Start of the fisrt word. */
11526 token = al;
11527
11528 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011529 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011530 al++;
11531 if (al == token)
11532 goto expect_comma;
11533
11534 /* Length of the token. */
11535 toklen = al - token;
11536 qvalue = 1000;
11537
11538 /* Check if the token exists in the list. If the token not exists,
11539 * jump to the next token.
11540 */
11541 str = args[0].data.str.str;
11542 w = str;
11543 while (1) {
11544 if (*str == ';' || *str == '\0') {
11545 if (language_range_match(token, toklen, w, str-w))
11546 goto look_for_q;
11547 if (*str == '\0')
11548 goto expect_comma;
11549 w = str + 1;
11550 }
11551 str++;
11552 }
11553 goto expect_comma;
11554
11555look_for_q:
11556
11557 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011558 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011559 al++;
11560 if (al >= end)
11561 goto process_value;
11562
11563 /* If ',' is found, process the result */
11564 if (*al == ',')
11565 goto process_value;
11566
11567 /* If the character is different from ';', look
11568 * for the end of the header part in best effort.
11569 */
11570 if (*al != ';')
11571 goto expect_comma;
11572
11573 /* Assumes that the char is ';', now expect "q=". */
11574 al++;
11575
11576 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011577 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011578 al++;
11579 if (al >= end)
11580 goto process_value;
11581
11582 /* Expect 'q'. If no 'q', continue in best effort */
11583 if (*al != 'q')
11584 goto process_value;
11585 al++;
11586
11587 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011588 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011589 al++;
11590 if (al >= end)
11591 goto process_value;
11592
11593 /* Expect '='. If no '=', continue in best effort */
11594 if (*al != '=')
11595 goto process_value;
11596 al++;
11597
11598 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011599 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011600 al++;
11601 if (al >= end)
11602 goto process_value;
11603
11604 /* Parse the q value. */
11605 qvalue = parse_qvalue(al, &al);
11606
11607process_value:
11608
11609 /* If the new q value is the best q value, then store the associated
11610 * language in the response. If qvalue is the biggest value (1000),
11611 * break the process.
11612 */
11613 if (qvalue > best_q) {
11614 smp->data.str.str = (char *)w;
11615 smp->data.str.len = str - w;
11616 if (qvalue >= 1000)
11617 break;
11618 best_q = qvalue;
11619 }
11620
11621expect_comma:
11622
11623 /* Expect comma or end. If the end is detected, quit the loop. */
11624 while (al < end && *al != ',')
11625 al++;
11626 if (al >= end)
11627 break;
11628
11629 /* Comma is found, jump it and restart the analyzer. */
11630 al++;
11631 }
11632
11633 /* Set default value if required. */
11634 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11635 smp->data.str.str = args[1].data.str.str;
11636 smp->data.str.len = args[1].data.str.len;
11637 }
11638
11639 /* Return true only if a matching language was found. */
11640 return smp->data.str.len != 0;
11641}
11642
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011643/* This function executes one of the set-{method,path,query,uri} actions. It
11644 * builds a string in the trash from the specified format string, then modifies
11645 * the relevant part of the request line accordingly. Then it updates various
11646 * pointers to the next elements which were moved, and the total buffer length.
11647 * It finds the action to be performed in p[2], previously filled by function
11648 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11649 * error, though this can be revisited when this code is finally exploited.
11650 */
11651int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn)
11652{
11653 char *cur_ptr, *cur_end;
11654 int offset;
11655 int delta;
11656
11657 chunk_reset(&trash);
11658
11659 /* prepare a '?' just in case we have to create a query string */
11660 trash.str[trash.len++] = '?';
11661 offset = 1;
11662 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11663
11664 switch (*(int *)&rule->arg.act.p[2]) {
11665 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011666 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011667 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11668
11669 /* adjust req line offsets and lengths */
11670 delta = trash.len - offset - (cur_end - cur_ptr);
11671 txn->req.sl.rq.m_l += delta;
11672 txn->req.sl.rq.u += delta;
11673 txn->req.sl.rq.v += delta;
11674 break;
11675
11676 case 1: // path
11677 cur_ptr = http_get_path(txn);
11678 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011679 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011680
11681 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011682 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 +010011683 cur_end++;
11684
11685 /* adjust req line offsets and lengths */
11686 delta = trash.len - offset - (cur_end - cur_ptr);
11687 txn->req.sl.rq.u_l += delta;
11688 txn->req.sl.rq.v += delta;
11689 break;
11690
11691 case 2: // query
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011692 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011693 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11694 while (cur_ptr < cur_end && *cur_ptr != '?')
11695 cur_ptr++;
11696
11697 /* skip the question mark or indicate that we must insert it
11698 * (but only if the format string is not empty then).
11699 */
11700 if (cur_ptr < cur_end)
11701 cur_ptr++;
11702 else if (trash.len > 1)
11703 offset = 0;
11704
11705 /* adjust req line offsets and lengths */
11706 delta = trash.len - offset - (cur_end - cur_ptr);
11707 txn->req.sl.rq.u_l += delta;
11708 txn->req.sl.rq.v += delta;
11709 break;
11710
11711 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011712 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011713 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11714
11715 /* adjust req line offsets and lengths */
11716 delta = trash.len - offset - (cur_end - cur_ptr);
11717 txn->req.sl.rq.u_l += delta;
11718 txn->req.sl.rq.v += delta;
11719 break;
11720
11721 default:
11722 return -1;
11723 }
11724
11725 /* commit changes and adjust end of message */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011726 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011727 http_msg_move_end(&txn->req, delta);
11728 return 0;
11729}
11730
11731/* parse an http-request action among :
11732 * set-method
11733 * set-path
11734 * set-query
11735 * set-uri
11736 *
11737 * All of them accept a single argument of type string representing a log-format.
11738 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11739 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11740 * It returns 0 on success, < 0 on error.
11741 */
11742int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11743{
11744 int cur_arg = *orig_arg;
11745
11746 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11747
11748 switch (args[0][4]) {
11749 case 'm' :
11750 *(int *)&rule->arg.act.p[2] = 0;
11751 rule->action_ptr = http_action_set_req_line;
11752 break;
11753 case 'p' :
11754 *(int *)&rule->arg.act.p[2] = 1;
11755 rule->action_ptr = http_action_set_req_line;
11756 break;
11757 case 'q' :
11758 *(int *)&rule->arg.act.p[2] = 2;
11759 rule->action_ptr = http_action_set_req_line;
11760 break;
11761 case 'u' :
11762 *(int *)&rule->arg.act.p[2] = 3;
11763 rule->action_ptr = http_action_set_req_line;
11764 break;
11765 default:
11766 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11767 return -1;
11768 }
11769
11770 if (!*args[cur_arg] ||
11771 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11772 memprintf(err, "expects exactly 1 argument <format>");
11773 return -1;
11774 }
11775
11776 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11777 proxy->conf.args.ctx = ARGC_HRQ;
11778 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11779 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11780 proxy->conf.args.file, proxy->conf.args.line);
11781
11782 (*orig_arg)++;
11783 return 0;
11784}
11785
William Lallemand73025dd2014-04-24 14:38:37 +020011786/*
11787 * Return the struct http_req_action_kw associated to a keyword.
11788 */
11789struct http_req_action_kw *action_http_req_custom(const char *kw)
11790{
11791 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11792 struct http_req_action_kw_list *kw_list;
11793 int i;
11794
11795 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11796 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11797 if (!strcmp(kw, kw_list->kw[i].kw))
11798 return &kw_list->kw[i];
11799 }
11800 }
11801 }
11802 return NULL;
11803}
11804
11805/*
11806 * Return the struct http_res_action_kw associated to a keyword.
11807 */
11808struct http_res_action_kw *action_http_res_custom(const char *kw)
11809{
11810 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11811 struct http_res_action_kw_list *kw_list;
11812 int i;
11813
11814 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11815 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11816 if (!strcmp(kw, kw_list->kw[i].kw))
11817 return &kw_list->kw[i];
11818 }
11819 }
11820 }
11821 return NULL;
11822}
11823
Willy Tarreau4a568972010-05-12 08:08:50 +020011824/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011825/* All supported ACL keywords must be declared here. */
11826/************************************************************************/
11827
11828/* Note: must not be declared <const> as its list will be overwritten.
11829 * Please take care of keeping this list alphabetically sorted.
11830 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011831static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011832 { "base", "base", PAT_MATCH_STR },
11833 { "base_beg", "base", PAT_MATCH_BEG },
11834 { "base_dir", "base", PAT_MATCH_DIR },
11835 { "base_dom", "base", PAT_MATCH_DOM },
11836 { "base_end", "base", PAT_MATCH_END },
11837 { "base_len", "base", PAT_MATCH_LEN },
11838 { "base_reg", "base", PAT_MATCH_REG },
11839 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011840
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011841 { "cook", "req.cook", PAT_MATCH_STR },
11842 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11843 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11844 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11845 { "cook_end", "req.cook", PAT_MATCH_END },
11846 { "cook_len", "req.cook", PAT_MATCH_LEN },
11847 { "cook_reg", "req.cook", PAT_MATCH_REG },
11848 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011849
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011850 { "hdr", "req.hdr", PAT_MATCH_STR },
11851 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11852 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11853 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11854 { "hdr_end", "req.hdr", PAT_MATCH_END },
11855 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11856 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11857 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011858
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011859 /* these two declarations uses strings with list storage (in place
11860 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11861 * and delete functions are relative to the list management. The parse
11862 * and match method are related to the corresponding fetch methods. This
11863 * is very particular ACL declaration mode.
11864 */
11865 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11866 { "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 +020011867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011868 { "path", "path", PAT_MATCH_STR },
11869 { "path_beg", "path", PAT_MATCH_BEG },
11870 { "path_dir", "path", PAT_MATCH_DIR },
11871 { "path_dom", "path", PAT_MATCH_DOM },
11872 { "path_end", "path", PAT_MATCH_END },
11873 { "path_len", "path", PAT_MATCH_LEN },
11874 { "path_reg", "path", PAT_MATCH_REG },
11875 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011877 { "req_ver", "req.ver", PAT_MATCH_STR },
11878 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011880 { "scook", "res.cook", PAT_MATCH_STR },
11881 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11882 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11883 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11884 { "scook_end", "res.cook", PAT_MATCH_END },
11885 { "scook_len", "res.cook", PAT_MATCH_LEN },
11886 { "scook_reg", "res.cook", PAT_MATCH_REG },
11887 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011889 { "shdr", "res.hdr", PAT_MATCH_STR },
11890 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11891 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11892 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11893 { "shdr_end", "res.hdr", PAT_MATCH_END },
11894 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11895 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11896 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011897
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011898 { "url", "url", PAT_MATCH_STR },
11899 { "url_beg", "url", PAT_MATCH_BEG },
11900 { "url_dir", "url", PAT_MATCH_DIR },
11901 { "url_dom", "url", PAT_MATCH_DOM },
11902 { "url_end", "url", PAT_MATCH_END },
11903 { "url_len", "url", PAT_MATCH_LEN },
11904 { "url_reg", "url", PAT_MATCH_REG },
11905 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011907 { "urlp", "urlp", PAT_MATCH_STR },
11908 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11909 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11910 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11911 { "urlp_end", "urlp", PAT_MATCH_END },
11912 { "urlp_len", "urlp", PAT_MATCH_LEN },
11913 { "urlp_reg", "urlp", PAT_MATCH_REG },
11914 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011915
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011916 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011917}};
11918
11919/************************************************************************/
11920/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011921/************************************************************************/
11922/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011923static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011924 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011925 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11926 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11927
William Lallemanda43ba4e2014-01-28 18:14:25 +010011928 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011929 { "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 +020011930
11931 /* retrieve these captures from the HTTP logs */
11932 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11933 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11934 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11935
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011936 { "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 +020011937 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011938
Willy Tarreau409bcde2013-01-08 00:31:00 +010011939 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11940 * are only here to match the ACL's name, are request-only and are used
11941 * for ACL compatibility only.
11942 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011943 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11944 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011945 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11946 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11947
11948 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11949 * only here to match the ACL's name, are request-only and are used for
11950 * ACL compatibility only.
11951 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011952 { "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 +010011953 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11954 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11955 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11956
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011957 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011958 { "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 +010011959 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011960 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011961 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011962 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011963
11964 /* HTTP protocol on the request path */
11965 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011966 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011967
11968 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011969 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11970 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011971
11972 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011973 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11974 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011975
Willy Tarreau18ed2562013-01-14 15:56:36 +010011976 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011977 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011978 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11979 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11980
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011981 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011982 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011983 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011984 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11985 { "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 +010011986 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011987 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11988
11989 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011990 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011991 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11992 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11993
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011994 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011995 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011996 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011997 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11998 { "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 +010011999 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012000 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12001
Willy Tarreau409bcde2013-01-08 00:31:00 +010012002 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012003 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012004 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12005 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012006 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012007
12008 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012009 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012010 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12011 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12012 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12013
12014 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012015 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012016 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12017 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012018 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12019 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012020 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12021 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012022 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12023 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012024}};
12025
Willy Tarreau8797c062007-05-07 00:55:35 +020012026
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012027/************************************************************************/
12028/* All supported converter keywords must be declared here. */
12029/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012030/* Note: must not be declared <const> as its list will be overwritten */
12031static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012032 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12033 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012034 { NULL, NULL, 0, 0, 0 },
12035}};
12036
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012037/************************************************************************/
12038/* All supported http-request action keywords must be declared here. */
12039/************************************************************************/
12040struct http_req_action_kw_list http_req_actions = {
12041 .scope = "http",
12042 .kw = {
12043 { "set-method", parse_set_req_line },
12044 { "set-path", parse_set_req_line },
12045 { "set-query", parse_set_req_line },
12046 { "set-uri", parse_set_req_line },
12047 }
12048};
12049
Willy Tarreau8797c062007-05-07 00:55:35 +020012050__attribute__((constructor))
12051static void __http_protocol_init(void)
12052{
12053 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012054 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012055 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012056 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012057}
12058
12059
Willy Tarreau58f10d72006-12-04 02:26:12 +010012060/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012061 * Local variables:
12062 * c-indent-level: 8
12063 * c-basic-offset: 8
12064 * End:
12065 */