blob: f87583f71905517c2da21de5eb62bfe30a47fae5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreauc90dc232015-02-20 13:51:36 +0100590/* Find the first or next header field in message buffer <sol> using headers
591 * index <idx>, and return it in the <ctx> structure. This structure holds
592 * everything necessary to use the header and find next occurrence. If its
593 * <idx> member is 0, the first header is retrieved. Otherwise, the next
594 * occurrence is returned. The function returns 1 when it finds a value, and
595 * 0 when there is no more. It is equivalent to http_find_full_header2() with
596 * no header name.
597 */
598int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
599{
600 char *eol, *sov;
601 int cur_idx, old_idx;
602 int len;
603
604 cur_idx = ctx->idx;
605 if (cur_idx) {
606 /* We have previously returned a header, let's search another one */
607 sol = ctx->line;
608 eol = sol + idx->v[cur_idx].len;
609 goto next_hdr;
610 }
611
612 /* first request for this header */
613 sol += hdr_idx_first_pos(idx);
614 old_idx = 0;
615 cur_idx = hdr_idx_first_idx(idx);
616 while (cur_idx) {
617 eol = sol + idx->v[cur_idx].len;
618
619 len = 0;
620 while (1) {
621 if (len >= eol - sol)
622 goto next_hdr;
623 if (sol[len] == ':')
624 break;
625 len++;
626 }
627
628 ctx->del = len;
629 sov = sol + len + 1;
630 while (sov < eol && http_is_lws[(unsigned char)*sov])
631 sov++;
632
633 ctx->line = sol;
634 ctx->prev = old_idx;
635 ctx->idx = cur_idx;
636 ctx->val = sov - sol;
637 ctx->tws = 0;
638
639 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
640 eol--;
641 ctx->tws++;
642 }
643 ctx->vlen = eol - sov;
644 return 1;
645
646 next_hdr:
647 sol = eol + idx->v[cur_idx].cr + 1;
648 old_idx = cur_idx;
649 cur_idx = idx->v[cur_idx].next;
650 }
651 return 0;
652}
653
Willy Tarreau68085d82010-01-18 14:54:04 +0100654/* Find the end of the header value contained between <s> and <e>. See RFC2616,
655 * par 2.2 for more information. Note that it requires a valid header to return
656 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100658char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200659{
660 int quoted, qdpair;
661
662 quoted = qdpair = 0;
663 for (; s < e; s++) {
664 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200665 else if (quoted) {
666 if (*s == '\\') qdpair = 1;
667 else if (*s == '"') quoted = 0;
668 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 else if (*s == '"') quoted = 1;
670 else if (*s == ',') return s;
671 }
672 return s;
673}
674
675/* Find the first or next occurrence of header <name> in message buffer <sol>
676 * using headers index <idx>, and return it in the <ctx> structure. This
677 * structure holds everything necessary to use the header and find next
678 * occurrence. If its <idx> member is 0, the header is searched from the
679 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * 1 when it finds a value, and 0 when there is no more. It is designed to work
681 * with headers defined as comma-separated lists. As a special case, if ctx->val
682 * is NULL when searching for a new values of a header, the current header is
683 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200684 */
685int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
Willy Tarreau68085d82010-01-18 14:54:04 +0100689 char *eol, *sov;
690 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 cur_idx = ctx->idx;
693 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200694 /* We have previously returned a value, let's search
695 * another one on the same line.
696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200697 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200698 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 eol = sol + idx->v[cur_idx].len;
701
702 if (sov >= eol)
703 /* no more values in this header */
704 goto next_hdr;
705
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 /* values remaining for this header, skip the comma but save it
707 * for later use (eg: for header deletion).
708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200709 sov++;
710 while (sov < eol && http_is_lws[(unsigned char)*sov])
711 sov++;
712
713 goto return_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200723 if (len == 0) {
724 /* No argument was passed, we want any header.
725 * To achieve this, we simply build a fake request. */
726 while (sol + len < eol && sol[len] != ':')
727 len++;
728 name = sol;
729 }
730
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 if ((len < eol - sol) &&
732 (sol[len] == ':') &&
733 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200735 sov = sol + len + 1;
736 while (sov < eol && http_is_lws[(unsigned char)*sov])
737 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100738
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 ctx->prev = old_idx;
741 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744
745 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200747 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200748 eol--;
749 ctx->tws++;
750 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 ctx->vlen = eol - sov;
752 return 1;
753 }
754 next_hdr:
755 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100756 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 cur_idx = idx->v[cur_idx].next;
758 }
759 return 0;
760}
761
762int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 struct hdr_ctx *ctx)
765{
766 return http_find_header2(name, strlen(name), sol, idx, ctx);
767}
768
Willy Tarreau68085d82010-01-18 14:54:04 +0100769/* Remove one value of a header. This only works on a <ctx> returned by one of
770 * the http_find_header functions. The value is removed, as well as surrounding
771 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * message <msg>. The new index is returned. If it is zero, it means there is
774 * no more header, so any processing may stop. The ctx is always left in a form
775 * that can be handled by http_find_header2() to find next occurrence.
776 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100778{
779 int cur_idx = ctx->idx;
780 char *sol = ctx->line;
781 struct hdr_idx_elem *hdr;
782 int delta, skip_comma;
783
784 if (!cur_idx)
785 return 0;
786
787 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 http_msg_move_end(msg, delta);
792 idx->used--;
793 hdr->len = 0; /* unused entry */
794 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100795 if (idx->tail == ctx->idx)
796 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100798 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200800 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 return ctx->idx;
802 }
803
804 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
806 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 */
808
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200809 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200811 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 NULL, 0);
813 hdr->len += delta;
814 http_msg_move_end(msg, delta);
815 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200816 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 return ctx->idx;
818}
819
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820/* This function handles a server error at the stream interface level. The
821 * stream interface is assumed to be already in a closed state. An optional
822 * message is copied into the input buffer, and an HTTP status code stored.
823 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100824 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200826static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200829 channel_auto_read(si->ob);
830 channel_abort(si->ob);
831 channel_auto_close(si->ob);
832 channel_erase(si->ob);
833 channel_auto_close(si->ib);
834 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100835 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200836 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200837 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200839 if (!(s->flags & SN_ERR_MASK))
840 s->flags |= err;
841 if (!(s->flags & SN_FINST_MASK))
842 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843}
844
Willy Tarreau80587432006-12-24 17:47:20 +0100845/* This function returns the appropriate error location for the given session
846 * and message.
847 */
848
Willy Tarreau783f2582012-09-04 12:19:04 +0200849struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100850{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 if (s->be->errmsg[msgnum].str)
852 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100853 else if (s->fe->errmsg[msgnum].str)
854 return &s->fe->errmsg[msgnum];
855 else
856 return &http_err_chunks[msgnum];
857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreau53b6c742006-12-17 13:37:46 +0100859/*
860 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
861 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
862 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100864{
865 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100867
868 m = ((unsigned)*str - 'A');
869
870 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 for (h = http_methods[m]; h->len > 0; h++) {
872 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100873 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100875 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 };
877 return HTTP_METH_OTHER;
878 }
879 return HTTP_METH_NONE;
880
881}
882
Willy Tarreau21d2af32008-02-14 20:25:24 +0100883/* Parse the URI from the given transaction (which is assumed to be in request
884 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
885 * It is returned otherwise.
886 */
887static char *
888http_get_path(struct http_txn *txn)
889{
890 char *ptr, *end;
891
Willy Tarreau9b28e032012-10-12 23:49:43 +0200892 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 end = ptr + txn->req.sl.rq.u_l;
894
895 if (ptr >= end)
896 return NULL;
897
898 /* RFC2616, par. 5.1.2 :
899 * Request-URI = "*" | absuri | abspath | authority
900 */
901
902 if (*ptr == '*')
903 return NULL;
904
905 if (isalpha((unsigned char)*ptr)) {
906 /* this is a scheme as described by RFC3986, par. 3.1 */
907 ptr++;
908 while (ptr < end &&
909 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
910 ptr++;
911 /* skip '://' */
912 if (ptr == end || *ptr++ != ':')
913 return NULL;
914 if (ptr == end || *ptr++ != '/')
915 return NULL;
916 if (ptr == end || *ptr++ != '/')
917 return NULL;
918 }
919 /* skip [user[:passwd]@]host[:[port]] */
920
921 while (ptr < end && *ptr != '/')
922 ptr++;
923
924 if (ptr == end)
925 return NULL;
926
927 /* OK, we got the '/' ! */
928 return ptr;
929}
930
William Lallemand65ad6e12014-01-31 15:08:02 +0100931/* Parse the URI from the given string and look for the "/" beginning the PATH.
932 * If not found, return NULL. It is returned otherwise.
933 */
934static char *
935http_get_path_from_string(char *str)
936{
937 char *ptr = str;
938
939 /* RFC2616, par. 5.1.2 :
940 * Request-URI = "*" | absuri | abspath | authority
941 */
942
943 if (*ptr == '*')
944 return NULL;
945
946 if (isalpha((unsigned char)*ptr)) {
947 /* this is a scheme as described by RFC3986, par. 3.1 */
948 ptr++;
949 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
950 ptr++;
951 /* skip '://' */
952 if (*ptr == '\0' || *ptr++ != ':')
953 return NULL;
954 if (*ptr == '\0' || *ptr++ != '/')
955 return NULL;
956 if (*ptr == '\0' || *ptr++ != '/')
957 return NULL;
958 }
959 /* skip [user[:passwd]@]host[:[port]] */
960
961 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
962 ptr++;
963
964 if (*ptr == '\0' || *ptr == ' ')
965 return NULL;
966
967 /* OK, we got the '/' ! */
968 return ptr;
969}
970
Willy Tarreau71241ab2012-12-27 11:30:54 +0100971/* Returns a 302 for a redirectable request that reaches a server working in
972 * in redirect mode. This may only be called just after the stream interface
973 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
974 * follow normal proxy processing. NOTE: this function is designed to support
975 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100977void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978{
979 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200982 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 trash.len = strlen(HTTP_302);
986 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100988 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100989
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100991 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992 return;
993
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100994 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100996 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
997 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100998 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001000 /* 3: add the request URI. Since it was already forwarded, we need
1001 * to temporarily rewind the buffer.
1002 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +02001004 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001005
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001007 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001008
Willy Tarreau9b28e032012-10-12 23:49:43 +02001009 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001010
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 if (!path)
1012 return;
1013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001014 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 return;
1016
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 memcpy(trash.str + trash.len, path, len);
1018 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001019
1020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001021 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1022 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001023 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001024 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1025 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001026 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001027
1028 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->state = SI_ST_CLO;
1033
1034 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +02001035 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001036
1037 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001038 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001039 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040}
1041
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001043 * that the server side is closed. Note that err_type is actually a
1044 * bitmask, where almost only aborts may be cumulated with other
1045 * values. We consider that aborted operations are more important
1046 * than timeouts or errors due to the fact that nobody else in the
1047 * logs might explain incomplete retries. All others should avoid
1048 * being cumulated. It should normally not be possible to have multiple
1049 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001050 * Note that connection errors appearing on the second request of a keep-alive
1051 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001053void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001054{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001058 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001059 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001061 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001062 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1063 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001064 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001065 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001066 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001068 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001069 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001071 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001072 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1073 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001075 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001076 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001077 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001078 else if (err_type & SI_ET_CONN_RES)
1079 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001080 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1081 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001083 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001084 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085}
1086
Willy Tarreau42250582007-04-01 01:30:43 +02001087extern const char sess_term_cond[8];
1088extern const char sess_fin_state[8];
1089extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001090struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001091struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001092struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Willy Tarreau117f59e2007-03-04 18:17:17 +01001094/*
1095 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001096 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001097 */
1098void capture_headers(char *som, struct hdr_idx *idx,
1099 char **cap, struct cap_hdr *cap_hdr)
1100{
1101 char *eol, *sol, *col, *sov;
1102 int cur_idx;
1103 struct cap_hdr *h;
1104 int len;
1105
1106 sol = som + hdr_idx_first_pos(idx);
1107 cur_idx = hdr_idx_first_idx(idx);
1108
1109 while (cur_idx) {
1110 eol = sol + idx->v[cur_idx].len;
1111
1112 col = sol;
1113 while (col < eol && *col != ':')
1114 col++;
1115
1116 sov = col + 1;
1117 while (sov < eol && http_is_lws[(unsigned char)*sov])
1118 sov++;
1119
1120 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001121 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001122 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1123 if (cap[h->index] == NULL)
1124 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001125 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001126
1127 if (cap[h->index] == NULL) {
1128 Alert("HTTP capture : out of memory.\n");
1129 continue;
1130 }
1131
1132 len = eol - sov;
1133 if (len > h->len)
1134 len = h->len;
1135
1136 memcpy(cap[h->index], sov, len);
1137 cap[h->index][len]=0;
1138 }
1139 }
1140 sol = eol + idx->v[cur_idx].cr + 1;
1141 cur_idx = idx->v[cur_idx].next;
1142 }
1143}
1144
1145
Willy Tarreau42250582007-04-01 01:30:43 +02001146/* either we find an LF at <ptr> or we jump to <bad>.
1147 */
1148#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1149
1150/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1151 * otherwise to <http_msg_ood> with <state> set to <st>.
1152 */
1153#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1154 ptr++; \
1155 if (likely(ptr < end)) \
1156 goto good; \
1157 else { \
1158 state = (st); \
1159 goto http_msg_ood; \
1160 } \
1161 } while (0)
1162
1163
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001165 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1167 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1168 * will give undefined results.
1169 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1170 * and that msg->sol points to the beginning of the response.
1171 * If a complete line is found (which implies that at least one CR or LF is
1172 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1173 * returned indicating an incomplete line (which does not mean that parts have
1174 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1175 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1176 * upon next call.
1177 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001178 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1180 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001181 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001183const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001184 enum ht_state state, const char *ptr, const char *end,
1185 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001188
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001192 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1194
1195 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001196 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1198 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001199 state = HTTP_MSG_ERROR;
1200 break;
1201
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001203 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001205 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 goto http_msg_rpcode;
1207 }
1208 if (likely(HTTP_IS_SPHT(*ptr)))
1209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001215 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 if (likely(!HTTP_IS_LWS(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1218
1219 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1222 }
1223
1224 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 http_msg_rsp_reason:
1227 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001228 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 msg->sl.st.r_l = 0;
1230 goto http_msg_rpline_eol;
1231
Willy Tarreau8973c702007-01-21 23:58:29 +01001232 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 goto http_msg_rpreason;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1240 /* so it's a CR/LF, so there is no reason phrase */
1241 goto http_msg_rsp_reason;
1242
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (likely(!HTTP_IS_CRLF(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 http_msg_rpline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the response would not be
1252 * complete. We can then record the response length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 return ptr;
1257
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001259#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1261 exit(1);
1262#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001263 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 }
1265
1266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001273}
1274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 * This function parses a request line between <ptr> and <end>, starting with
1277 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1278 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the request.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (likely(HTTP_IS_TOKEN(*ptr)))
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_CRLF(*ptr))) {
1312 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001313 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sl.rq.v_l = 0;
1321 goto http_msg_rqline_eol;
1322 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
1325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001329 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rquri;
1331 }
1332 if (likely(HTTP_IS_SPHT(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1334 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1335 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001339 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001343 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001347 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001348 /* non-ASCII chars are forbidden unless option
1349 * accept-invalid-http-request is enabled in the frontend.
1350 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001351 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001352 if (msg->err_pos < -1)
1353 goto invalid_char;
1354 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001356 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1357 }
1358
1359 if (likely(HTTP_IS_CRLF(*ptr))) {
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri_e;
1362 }
1363
1364 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001365 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 state = HTTP_MSG_ERROR;
1368 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001373 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 goto http_msg_rqver;
1375 }
1376 if (likely(HTTP_IS_SPHT(*ptr)))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1378 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1379 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001383 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385
1386 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001387 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001388 http_msg_rqline_eol:
1389 /* We have seen the end of line. Note that we do not
1390 * necessarily have the \n yet, but at least we know that we
1391 * have EITHER \r OR \n, otherwise the request would not be
1392 * complete. We can then record the request length and return
1393 * to the caller which will be able to register it.
1394 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001395 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001396 return ptr;
1397 }
1398
1399 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001400 state = HTTP_MSG_ERROR;
1401 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001404#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
1407#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001408 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001412 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (ret_state)
1414 *ret_state = state;
1415 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001420/*
1421 * Returns the data from Authorization header. Function may be called more
1422 * than once so data is stored in txn->auth_data. When no header is found
1423 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001424 * searching again for something we are unable to find anyway. However, if
1425 * the result if valid, the cache is not reused because we would risk to
1426 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001427 */
1428
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001429/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1430 * set according to global.tune.bufsize.
1431 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001432char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001433
1434int
1435get_http_auth(struct session *s)
1436{
1437
1438 struct http_txn *txn = &s->txn;
1439 struct chunk auth_method;
1440 struct hdr_ctx ctx;
1441 char *h, *p;
1442 int len;
1443
1444#ifdef DEBUG_AUTH
1445 printf("Auth for session %p: %d\n", s, txn->auth.method);
1446#endif
1447
1448 if (txn->auth.method == HTTP_AUTH_WRONG)
1449 return 0;
1450
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001451 txn->auth.method = HTTP_AUTH_WRONG;
1452
1453 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001454
1455 if (txn->flags & TX_USE_PX_CONN) {
1456 h = "Proxy-Authorization";
1457 len = strlen(h);
1458 } else {
1459 h = "Authorization";
1460 len = strlen(h);
1461 }
1462
Willy Tarreau9b28e032012-10-12 23:49:43 +02001463 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001464 return 0;
1465
1466 h = ctx.line + ctx.val;
1467
1468 p = memchr(h, ' ', ctx.vlen);
1469 if (!p || p == h)
1470 return 0;
1471
1472 chunk_initlen(&auth_method, h, 0, p-h);
1473 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1474
1475 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1476
1477 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001478 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001479
1480 if (len < 0)
1481 return 0;
1482
1483
1484 get_http_auth_buff[len] = '\0';
1485
1486 p = strchr(get_http_auth_buff, ':');
1487
1488 if (!p)
1489 return 0;
1490
1491 txn->auth.user = get_http_auth_buff;
1492 *p = '\0';
1493 txn->auth.pass = p+1;
1494
1495 txn->auth.method = HTTP_AUTH_BASIC;
1496 return 1;
1497 }
1498
1499 return 0;
1500}
1501
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503/*
1504 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001505 * depending on the initial msg->msg_state. The caller is responsible for
1506 * ensuring that the message does not wrap. The function can be preempted
1507 * everywhere when data are missing and recalled at the exact same location
1508 * with no information loss. The message may even be realigned between two
1509 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001510 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001511 * fields. Note that msg->sol will be initialized after completing the first
1512 * state, so that none of the msg pointers has to be initialized prior to the
1513 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001515void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516{
Willy Tarreau3770f232013-12-07 00:01:53 +01001517 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001522 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 ptr = buf->p + msg->next;
1524 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(ptr >= end))
1527 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /*
1531 * First, states that are specific to the response only.
1532 * We check them first so that request and headers are
1533 * closer to each other (accessed more often).
1534 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 hdr_idx_init(idx);
1551 state = HTTP_MSG_RPVER;
1552 goto http_msg_rpver;
1553 }
1554
1555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
1557
1558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1561 /* stop here */
1562
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1567 /* stop here */
1568
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER_SP:
1572 case HTTP_MSG_RPCODE:
1573 case HTTP_MSG_RPCODE_SP:
1574 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 if (unlikely(!ptr))
1579 return;
1580
1581 /* we have a full response and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1589 goto http_msg_rpline_end;
1590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* msg->sol must point to the first of CR or LF. */
1594 EXPECT_LF_HERE(ptr, http_msg_invalid);
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1596 /* stop here */
1597
1598 /*
1599 * Second, states that are specific to the request only
1600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001602 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001604 /* we have a start of message, but we have to check
1605 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001606 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 if (likely(ptr != buf->p)) {
1609 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001611 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001612 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 }
Willy Tarreau26927362012-05-18 23:22:52 +02001614 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001615 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 state = HTTP_MSG_RQMETH;
1617 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1621 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(*ptr == '\n'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1625 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH_SP:
1637 case HTTP_MSG_RQURI:
1638 case HTTP_MSG_RQURI_SP:
1639 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001640 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001641 state, ptr, end,
1642 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(!ptr))
1644 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* we have a full request and we know that we have either a CR
1647 * or an LF at <ptr>.
1648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 /* check for HTTP/0.9 request : no version information available.
1659 * msg->sol must point to the first of CR or LF.
1660 */
1661 if (unlikely(msg->sl.rq.v_l == 0))
1662 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /*
1669 * Common states below
1670 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 if (likely(!HTTP_IS_CRLF(*ptr))) {
1675 goto http_msg_hdr_name;
1676 }
1677
1678 if (likely(*ptr == '\r'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1680 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 /* assumes msg->sol points to the first char */
1685 if (likely(HTTP_IS_TOKEN(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001688 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001691 if (likely(msg->err_pos < -1) || *ptr == '\n')
1692 goto http_msg_invalid;
1693
1694 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001696
1697 /* and we still accept this non-token character */
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001701 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_SPHT(*ptr)))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_val;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1715 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 EXPECT_LF_HERE(ptr, http_msg_invalid);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr))) {
1725 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 for (; buf->p + msg->sov < ptr; msg->sov++)
1727 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto http_msg_hdr_l1_sp;
1729 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001730 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001731 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 goto http_msg_complete_header;
1733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001736 /* assumes msg->sol points to the first char, and msg->sov
1737 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 */
1739 if (likely(!HTTP_IS_CRLF(*ptr)))
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* Note: we could also copy eol into ->eoh so that we have the
1744 * real header end in case it ends with lots of LWS, but is this
1745 * really needed ?
1746 */
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1749 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 EXPECT_LF_HERE(ptr, http_msg_invalid);
1754 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1759 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001760 for (; buf->p + msg->eol < ptr; msg->eol++)
1761 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 goto http_msg_hdr_val;
1763 }
1764 http_msg_complete_header:
1765 /*
1766 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001767 * Assumes msg->sol points to the first char, msg->sov points
1768 * to the first character of the value and msg->eol to the
1769 * first CR or LF so we know how the line ends. We insert last
1770 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 idx, idx->tail) < 0))
1774 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001775
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001787 /* Assumes msg->sol points to the first of either CR or LF.
1788 * Sets ->sov and ->next to the total header length, ->eoh to
1789 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EXPECT_LF_HERE(ptr, http_msg_invalid);
1792 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001794 msg->eoh = msg->sol;
1795 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001796 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001797 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001799
1800 case HTTP_MSG_ERROR:
1801 /* this may only happen if we call http_msg_analyser() twice with an error */
1802 break;
1803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001805#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1807 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001809 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
1811 http_msg_ood:
1812 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001813 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 http_msg_invalid:
1818 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001819 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 return;
1822}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001823
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1825 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1826 * nothing is done and 1 is returned.
1827 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001828static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001829{
1830 int delta;
1831 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001832 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001833
1834 if (msg->sl.rq.v_l != 0)
1835 return 1;
1836
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001837 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1838 if (txn->meth != HTTP_METH_GET)
1839 return 0;
1840
Willy Tarreau9b28e032012-10-12 23:49:43 +02001841 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001842 delta = 0;
1843
1844 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001845 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1846 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001847 }
1848 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001850 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001851 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001852 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001854 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 NULL, NULL);
1856 if (unlikely(!cur_end))
1857 return 0;
1858
1859 /* we have a full HTTP/1.0 request now and we know that
1860 * we have either a CR or an LF at <ptr>.
1861 */
1862 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1863 return 1;
1864}
1865
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001867 * and "keep-alive" values. If we already know that some headers may safely
1868 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1870 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001871 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1873 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1874 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001878{
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 const char *hdr_val = "Connection";
1881 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 return;
1885
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1887 hdr_val = "Proxy-Connection";
1888 hdr_len = 16;
1889 }
1890
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001893 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1895 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 if (to_del & 2)
1897 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else
1899 txn->flags |= TX_CON_KAL_SET;
1900 }
1901 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1902 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 if (to_del & 1)
1904 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 else
1906 txn->flags |= TX_CON_CLO_SET;
1907 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001908 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1909 txn->flags |= TX_HDR_CONN_UPG;
1910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 }
1912
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags |= TX_HDR_CONN_PRS;
1914 return;
1915}
Willy Tarreau5b154472009-12-21 20:11:07 +01001916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001917/* Apply desired changes on the Connection: header. Values may be removed and/or
1918 * added depending on the <wanted> flags, which are exclusively composed of
1919 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1920 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1921 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001922void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001923{
1924 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001925 const char *hdr_val = "Connection";
1926 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001927
1928 ctx.idx = 0;
1929
Willy Tarreau88d349d2010-01-25 12:15:43 +01001930
1931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1932 hdr_val = "Proxy-Connection";
1933 hdr_len = 16;
1934 }
1935
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001936 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001937 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1939 if (wanted & TX_CON_KAL_SET)
1940 txn->flags |= TX_CON_KAL_SET;
1941 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1945 if (wanted & TX_CON_CLO_SET)
1946 txn->flags |= TX_CON_CLO_SET;
1947 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001948 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001949 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001950 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951
1952 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1953 return;
1954
1955 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1956 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 hdr_val = "Connection: close";
1958 hdr_len = 17;
1959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1960 hdr_val = "Proxy-Connection: close";
1961 hdr_len = 23;
1962 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 }
1965
1966 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1967 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001968 hdr_val = "Connection: keep-alive";
1969 hdr_len = 22;
1970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection: keep-alive";
1972 hdr_len = 28;
1973 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001974 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 }
1976 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001977}
1978
Willy Tarreauc24715e2014-04-17 15:21:20 +02001979/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1980 * the first byte of data after the chunk size, so that we know we can forward
1981 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1982 * the chunk size. That way it is always possible to differentiate between the
1983 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001987static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001988{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001989 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001991 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *end = buf->data + buf->size;
1993 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 unsigned int chunk = 0;
1995
1996 /* The chunk size is in the following form, though we are only
1997 * interested in the size and CRLF :
1998 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1999 */
2000 while (1) {
2001 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 if (c < 0) /* not a hex digit anymore */
2006 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002009 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002011 chunk = (chunk << 4) + c;
2012 }
2013
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002015 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017
2018 while (http_is_spht[(unsigned char)*ptr]) {
2019 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002021 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 }
2024
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 /* Up to there, we know that at least one byte is present at *ptr. Check
2026 * for the end of chunk size.
2027 */
2028 while (1) {
2029 if (likely(HTTP_IS_CRLF(*ptr))) {
2030 /* we now have a CR or an LF at ptr */
2031 if (likely(*ptr == '\r')) {
2032 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002037
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002039 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 /* done */
2043 break;
2044 }
2045 else if (*ptr == ';') {
2046 /* chunk extension, ends at next CRLF */
2047 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 while (!HTTP_IS_CRLF(*ptr)) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
2058 /* we have a CRLF now, loop above */
2059 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 }
2064
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002066 * which may or may not be present. We save that into ->next,
2067 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002069 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002070 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002073 msg->chunk_len = chunk;
2074 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002077 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080}
2081
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002082/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 * the trailers is found, it is automatically scheduled to be forwarded,
2085 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2086 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * except maybe msg->next if it could parse some lines, and returns zero.
2088 * If a parse error is encountered, the function returns < 0 and does not
2089 * change anything except maybe msg->next. Note that the message must
2090 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002091 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 * forwarding, and that msg->next exactly matches the length of trailers
2093 * already parsed and not forwarded. It is also important to note that this
2094 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002096static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002098 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002099
Willy Tarreaua458b672012-03-05 11:17:50 +01002100 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *ptr = b_ptr(buf, msg->next);
2104 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002105 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106
2107 /* scan current line and stop at LF or CRLF */
2108 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002109 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 return 0;
2111
2112 if (*ptr == '\n') {
2113 if (!p1)
2114 p1 = ptr;
2115 p2 = ptr;
2116 break;
2117 }
2118
2119 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 p1 = ptr;
2125 }
2126
2127 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 if (ptr >= buf->data + buf->size)
2129 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 }
2131
2132 /* after LF; point to beginning of next line */
2133 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (p2 >= buf->data + buf->size)
2135 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002137 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002138 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002140
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 /* LF/CRLF at beginning of line => end of trailers at p2.
2143 * Everything was scheduled for forwarding, there's nothing
2144 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_DONE;
2148 return 1;
2149 }
2150 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 }
2153}
2154
Willy Tarreauc24715e2014-04-17 15:21:20 +02002155/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2156 * or a possible LF alone at the end of a chunk. It automatically adjusts
2157 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002158 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2160 * not enough data are available, the function does not change anything and
2161 * returns zero. If a parse error is encountered, the function returns < 0 and
2162 * does not change anything. Note: this function is designed to parse wrapped
2163 * CRLF at the end of the buffer.
2164 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002165static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002167 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002168 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169 int bytes;
2170
2171 /* NB: we'll check data availabilty at the end. It's not a
2172 * problem because whatever we match first will be checked
2173 * against the correct length.
2174 */
2175 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002176 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 if (*ptr == '\r') {
2178 bytes++;
2179 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 if (ptr >= buf->data + buf->size)
2181 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 }
2183
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002184 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002185 return 0;
2186
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002187 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002188 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191
2192 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002193 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002194 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2198 return 1;
2199}
Willy Tarreau5b154472009-12-21 20:11:07 +01002200
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002201/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2202 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2203 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2204 * Unparsable qvalues return 1000 as "q=1.000".
2205 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002206int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207{
2208 int q = 1000;
2209
Willy Tarreau506c69a2014-07-08 00:59:48 +02002210 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002211 goto out;
2212 q = (*qvalue++ - '0') * 1000;
2213
2214 if (*qvalue++ != '.')
2215 goto out;
2216
Willy Tarreau506c69a2014-07-08 00:59:48 +02002217 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002218 goto out;
2219 q += (*qvalue++ - '0') * 100;
2220
Willy Tarreau506c69a2014-07-08 00:59:48 +02002221 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 goto out;
2223 q += (*qvalue++ - '0') * 10;
2224
Willy Tarreau506c69a2014-07-08 00:59:48 +02002225 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 goto out;
2227 q += (*qvalue++ - '0') * 1;
2228 out:
2229 if (q > 1000)
2230 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002231 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002232 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 return q;
2234}
William Lallemand82fe75c2012-10-23 10:25:10 +02002235
2236/*
2237 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002238 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002239int select_compression_request_header(struct session *s, struct buffer *req)
2240{
2241 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 struct hdr_ctx ctx;
2244 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002245 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002247 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2248 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002249 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2250 */
2251 ctx.idx = 0;
2252 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2253 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002254 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2255 (ctx.vlen < 31 ||
2256 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2257 ctx.line[ctx.val + 30] < '6' ||
2258 (ctx.line[ctx.val + 30] == '6' &&
2259 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2260 s->comp_algo = NULL;
2261 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002262 }
2263
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 int best_q = 0;
2267
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 ctx.idx = 0;
2269 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002270 const char *qval;
2271 int q;
2272 int toklen;
2273
2274 /* try to isolate the token from the optional q-value */
2275 toklen = 0;
2276 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2277 toklen++;
2278
2279 qval = ctx.line + ctx.val + toklen;
2280 while (1) {
2281 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2282 qval++;
2283
2284 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2285 qval = NULL;
2286 break;
2287 }
2288 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002289
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2291 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002292
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002293 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2294 qval = NULL;
2295 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002297 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2298 break;
2299
2300 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2301 qval++;
2302 }
2303
2304 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002305 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306
2307 if (q <= best_q)
2308 continue;
2309
2310 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2311 if (*(ctx.line + ctx.val) == '*' ||
2312 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2313 s->comp_algo = comp_algo;
2314 best_q = q;
2315 break;
2316 }
2317 }
2318 }
2319 }
2320
2321 /* remove all occurrences of the header when "compression offload" is set */
2322 if (s->comp_algo) {
2323 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2324 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2325 ctx.idx = 0;
2326 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2327 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 }
2329 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002330 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
2332
2333 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2335 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 if (comp_algo->add_data == identity_add_data) {
2337 s->comp_algo = comp_algo;
2338 return 1;
2339 }
2340 }
2341 }
2342
2343 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 return 0;
2345}
2346
2347/*
2348 * Selects a comression algorithm depending of the server response.
2349 */
2350int select_compression_response_header(struct session *s, struct buffer *res)
2351{
2352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->rsp;
2354 struct hdr_ctx ctx;
2355 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002356
2357 /* no common compression algorithm was found in request header */
2358 if (s->comp_algo == NULL)
2359 goto fail;
2360
2361 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002362 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 goto fail;
2364
William Lallemandd3002612012-11-26 14:34:47 +01002365 /* 200 only */
2366 if (txn->status != 200)
2367 goto fail;
2368
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 /* Content-Length is null */
2370 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2371 goto fail;
2372
2373 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002374 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2376 goto fail;
2377
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002378 /* no compression when Cache-Control: no-transform is present in the message */
2379 ctx.idx = 0;
2380 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2381 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2382 goto fail;
2383 }
2384
William Lallemand82fe75c2012-10-23 10:25:10 +02002385 comp_type = NULL;
2386
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002387 /* we don't want to compress multipart content-types, nor content-types that are
2388 * not listed in the "compression type" directive if any. If no content-type was
2389 * found but configuration requires one, we don't compress either. Backend has
2390 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002391 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002392 ctx.idx = 0;
2393 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2394 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2395 goto fail;
2396
2397 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2398 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002399 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002400 if (ctx.vlen >= comp_type->name_len &&
2401 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 /* this Content-Type should be compressed */
2403 break;
2404 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002405 /* this Content-Type should not be compressed */
2406 if (comp_type == NULL)
2407 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002409 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 else { /* no content-type header */
2411 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2412 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002413 }
2414
William Lallemandd85f9172012-11-09 17:05:39 +01002415 /* limit compression rate */
2416 if (global.comp_rate_lim > 0)
2417 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2418 goto fail;
2419
William Lallemand072a2bf2012-11-20 17:01:01 +01002420 /* limit cpu usage */
2421 if (idle_pct < compress_min_idle)
2422 goto fail;
2423
William Lallemand4c49fae2012-11-07 15:00:23 +01002424 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002425 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002426 goto fail;
2427
William Lallemandec3e3892012-11-12 17:02:18 +01002428 s->flags |= SN_COMP_READY;
2429
William Lallemand82fe75c2012-10-23 10:25:10 +02002430 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002431 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2434
2435 /* add Transfer-Encoding header */
2436 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2437 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2438
2439 /*
2440 * Add Content-Encoding header when it's not identity encoding.
2441 * RFC 2616 : Identity encoding: This content-coding is used only in the
2442 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2443 * header.
2444 */
2445 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002446 trash.len = 18;
2447 memcpy(trash.str, "Content-Encoding: ", trash.len);
2448 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2449 trash.len += s->comp_algo->name_len;
2450 trash.str[trash.len] = '\0';
2451 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002452 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 return 1;
2454
2455fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002456 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002457 return 0;
2458}
2459
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002460void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2461{
2462 int tmp = TX_CON_WANT_KAL;
2463
2464 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2465 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2467 tmp = TX_CON_WANT_TUN;
2468
2469 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2470 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2471 tmp = TX_CON_WANT_TUN;
2472 }
2473
2474 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2475 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2476 /* option httpclose + server_close => forceclose */
2477 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2479 tmp = TX_CON_WANT_CLO;
2480 else
2481 tmp = TX_CON_WANT_SCL;
2482 }
2483
2484 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2486 tmp = TX_CON_WANT_CLO;
2487
2488 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2490
2491 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2492 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2493 /* parse the Connection header and possibly clean it */
2494 int to_del = 0;
2495 if ((msg->flags & HTTP_MSGF_VER_11) ||
2496 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2497 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2498 to_del |= 2; /* remove "keep-alive" */
2499 if (!(msg->flags & HTTP_MSGF_VER_11))
2500 to_del |= 1; /* remove "close" */
2501 http_parse_connection_header(txn, msg, to_del);
2502 }
2503
2504 /* check if client or config asks for explicit close in KAL/SCL */
2505 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2506 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2507 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2508 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2509 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2510 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2511 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2512}
William Lallemand82fe75c2012-10-23 10:25:10 +02002513
Willy Tarreaud787e662009-07-07 10:14:51 +02002514/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2515 * processing can continue on next analysers, or zero if it either needs more
2516 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2517 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2518 * when it has nothing left to do, and may remove any analyser when it wants to
2519 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002520 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002521int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002522{
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 /*
2524 * We will parse the partial (or complete) lines.
2525 * We will check the request syntax, and also join multi-line
2526 * headers. An index of all the lines will be elaborated while
2527 * parsing.
2528 *
2529 * For the parsing, we use a 28 states FSM.
2530 *
2531 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002532 * req->buf->p = beginning of request
2533 * req->buf->p + msg->eoh = end of processed headers / start of current one
2534 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002535 * msg->eol = end of current header or line (LF or CRLF)
2536 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 *
2538 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002539 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2541 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002545 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 struct http_txn *txn = &s->txn;
2547 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002548 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002549
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002550 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002551 now_ms, __FUNCTION__,
2552 s,
2553 req,
2554 req->rex, req->wex,
2555 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002557 req->analysers);
2558
Willy Tarreau52a0c602009-08-16 22:45:38 +02002559 /* we're speaking HTTP here, so let's speak HTTP to the client */
2560 s->srv_error = http_return_srv_error;
2561
Willy Tarreau83e3af02009-12-28 17:39:57 +01002562 /* There's a protected area at the end of the buffer for rewriting
2563 * purposes. We don't want to start to parse the request if the
2564 * protected area is affected, because we may have to move processed
2565 * data later, which is much more complicated.
2566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002567 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002568 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002569 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002570 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002571 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002572 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002573 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002574 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002575 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002576 return 0;
2577 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002578 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2579 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2580 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002581 }
2582
Willy Tarreau065e8332010-01-08 00:30:20 +01002583 /* Note that we have the same problem with the response ; we
2584 * may want to send a redirect, error or anything which requires
2585 * some spare space. So we'll ensure that we have at least
2586 * maxrewrite bytes available in the response buffer before
2587 * processing that one. This will only affect pipelined
2588 * keep-alive requests.
2589 */
2590 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreauba0902e2015-01-13 14:39:16 +01002591 unlikely(!channel_is_rewritable(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2593 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2594 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002597 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002600 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002601 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002602 return 0;
2603 }
2604 }
2605
Willy Tarreau9b28e032012-10-12 23:49:43 +02002606 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002607 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002608 }
2609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* 1: we might have to print this header in debug mode */
2611 if (unlikely((global.mode & MODE_DEBUG) &&
2612 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002613 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau9b28e032012-10-12 23:49:43 +02002616 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002617 /* this is a bit complex : in case of error on the request line,
2618 * we know that rq.l is still zero, so we display only the part
2619 * up to the end of the line (truncated by debug_hdr).
2620 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002621 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 sol += hdr_idx_first_pos(&txn->hdr_idx);
2625 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 while (cur_idx) {
2628 eol = sol + txn->hdr_idx.v[cur_idx].len;
2629 debug_hdr("clihdr", s, sol, eol);
2630 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2631 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 }
2634
Willy Tarreau58f10d72006-12-04 02:26:12 +01002635
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 /*
2637 * Now we quickly check if we have found a full valid request.
2638 * If not so, we check the FD and buffer states before leaving.
2639 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002640 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002641 * requests are checked first. When waiting for a second request
2642 * on a keep-alive session, if we encounter and error, close, t/o,
2643 * we note the error in the session flags but don't set any state.
2644 * Since the error will be noted there, it will not be counted by
2645 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002646 * Last, we may increase some tracked counters' http request errors on
2647 * the cases that are deliberately the client's fault. For instance,
2648 * a timeout or connection reset is not counted as an error. However
2649 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau655dce92009-11-08 13:10:58 +01002652 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002653 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002655 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002656 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_req_ctr(s);
2658 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 /* 1: Since we are in header mode, if there's no space
2664 * left for headers, we won't be able to free more
2665 * later, so the session will never terminate. We
2666 * must terminate it now.
2667 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /* FIXME: check if URI is set and return Status
2670 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002671 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002672 session_inc_http_req_ctr(s);
2673 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002674 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002675 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 goto return_bad_req;
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002682 if (!(s->flags & SN_ERR_MASK))
2683 s->flags |= SN_ERR_CLICL;
2684
Willy Tarreaufcffa692010-01-10 14:21:19 +01002685 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002686 goto failed_keep_alive;
2687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002690 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002691 session_inc_http_err_ctr(s);
2692 }
2693
Willy Tarreaudc979f22012-12-04 10:39:01 +01002694 txn->status = 400;
2695 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 msg->msg_state = HTTP_MSG_ERROR;
2697 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002700 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002701 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002702 if (s->listener->counters)
2703 s->listener->counters->failed_req++;
2704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 if (!(s->flags & SN_FINST_MASK))
2706 s->flags |= SN_FINST_R;
2707 return 0;
2708 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002709
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002711 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002712 if (!(s->flags & SN_ERR_MASK))
2713 s->flags |= SN_ERR_CLITO;
2714
Willy Tarreaufcffa692010-01-10 14:21:19 +01002715 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002716 goto failed_keep_alive;
2717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002719 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002720 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002721 session_inc_http_err_ctr(s);
2722 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002724 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 msg->msg_state = HTTP_MSG_ERROR;
2726 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002727
Willy Tarreauda7ff642010-06-23 11:44:09 +02002728 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002729 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002730 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731 if (s->listener->counters)
2732 s->listener->counters->failed_req++;
2733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 if (!(s->flags & SN_FINST_MASK))
2735 s->flags |= SN_FINST_R;
2736 return 0;
2737 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002740 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002741 if (!(s->flags & SN_ERR_MASK))
2742 s->flags |= SN_ERR_CLICL;
2743
Willy Tarreaufcffa692010-01-10 14:21:19 +01002744 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002745 goto failed_keep_alive;
2746
Willy Tarreau4076a152009-04-02 15:18:36 +02002747 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002748 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 msg->msg_state = HTTP_MSG_ERROR;
2752 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreauda7ff642010-06-23 11:44:09 +02002754 session_inc_http_err_ctr(s);
2755 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002756 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002757 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758 if (s->listener->counters)
2759 s->listener->counters->failed_req++;
2760
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 if (!(s->flags & SN_FINST_MASK))
2762 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002763 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 }
2765
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002766 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002767 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2768 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002769#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002770 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002771 /* We need more data, we have to re-enable quick-ack in case we
2772 * previously disabled it, otherwise we might cause the client
2773 * to delay next data.
2774 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002775 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002776 }
2777#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002778
Willy Tarreaufcffa692010-01-10 14:21:19 +01002779 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2780 /* If the client starts to talk, let's fall back to
2781 * request timeout processing.
2782 */
2783 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002784 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002785 }
2786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002788 if (!tick_isset(req->analyse_exp)) {
2789 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2790 (txn->flags & TX_WAIT_NEXT_RQ) &&
2791 tick_isset(s->be->timeout.httpka))
2792 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2793 else
2794 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002796
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 /* we're not ready yet */
2798 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002799
2800 failed_keep_alive:
2801 /* Here we process low-level errors for keep-alive requests. In
2802 * short, if the request is not the first one and it experiences
2803 * a timeout, read error or shutdown, we just silently close so
2804 * that the client can try again.
2805 */
2806 txn->status = 0;
2807 msg->msg_state = HTTP_MSG_RQBEFORE;
2808 req->analysers = 0;
2809 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002810 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002811 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002812 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002813 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002815
Willy Tarreaud787e662009-07-07 10:14:51 +02002816 /* OK now we have a complete HTTP request with indexed headers. Let's
2817 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002818 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002819 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002820 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002821 * byte after the last LF. msg->sov points to the first byte of data.
2822 * msg->eol cannot be trusted because it may have been left uninitialized
2823 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002825
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002827 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2828
Willy Tarreaub16a5742010-01-10 14:46:16 +01002829 if (txn->flags & TX_WAIT_NEXT_RQ) {
2830 /* kill the pending keep-alive timeout */
2831 txn->flags &= ~TX_WAIT_NEXT_RQ;
2832 req->analyse_exp = TICK_ETERNITY;
2833 }
2834
2835
Willy Tarreaud787e662009-07-07 10:14:51 +02002836 /* Maybe we found in invalid header name while we were configured not
2837 * to block on that, so we have to capture it now.
2838 */
2839 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002840 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002841
Willy Tarreau59234e92008-11-30 23:51:27 +01002842 /*
2843 * 1: identify the method
2844 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002845 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002846
2847 /* we can make use of server redirect on GET and HEAD */
2848 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2849 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /*
2852 * 2: check if the URI matches the monitor_uri.
2853 * We have to do this for every request which gets in, because
2854 * the monitor-uri is defined by the frontend.
2855 */
2856 if (unlikely((s->fe->monitor_uri_len != 0) &&
2857 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002858 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 s->fe->monitor_uri,
2860 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002863 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002865
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002867 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002871 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002872
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 ret = acl_pass(ret);
2874 if (cond->pol == ACL_COND_UNLESS)
2875 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 if (ret) {
2878 /* we fail this request, let's return 503 service unavail */
2879 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002880 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002881 if (!(s->flags & SN_ERR_MASK))
2882 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002884 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002885 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002886
Willy Tarreau59234e92008-11-30 23:51:27 +01002887 /* nothing to fail, let's reply normaly */
2888 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002889 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002890 if (!(s->flags & SN_ERR_MASK))
2891 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002892 goto return_prx_cond;
2893 }
2894
2895 /*
2896 * 3: Maybe we have to copy the original REQURI for the logs ?
2897 * Note: we cannot log anymore if the request has been
2898 * classified as invalid.
2899 */
2900 if (unlikely(s->logs.logwait & LW_REQ)) {
2901 /* we have a complete HTTP request that we must log */
2902 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2903 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002904
Willy Tarreau59234e92008-11-30 23:51:27 +01002905 if (urilen >= REQURI_LEN)
2906 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002907 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002909
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002910 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 s->do_log(s);
2912 } else {
2913 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002914 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 }
Willy Tarreau06619262006-12-17 08:37:22 +01002916
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002918 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002919 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002920
Willy Tarreau5b154472009-12-21 20:11:07 +01002921 /* ... and check if the request is HTTP/1.1 or above */
2922 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002923 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2924 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2925 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002926 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002927
2928 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002929 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002930
Willy Tarreau88d349d2010-01-25 12:15:43 +01002931 /* if the frontend has "option http-use-proxy-header", we'll check if
2932 * we have what looks like a proxied connection instead of a connection,
2933 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2934 * Note that this is *not* RFC-compliant, however browsers and proxies
2935 * happen to do that despite being non-standard :-(
2936 * We consider that a request not beginning with either '/' or '*' is
2937 * a proxied connection, which covers both "scheme://location" and
2938 * CONNECT ip:port.
2939 */
2940 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002941 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002942 txn->flags |= TX_USE_PX_CONN;
2943
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002944 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002945 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002946
Willy Tarreau59234e92008-11-30 23:51:27 +01002947 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002948 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002951
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002952 /* 6: determine the transfer-length.
2953 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2954 * the presence of a message-body in a REQUEST and its transfer length
2955 * must be determined that way (in order of precedence) :
2956 * 1. The presence of a message-body in a request is signaled by the
2957 * inclusion of a Content-Length or Transfer-Encoding header field
2958 * in the request's header fields. When a request message contains
2959 * both a message-body of non-zero length and a method that does
2960 * not define any semantics for that request message-body, then an
2961 * origin server SHOULD either ignore the message-body or respond
2962 * with an appropriate error message (e.g., 413). A proxy or
2963 * gateway, when presented the same request, SHOULD either forward
2964 * the request inbound with the message- body or ignore the
2965 * message-body when determining a response.
2966 *
2967 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2968 * and the "chunked" transfer-coding (Section 6.2) is used, the
2969 * transfer-length is defined by the use of this transfer-coding.
2970 * If a Transfer-Encoding header field is present and the "chunked"
2971 * transfer-coding is not present, the transfer-length is defined
2972 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002974 * 3. If a Content-Length header field is present, its decimal value in
2975 * OCTETs represents both the entity-length and the transfer-length.
2976 * If a message is received with both a Transfer-Encoding header
2977 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002978 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002979 * 4. By the server closing the connection. (Closing the connection
2980 * cannot be used to indicate the end of a request body, since that
2981 * would leave no possibility for the server to send back a response.)
2982 *
2983 * Whenever a transfer-coding is applied to a message-body, the set of
2984 * transfer-codings MUST include "chunked", unless the message indicates
2985 * it is terminated by closing the connection. When the "chunked"
2986 * transfer-coding is used, it MUST be the last transfer-coding applied
2987 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002988 */
2989
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002990 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002991 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002992 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002993 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002994 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2997 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* bad transfer-encoding (chunked followed by something else) */
2999 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003000 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 break;
3002 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 }
3004
Willy Tarreau32b47f42009-10-18 20:55:02 +02003005 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003006 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003007 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 signed long long cl;
3009
Willy Tarreauad14f752011-09-02 20:33:27 +02003010 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreauad14f752011-09-02 20:33:27 +02003015 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003016 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003019
Willy Tarreauad14f752011-09-02 20:33:27 +02003020 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003021 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003024
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003025 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003028 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003030 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003031 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032 }
3033
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003034 /* bodyless requests have a known length */
3035 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003036 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003037
Willy Tarreau179085c2014-04-28 16:48:56 +02003038 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3039 * only change if both the request and the config reference something else.
3040 * Option httpclose by itself sets tunnel mode where headers are mangled.
3041 * However, if another mode is set, it will affect it (eg: server-close/
3042 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3043 * if FE and BE have the same settings (common). The method consists in
3044 * checking if options changed between the two calls (implying that either
3045 * one is non-null, or one of them is non-null and we are there for the first
3046 * time.
3047 */
3048 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003049 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3050 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003051
Willy Tarreaud787e662009-07-07 10:14:51 +02003052 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003053 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003054 req->analyse_exp = TICK_ETERNITY;
3055 return 1;
3056
3057 return_bad_req:
3058 /* We centralize bad requests processing here */
3059 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3060 /* we detected a parsing error. We want to archive this request
3061 * in the dedicated proxy area for later troubleshooting.
3062 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003063 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003064 }
3065
3066 txn->req.msg_state = HTTP_MSG_ERROR;
3067 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003068 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003069
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003070 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003071 if (s->listener->counters)
3072 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003073
3074 return_prx_cond:
3075 if (!(s->flags & SN_ERR_MASK))
3076 s->flags |= SN_ERR_PRXCOND;
3077 if (!(s->flags & SN_FINST_MASK))
3078 s->flags |= SN_FINST_R;
3079
3080 req->analysers = 0;
3081 req->analyse_exp = TICK_ETERNITY;
3082 return 0;
3083}
3084
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003085
Willy Tarreau347a35d2013-11-22 17:51:09 +01003086/* This function prepares an applet to handle the stats. It can deal with the
3087 * "100-continue" expectation, check that admin rules are met for POST requests,
3088 * and program a response message if something was unexpected. It cannot fail
3089 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003090 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003091 * s->target which is supposed to already point to the stats applet. The caller
3092 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 */
3094int http_handle_stats(struct session *s, struct channel *req)
3095{
3096 struct stats_admin_rule *stats_admin_rule;
3097 struct stream_interface *si = s->rep->prod;
3098 struct http_txn *txn = &s->txn;
3099 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003100 struct uri_auth *uri_auth = s->be->uri_auth;
3101 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003104 appctx = si_appctx(si);
3105 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3106 appctx->st1 = appctx->st2 = 0;
3107 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3108 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003109 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3110 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111
3112 uri = msg->chn->buf->p + msg->sl.rq.u;
3113 lookup = uri + uri_auth->uri_len;
3114
3115 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3116 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 break;
3119 }
3120 }
3121
3122 if (uri_auth->refresh) {
3123 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3124 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126 break;
3127 }
3128 }
3129 }
3130
3131 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3132 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003134 break;
3135 }
3136 }
3137
3138 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3139 if (memcmp(h, ";st=", 4) == 0) {
3140 int i;
3141 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003142 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003143 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3144 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 break;
3147 }
3148 }
3149 break;
3150 }
3151 }
3152
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003153 appctx->ctx.stats.scope_str = 0;
3154 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003155 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3156 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3157 int itx = 0;
3158 const char *h2;
3159 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3160 const char *err;
3161
3162 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3163 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003165 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3166 itx++;
3167 h++;
3168 }
3169
3170 if (itx > STAT_SCOPE_TXT_MAXLEN)
3171 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003173
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 memcpy(scope_txt, h2, itx);
3176 scope_txt[itx] = '\0';
3177 err = invalid_char(scope_txt);
3178 if (err) {
3179 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.scope_str = 0;
3181 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 }
3183 break;
3184 }
3185 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003186
3187 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189 int ret = 1;
3190
3191 if (stats_admin_rule->cond) {
3192 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3193 ret = acl_pass(ret);
3194 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3195 ret = !ret;
3196 }
3197
3198 if (ret) {
3199 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003201 break;
3202 }
3203 }
3204
3205 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003206 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003208 /* we'll need the request body, possibly after sending 100-continue */
3209 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003211 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003212 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3214 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003215 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003217 else {
3218 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003220 }
3221
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003222 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003223 return 1;
3224}
3225
Lukas Tribus67db8df2013-06-23 17:37:13 +02003226/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3227 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3228 */
3229static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3230{
3231#ifdef IP_TOS
3232 if (from.ss_family == AF_INET)
3233 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3234#endif
3235#ifdef IPV6_TCLASS
3236 if (from.ss_family == AF_INET6) {
3237 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3238 /* v4-mapped addresses need IP_TOS */
3239 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3240 else
3241 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3242 }
3243#endif
3244}
3245
Sasha Pachev218f0642014-06-16 12:05:59 -06003246/* Returns the number of characters written to destination,
3247 * -1 on internal error and -2 if no replacement took place.
3248 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003249static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003250 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003251{
Willy Tarreau15a53a42015-01-21 13:39:42 +01003252 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch, 0))
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 return -2;
3254
3255 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3256}
3257
3258/* Returns the number of characters written to destination,
3259 * -1 on internal error and -2 if no replacement took place.
3260 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003261static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003262 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003263{
3264 char* p = val;
3265 char* dst_end = dst + dst_size;
3266 char* dst_p = dst;
3267
3268 for (;;) {
3269 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003270
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003271 /* look for delim. */
3272 p_delim = p;
3273 while (p_delim < p + len && *p_delim != delim)
3274 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003275
Willy Tarreau15a53a42015-01-21 13:39:42 +01003276 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch, 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003277 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3278
3279 if (replace_n < 0)
3280 return -1;
3281
3282 dst_p += replace_n;
3283 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003284 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285
3286 if (dst_p + len >= dst_end)
3287 return -1;
3288
3289 memcpy(dst_p, p, len);
3290 dst_p += len;
3291 }
3292
3293 if (dst_p >= dst_end)
3294 return -1;
3295
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003296 /* end of the replacements. */
3297 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003298 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003299
3300 /* Next part. */
3301 *dst_p++ = delim;
3302 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003303 }
3304
3305 return dst_p - dst;
3306}
3307
3308static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003309 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003310 struct hdr_ctx* ctx, int action)
3311{
3312 ctx->idx = 0;
3313
3314 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3315 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3316 int delta;
Willy Tarreauaa435e72015-01-29 14:01:34 +01003317 char* val = (char*)ctx->line + ctx->val;
Sasha Pachev218f0642014-06-16 12:05:59 -06003318 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003319 char* reg_dst_buf;
3320 uint reg_dst_buf_size;
3321 int n_replaced;
3322
Sasha Pachev218f0642014-06-16 12:05:59 -06003323 trash.len = build_logline(s, trash.str, trash.size, fmt);
3324
3325 if (trash.len >= trash.size - 1)
3326 return -1;
3327
3328 reg_dst_buf = trash.str + trash.len + 1;
3329 reg_dst_buf_size = trash.size - trash.len - 1;
3330
3331 switch (action) {
3332 case HTTP_REQ_ACT_REPLACE_VAL:
3333 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003334 n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003335 break;
3336 case HTTP_REQ_ACT_REPLACE_HDR:
3337 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003338 n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003339 break;
3340 default: /* impossible */
3341 return -1;
3342 }
3343
Sasha Pachev218f0642014-06-16 12:05:59 -06003344 switch (n_replaced) {
3345 case -1: return -1;
3346 case -2: continue;
3347 }
3348
3349 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3350
3351 hdr->len += delta;
3352 http_msg_move_end(msg, delta);
3353 }
3354
3355 return 0;
3356}
3357
Willy Tarreau20b0de52012-12-24 15:45:22 +01003358/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 * transaction <txn>. Returns the verdict of the first rule that prevents
3360 * further processing of the request (auth, deny, ...), and defaults to
3361 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3362 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3363 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003364 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003365enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003366http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003367{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003368 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003369 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003370 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003371 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003372
Willy Tarreauff011f22011-01-06 17:51:27 +01003373 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003374 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003375 continue;
3376
Willy Tarreau96257ec2012-12-27 10:46:37 +01003377 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003378 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003379 int ret;
3380
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003381 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003382 ret = acl_pass(ret);
3383
Willy Tarreauff011f22011-01-06 17:51:27 +01003384 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003385 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003386
3387 if (!ret) /* condition not matched */
3388 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003389 }
3390
Willy Tarreau20b0de52012-12-24 15:45:22 +01003391
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 switch (rule->action) {
3393 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003394 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395
3396 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003397 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398
Willy Tarreauccbcc372012-12-27 12:37:57 +01003399 case HTTP_REQ_ACT_TARPIT:
3400 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003401 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003402
Willy Tarreau96257ec2012-12-27 10:46:37 +01003403 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003404 /* Auth might be performed on regular http-req rules as well as on stats */
3405 auth_realm = rule->arg.auth.realm;
3406 if (!auth_realm) {
3407 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3408 auth_realm = STATS_DEFAULT_REALM;
3409 else
3410 auth_realm = px->id;
3411 }
3412 /* send 401/407 depending on whether we use a proxy or not. We still
3413 * count one error, because normal browsing won't significantly
3414 * increase the counter but brute force attempts will.
3415 */
3416 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3417 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3418 stream_int_retnclose(&s->si[0], &trash);
3419 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003420 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003421
Willy Tarreau81499eb2012-12-27 12:19:02 +01003422 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003423 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3424 return HTTP_RULE_RES_BADREQ;
3425 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003426
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003427 case HTTP_REQ_ACT_SET_NICE:
3428 s->task->nice = rule->arg.nice;
3429 break;
3430
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003431 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003432 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003433 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003434 break;
3435
Willy Tarreau51347ed2013-06-11 19:34:13 +02003436 case HTTP_REQ_ACT_SET_MARK:
3437#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003438 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003439 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003440#endif
3441 break;
3442
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003443 case HTTP_REQ_ACT_SET_LOGL:
3444 s->logs.level = rule->arg.loglevel;
3445 break;
3446
Sasha Pachev218f0642014-06-16 12:05:59 -06003447 case HTTP_REQ_ACT_REPLACE_HDR:
3448 case HTTP_REQ_ACT_REPLACE_VAL:
3449 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3450 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003451 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003452 return HTTP_RULE_RES_BADREQ;
3453 break;
3454
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003455 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456 ctx.idx = 0;
3457 /* remove all occurrences of the header */
3458 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3459 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3460 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003461 }
Willy Tarreau85603282015-01-21 20:39:27 +01003462 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003463
Willy Tarreau85603282015-01-21 20:39:27 +01003464 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465 case HTTP_REQ_ACT_ADD_HDR:
3466 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3467 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3468 trash.len = rule->arg.hdr_add.name_len;
3469 trash.str[trash.len++] = ':';
3470 trash.str[trash.len++] = ' ';
3471 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003472
3473 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3474 /* remove all occurrences of the header */
3475 ctx.idx = 0;
3476 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3477 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3478 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3479 }
3480 }
3481
Willy Tarreau96257ec2012-12-27 10:46:37 +01003482 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3483 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003484
3485 case HTTP_REQ_ACT_DEL_ACL:
3486 case HTTP_REQ_ACT_DEL_MAP: {
3487 struct pat_ref *ref;
3488 char *key;
3489 int len;
3490
3491 /* collect reference */
3492 ref = pat_ref_lookup(rule->arg.map.ref);
3493 if (!ref)
3494 continue;
3495
3496 /* collect key */
3497 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3498 key = trash.str;
3499 key[len] = '\0';
3500
3501 /* perform update */
3502 /* returned code: 1=ok, 0=ko */
3503 pat_ref_delete(ref, key);
3504
3505 break;
3506 }
3507
3508 case HTTP_REQ_ACT_ADD_ACL: {
3509 struct pat_ref *ref;
3510 char *key;
3511 struct chunk *trash_key;
3512 int len;
3513
3514 trash_key = get_trash_chunk();
3515
3516 /* collect reference */
3517 ref = pat_ref_lookup(rule->arg.map.ref);
3518 if (!ref)
3519 continue;
3520
3521 /* collect key */
3522 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3523 key = trash_key->str;
3524 key[len] = '\0';
3525
3526 /* perform update */
3527 /* add entry only if it does not already exist */
3528 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003529 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003530
3531 break;
3532 }
3533
3534 case HTTP_REQ_ACT_SET_MAP: {
3535 struct pat_ref *ref;
3536 char *key, *value;
3537 struct chunk *trash_key, *trash_value;
3538 int len;
3539
3540 trash_key = get_trash_chunk();
3541 trash_value = get_trash_chunk();
3542
3543 /* collect reference */
3544 ref = pat_ref_lookup(rule->arg.map.ref);
3545 if (!ref)
3546 continue;
3547
3548 /* collect key */
3549 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3550 key = trash_key->str;
3551 key[len] = '\0';
3552
3553 /* collect value */
3554 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3555 value = trash_value->str;
3556 value[len] = '\0';
3557
3558 /* perform update */
3559 if (pat_ref_find_elt(ref, key) != NULL)
3560 /* update entry if it exists */
3561 pat_ref_set(ref, key, value, NULL);
3562 else
3563 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003564 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003565
3566 break;
3567 }
William Lallemand73025dd2014-04-24 14:38:37 +02003568
3569 case HTTP_REQ_ACT_CUSTOM_CONT:
3570 rule->action_ptr(rule, px, s, txn);
3571 break;
3572
3573 case HTTP_REQ_ACT_CUSTOM_STOP:
3574 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003575 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003576
3577 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3578 /* Note: only the first valid tracking parameter of each
3579 * applies.
3580 */
3581
3582 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3583 struct stktable *t;
3584 struct stksess *ts;
3585 struct stktable_key *key;
3586 void *ptr;
3587
3588 t = rule->act_prm.trk_ctr.table.t;
3589 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3590
3591 if (key && (ts = stktable_get_entry(t, key))) {
3592 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3593
3594 /* let's count a new HTTP request as it's the first time we do it */
3595 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3596 if (ptr)
3597 stktable_data_cast(ptr, http_req_cnt)++;
3598
3599 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3600 if (ptr)
3601 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3602 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3603
3604 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3605 if (s->fe != s->be)
3606 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3607 }
3608 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003609 }
3610 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003611
3612 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003613 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003614}
3615
Willy Tarreau71241ab2012-12-27 11:30:54 +01003616
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003617/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3618 * transaction <txn>. Returns the first rule that prevents further processing
3619 * of the response (deny, ...) or NULL if it executed all rules or stopped
3620 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3621 * rule.
3622 */
3623static struct http_res_rule *
3624http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3625{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003626 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003627 struct http_res_rule *rule;
3628 struct hdr_ctx ctx;
3629
3630 list_for_each_entry(rule, rules, list) {
3631 if (rule->action >= HTTP_RES_ACT_MAX)
3632 continue;
3633
3634 /* check optional condition */
3635 if (rule->cond) {
3636 int ret;
3637
3638 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3639 ret = acl_pass(ret);
3640
3641 if (rule->cond->pol == ACL_COND_UNLESS)
3642 ret = !ret;
3643
3644 if (!ret) /* condition not matched */
3645 continue;
3646 }
3647
3648
3649 switch (rule->action) {
3650 case HTTP_RES_ACT_ALLOW:
3651 return NULL; /* "allow" rules are OK */
3652
3653 case HTTP_RES_ACT_DENY:
3654 txn->flags |= TX_SVDENY;
3655 return rule;
3656
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003657 case HTTP_RES_ACT_SET_NICE:
3658 s->task->nice = rule->arg.nice;
3659 break;
3660
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003661 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003662 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003663 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003664 break;
3665
Willy Tarreau51347ed2013-06-11 19:34:13 +02003666 case HTTP_RES_ACT_SET_MARK:
3667#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003668 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003669 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003670#endif
3671 break;
3672
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003673 case HTTP_RES_ACT_SET_LOGL:
3674 s->logs.level = rule->arg.loglevel;
3675 break;
3676
Sasha Pachev218f0642014-06-16 12:05:59 -06003677 case HTTP_RES_ACT_REPLACE_HDR:
3678 case HTTP_RES_ACT_REPLACE_VAL:
3679 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3680 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003681 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003682 return NULL; /* note: we should report an error here */
3683 break;
3684
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003685 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003686 ctx.idx = 0;
3687 /* remove all occurrences of the header */
3688 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3689 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3690 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3691 }
Willy Tarreau85603282015-01-21 20:39:27 +01003692 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003693
Willy Tarreau85603282015-01-21 20:39:27 +01003694 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003695 case HTTP_RES_ACT_ADD_HDR:
3696 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3697 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3698 trash.len = rule->arg.hdr_add.name_len;
3699 trash.str[trash.len++] = ':';
3700 trash.str[trash.len++] = ' ';
3701 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 +01003702
3703 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3704 /* remove all occurrences of the header */
3705 ctx.idx = 0;
3706 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3707 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3708 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3709 }
3710 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003711 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3712 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003713
3714 case HTTP_RES_ACT_DEL_ACL:
3715 case HTTP_RES_ACT_DEL_MAP: {
3716 struct pat_ref *ref;
3717 char *key;
3718 int len;
3719
3720 /* collect reference */
3721 ref = pat_ref_lookup(rule->arg.map.ref);
3722 if (!ref)
3723 continue;
3724
3725 /* collect key */
3726 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3727 key = trash.str;
3728 key[len] = '\0';
3729
3730 /* perform update */
3731 /* returned code: 1=ok, 0=ko */
3732 pat_ref_delete(ref, key);
3733
3734 break;
3735 }
3736
3737 case HTTP_RES_ACT_ADD_ACL: {
3738 struct pat_ref *ref;
3739 char *key;
3740 struct chunk *trash_key;
3741 int len;
3742
3743 trash_key = get_trash_chunk();
3744
3745 /* collect reference */
3746 ref = pat_ref_lookup(rule->arg.map.ref);
3747 if (!ref)
3748 continue;
3749
3750 /* collect key */
3751 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3752 key = trash_key->str;
3753 key[len] = '\0';
3754
3755 /* perform update */
3756 /* check if the entry already exists */
3757 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003758 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003759
3760 break;
3761 }
3762
3763 case HTTP_RES_ACT_SET_MAP: {
3764 struct pat_ref *ref;
3765 char *key, *value;
3766 struct chunk *trash_key, *trash_value;
3767 int len;
3768
3769 trash_key = get_trash_chunk();
3770 trash_value = 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 /* collect value */
3783 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3784 value = trash_value->str;
3785 value[len] = '\0';
3786
3787 /* perform update */
3788 if (pat_ref_find_elt(ref, key) != NULL)
3789 /* update entry if it exists */
3790 pat_ref_set(ref, key, value, NULL);
3791 else
3792 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003793 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003794
3795 break;
3796 }
William Lallemand73025dd2014-04-24 14:38:37 +02003797
3798 case HTTP_RES_ACT_CUSTOM_CONT:
3799 rule->action_ptr(rule, px, s, txn);
3800 break;
3801
3802 case HTTP_RES_ACT_CUSTOM_STOP:
3803 rule->action_ptr(rule, px, s, txn);
3804 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805 }
3806 }
3807
3808 /* we reached the end of the rules, nothing to report */
3809 return NULL;
3810}
3811
3812
Willy Tarreau71241ab2012-12-27 11:30:54 +01003813/* Perform an HTTP redirect based on the information in <rule>. The function
3814 * returns non-zero on success, or zero in case of a, irrecoverable error such
3815 * as too large a request to build a valid response.
3816 */
3817static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3818{
3819 struct http_msg *msg = &txn->req;
3820 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003821 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003822
3823 /* build redirect message */
3824 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003825 case 308:
3826 msg_fmt = HTTP_308;
3827 break;
3828 case 307:
3829 msg_fmt = HTTP_307;
3830 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003831 case 303:
3832 msg_fmt = HTTP_303;
3833 break;
3834 case 301:
3835 msg_fmt = HTTP_301;
3836 break;
3837 case 302:
3838 default:
3839 msg_fmt = HTTP_302;
3840 break;
3841 }
3842
3843 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3844 return 0;
3845
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003846 location = trash.str + trash.len;
3847
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848 switch(rule->type) {
3849 case REDIRECT_TYPE_SCHEME: {
3850 const char *path;
3851 const char *host;
3852 struct hdr_ctx ctx;
3853 int pathlen;
3854 int hostlen;
3855
3856 host = "";
3857 hostlen = 0;
3858 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003859 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003860 host = ctx.line + ctx.val;
3861 hostlen = ctx.vlen;
3862 }
3863
3864 path = http_get_path(txn);
3865 /* build message using path */
3866 if (path) {
3867 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3868 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3869 int qs = 0;
3870 while (qs < pathlen) {
3871 if (path[qs] == '?') {
3872 pathlen = qs;
3873 break;
3874 }
3875 qs++;
3876 }
3877 }
3878 } else {
3879 path = "/";
3880 pathlen = 1;
3881 }
3882
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003883 if (rule->rdr_str) { /* this is an old "redirect" rule */
3884 /* check if we can add scheme + "://" + host + path */
3885 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3886 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003887
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003888 /* add scheme */
3889 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3890 trash.len += rule->rdr_len;
3891 }
3892 else {
3893 /* add scheme with executing log format */
3894 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003895
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003896 /* check if we can add scheme + "://" + host + path */
3897 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3898 return 0;
3899 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003900 /* add "://" */
3901 memcpy(trash.str + trash.len, "://", 3);
3902 trash.len += 3;
3903
3904 /* add host */
3905 memcpy(trash.str + trash.len, host, hostlen);
3906 trash.len += hostlen;
3907
3908 /* add path */
3909 memcpy(trash.str + trash.len, path, pathlen);
3910 trash.len += pathlen;
3911
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003912 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003913 if (trash.len && trash.str[trash.len - 1] != '/' &&
3914 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3915 if (trash.len > trash.size - 5)
3916 return 0;
3917 trash.str[trash.len] = '/';
3918 trash.len++;
3919 }
3920
3921 break;
3922 }
3923 case REDIRECT_TYPE_PREFIX: {
3924 const char *path;
3925 int pathlen;
3926
3927 path = http_get_path(txn);
3928 /* build message using path */
3929 if (path) {
3930 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3931 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3932 int qs = 0;
3933 while (qs < pathlen) {
3934 if (path[qs] == '?') {
3935 pathlen = qs;
3936 break;
3937 }
3938 qs++;
3939 }
3940 }
3941 } else {
3942 path = "/";
3943 pathlen = 1;
3944 }
3945
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003946 if (rule->rdr_str) { /* this is an old "redirect" rule */
3947 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3948 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003949
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003950 /* add prefix. Note that if prefix == "/", we don't want to
3951 * add anything, otherwise it makes it hard for the user to
3952 * configure a self-redirection.
3953 */
3954 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3955 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3956 trash.len += rule->rdr_len;
3957 }
3958 }
3959 else {
3960 /* add prefix with executing log format */
3961 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3962
3963 /* Check length */
3964 if (trash.len + pathlen > trash.size - 4)
3965 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 }
3967
3968 /* add path */
3969 memcpy(trash.str + trash.len, path, pathlen);
3970 trash.len += pathlen;
3971
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003972 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003973 if (trash.len && trash.str[trash.len - 1] != '/' &&
3974 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3975 if (trash.len > trash.size - 5)
3976 return 0;
3977 trash.str[trash.len] = '/';
3978 trash.len++;
3979 }
3980
3981 break;
3982 }
3983 case REDIRECT_TYPE_LOCATION:
3984 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003985 if (rule->rdr_str) { /* this is an old "redirect" rule */
3986 if (trash.len + rule->rdr_len > trash.size - 4)
3987 return 0;
3988
3989 /* add location */
3990 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3991 trash.len += rule->rdr_len;
3992 }
3993 else {
3994 /* add location with executing log format */
3995 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003997 /* Check left length */
3998 if (trash.len > trash.size - 4)
3999 return 0;
4000 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001 break;
4002 }
4003
4004 if (rule->cookie_len) {
4005 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4006 trash.len += 14;
4007 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4008 trash.len += rule->cookie_len;
4009 memcpy(trash.str + trash.len, "\r\n", 2);
4010 trash.len += 2;
4011 }
4012
4013 /* add end of headers and the keep-alive/close status.
4014 * We may choose to set keep-alive if the Location begins
4015 * with a slash, because the client will come back to the
4016 * same server.
4017 */
4018 txn->status = rule->code;
4019 /* let's log the request time */
4020 s->logs.tv_request = now;
4021
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004022 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004023 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4024 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4025 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4026 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4027 /* keep-alive possible */
4028 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4029 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4030 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4031 trash.len += 30;
4032 } else {
4033 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4034 trash.len += 24;
4035 }
4036 }
4037 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4038 trash.len += 4;
4039 bo_inject(txn->rsp.chn, trash.str, trash.len);
4040 /* "eat" the request */
4041 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004042 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004043 msg->sov = 0;
4044 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
4045 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
4046 txn->req.msg_state = HTTP_MSG_CLOSED;
4047 txn->rsp.msg_state = HTTP_MSG_DONE;
4048 } else {
4049 /* keep-alive not possible */
4050 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4051 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4052 trash.len += 29;
4053 } else {
4054 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4055 trash.len += 23;
4056 }
4057 stream_int_retnclose(txn->req.chn->prod, &trash);
4058 txn->req.chn->analysers = 0;
4059 }
4060
4061 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02004062 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004063 if (!(s->flags & SN_FINST_MASK))
4064 s->flags |= SN_FINST_R;
4065
4066 return 1;
4067}
4068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004069/* This stream analyser runs all HTTP request processing which is common to
4070 * frontends and backends, which means blocking ACLs, filters, connection-close,
4071 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004072 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004073 * either needs more data or wants to immediately abort the request (eg: deny,
4074 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004075 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004076int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004077{
Willy Tarreaud787e662009-07-07 10:14:51 +02004078 struct http_txn *txn = &s->txn;
4079 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004080 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004081 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004082 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004083
Willy Tarreau655dce92009-11-08 13:10:58 +01004084 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004085 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004086 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004087 return 0;
4088 }
4089
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004090 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 +02004091 now_ms, __FUNCTION__,
4092 s,
4093 req,
4094 req->rex, req->wex,
4095 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004096 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004097 req->analysers);
4098
Willy Tarreau65410832014-04-28 21:25:43 +02004099 /* just in case we have some per-backend tracking */
4100 session_inc_be_http_req_ctr(s);
4101
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004102 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4104 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004105
Willy Tarreau0b748332014-04-29 00:13:29 +02004106 switch (verdict) {
4107 case HTTP_RULE_RES_CONT:
4108 case HTTP_RULE_RES_STOP: /* nothing to do */
4109 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004110
Willy Tarreau0b748332014-04-29 00:13:29 +02004111 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4112 if (txn->flags & TX_CLTARPIT)
4113 goto tarpit;
4114 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004115
Willy Tarreau0b748332014-04-29 00:13:29 +02004116 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4117 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004118
Willy Tarreau0b748332014-04-29 00:13:29 +02004119 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004120 goto done;
4121
Willy Tarreau0b748332014-04-29 00:13:29 +02004122 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4123 goto return_bad_req;
4124 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004125 }
4126
Willy Tarreau52542592014-04-28 18:33:26 +02004127 /* OK at this stage, we know that the request was accepted according to
4128 * the http-request rules, we can check for the stats. Note that the
4129 * URI is detected *before* the req* rules in order not to be affected
4130 * by a possible reqrep, while they are processed *after* so that a
4131 * reqdeny can still block them. This clearly needs to change in 1.6!
4132 */
4133 if (stats_check_uri(s->rep->prod, txn, px)) {
4134 s->target = &http_stats_applet.obj_type;
4135 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4136 txn->status = 500;
4137 s->logs.tv_request = now;
4138 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004139
Willy Tarreau52542592014-04-28 18:33:26 +02004140 if (!(s->flags & SN_ERR_MASK))
4141 s->flags |= SN_ERR_RESOURCE;
4142 goto return_prx_cond;
4143 }
4144
4145 /* parse the whole stats request and extract the relevant information */
4146 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004147 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4148 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004149
Willy Tarreau0b748332014-04-29 00:13:29 +02004150 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4151 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004152
Willy Tarreau0b748332014-04-29 00:13:29 +02004153 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4154 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004155 }
4156
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 /* evaluate the req* rules except reqadd */
4158 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004159 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004160 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004161
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004162 if (txn->flags & TX_CLDENY)
4163 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004164
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165 if (txn->flags & TX_CLTARPIT)
4166 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004167 }
Willy Tarreau06619262006-12-17 08:37:22 +01004168
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004169 /* add request headers from the rule sets in the same order */
4170 list_for_each_entry(wl, &px->req_add, list) {
4171 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004172 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004173 ret = acl_pass(ret);
4174 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4175 ret = !ret;
4176 if (!ret)
4177 continue;
4178 }
4179
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004180 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004181 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004182 }
4183
Willy Tarreau52542592014-04-28 18:33:26 +02004184
4185 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004186 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004187 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004188 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4189 s->fe->fe_counters.intercepted_req++;
4190
4191 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4192 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4193 if (!(s->flags & SN_FINST_MASK))
4194 s->flags |= SN_FINST_R;
4195
Willy Tarreau70730dd2014-04-24 18:06:27 +02004196 /* we may want to compress the stats page */
4197 if (s->fe->comp || s->be->comp)
4198 select_compression_request_header(s, req->buf);
4199
4200 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004201 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004202 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004205 /* check whether we have some ACLs set to redirect this request */
4206 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004207 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004208 int ret;
4209
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004210 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004211 ret = acl_pass(ret);
4212 if (rule->cond->pol == ACL_COND_UNLESS)
4213 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214 if (!ret)
4215 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004216 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004217 if (!http_apply_redirect_rule(rule, s, txn))
4218 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004219 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004220 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004221
Willy Tarreau2be39392010-01-03 17:24:51 +01004222 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4223 * If this happens, then the data will not come immediately, so we must
4224 * send all what we have without waiting. Note that due to the small gain
4225 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004226 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004227 * itself once used.
4228 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004229 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004230
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004231 done: /* done with this analyser, continue with next ones that the calling
4232 * points will have set, if any.
4233 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004235 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4236 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004237 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004238
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004239 tarpit:
4240 /* When a connection is tarpitted, we use the tarpit timeout,
4241 * which may be the same as the connect timeout if unspecified.
4242 * If unset, then set it to zero because we really want it to
4243 * eventually expire. We build the tarpit as an analyser.
4244 */
4245 channel_erase(s->req);
4246
4247 /* wipe the request out so that we can drop the connection early
4248 * if the client closes first.
4249 */
4250 channel_dont_connect(req);
4251 req->analysers = 0; /* remove switching rules etc... */
4252 req->analysers |= AN_REQ_HTTP_TARPIT;
4253 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4254 if (!req->analyse_exp)
4255 req->analyse_exp = tick_add(now_ms, 0);
4256 session_inc_http_err_ctr(s);
4257 s->fe->fe_counters.denied_req++;
4258 if (s->fe != s->be)
4259 s->be->be_counters.denied_req++;
4260 if (s->listener->counters)
4261 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004262 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004263
4264 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004265 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 txn->status = 403;
4267 s->logs.tv_request = now;
4268 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4269 session_inc_http_err_ctr(s);
4270 s->fe->fe_counters.denied_req++;
4271 if (s->fe != s->be)
4272 s->be->be_counters.denied_req++;
4273 if (s->listener->counters)
4274 s->listener->counters->denied_req++;
4275 goto return_prx_cond;
4276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004277 return_bad_req:
4278 /* We centralize bad requests processing here */
4279 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4280 /* we detected a parsing error. We want to archive this request
4281 * in the dedicated proxy area for later troubleshooting.
4282 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004283 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004284 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004285
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004286 txn->req.msg_state = HTTP_MSG_ERROR;
4287 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004288 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004289
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004290 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004291 if (s->listener->counters)
4292 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004293
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 return_prx_cond:
4295 if (!(s->flags & SN_ERR_MASK))
4296 s->flags |= SN_ERR_PRXCOND;
4297 if (!(s->flags & SN_FINST_MASK))
4298 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 req->analysers = 0;
4301 req->analyse_exp = TICK_ETERNITY;
4302 return 0;
4303}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004304
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004305/* This function performs all the processing enabled for the current request.
4306 * It returns 1 if the processing can continue on next analysers, or zero if it
4307 * needs more data, encounters an error, or wants to immediately abort the
4308 * request. It relies on buffers flags, and updates s->req->analysers.
4309 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004310int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004311{
4312 struct http_txn *txn = &s->txn;
4313 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004314 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004315
Willy Tarreau655dce92009-11-08 13:10:58 +01004316 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004317 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004318 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004319 return 0;
4320 }
4321
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004322 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 +02004323 now_ms, __FUNCTION__,
4324 s,
4325 req,
4326 req->rex, req->wex,
4327 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004328 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004329 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004330
William Lallemand82fe75c2012-10-23 10:25:10 +02004331 if (s->fe->comp || s->be->comp)
4332 select_compression_request_header(s, req->buf);
4333
Willy Tarreau59234e92008-11-30 23:51:27 +01004334 /*
4335 * Right now, we know that we have processed the entire headers
4336 * and that unwanted requests have been filtered out. We can do
4337 * whatever we want with the remaining request. Also, now we
4338 * may have separate values for ->fe, ->be.
4339 */
Willy Tarreau06619262006-12-17 08:37:22 +01004340
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004342 * If HTTP PROXY is set we simply get remote server address parsing
4343 * incoming request. Note that this requires that a connection is
4344 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004345 */
4346 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004347 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004348 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004349
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004350 /* Note that for now we don't reuse existing proxy connections */
4351 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004352 txn->req.msg_state = HTTP_MSG_ERROR;
4353 txn->status = 500;
4354 req->analysers = 0;
4355 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4356
4357 if (!(s->flags & SN_ERR_MASK))
4358 s->flags |= SN_ERR_RESOURCE;
4359 if (!(s->flags & SN_FINST_MASK))
4360 s->flags |= SN_FINST_R;
4361
4362 return 0;
4363 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004364
4365 path = http_get_path(txn);
4366 url2sa(req->buf->p + msg->sl.rq.u,
4367 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004368 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004369 /* if the path was found, we have to remove everything between
4370 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4371 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4372 * u_l characters by a single "/".
4373 */
4374 if (path) {
4375 char *cur_ptr = req->buf->p;
4376 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4377 int delta;
4378
4379 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4380 http_msg_move_end(&txn->req, delta);
4381 cur_end += delta;
4382 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4383 goto return_bad_req;
4384 }
4385 else {
4386 char *cur_ptr = req->buf->p;
4387 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4388 int delta;
4389
4390 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4391 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4392 http_msg_move_end(&txn->req, delta);
4393 cur_end += delta;
4394 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4395 goto return_bad_req;
4396 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004398
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004400 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004401 * Note that doing so might move headers in the request, but
4402 * the fields will stay coherent and the URI will not move.
4403 * This should only be performed in the backend.
4404 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004405 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004406 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4407 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004408
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004410 * 8: the appsession cookie was looked up very early in 1.2,
4411 * so let's do the same now.
4412 */
4413
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004414 /* It needs to look into the URI unless persistence must be ignored */
4415 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004416 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 +01004417 }
4418
William Lallemanda73203e2012-03-12 12:48:57 +01004419 /* add unique-id if "header-unique-id" is specified */
4420
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004421 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4422 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4423 goto return_bad_req;
4424 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004425 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004426 }
William Lallemanda73203e2012-03-12 12:48:57 +01004427
4428 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004429 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4430 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004431 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004432 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004433 goto return_bad_req;
4434 }
4435
Cyril Bontéb21570a2009-11-29 20:04:48 +01004436 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004437 * 9: add X-Forwarded-For if either the frontend or the backend
4438 * asks for it.
4439 */
4440 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004441 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004442 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004443 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4444 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004445 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004446 /* The header is set to be added only if none is present
4447 * and we found it, so don't do anything.
4448 */
4449 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004450 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004451 /* Add an X-Forwarded-For header unless the source IP is
4452 * in the 'except' network range.
4453 */
4454 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004455 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004456 != s->fe->except_net.s_addr) &&
4457 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004458 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004459 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004460 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004461 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004462 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004463
4464 /* Note: we rely on the backend to get the header name to be used for
4465 * x-forwarded-for, because the header is really meant for the backends.
4466 * However, if the backend did not specify any option, we have to rely
4467 * on the frontend's header name.
4468 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 if (s->be->fwdfor_hdr_len) {
4470 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004471 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004472 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004474 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004475 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004476 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 +01004477
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004478 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004479 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004480 }
4481 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004482 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004483 /* FIXME: for the sake of completeness, we should also support
4484 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004485 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004486 int len;
4487 char pn[INET6_ADDRSTRLEN];
4488 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004489 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004490 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004491
Willy Tarreau59234e92008-11-30 23:51:27 +01004492 /* Note: we rely on the backend to get the header name to be used for
4493 * x-forwarded-for, because the header is really meant for the backends.
4494 * However, if the backend did not specify any option, we have to rely
4495 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004496 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 if (s->be->fwdfor_hdr_len) {
4498 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004499 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004500 } else {
4501 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004502 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004503 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004504 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004505
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004506 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004507 goto return_bad_req;
4508 }
4509 }
4510
4511 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004512 * 10: add X-Original-To if either the frontend or the backend
4513 * asks for it.
4514 */
4515 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4516
4517 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004518 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004519 /* Add an X-Original-To header unless the destination IP is
4520 * in the 'except' network range.
4521 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004522 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004523
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004524 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004525 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 (((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 +02004527 != s->fe->except_to.s_addr) &&
4528 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004529 (((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 +02004530 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004531 int len;
4532 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004533 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004534
4535 /* Note: we rely on the backend to get the header name to be used for
4536 * x-original-to, because the header is really meant for the backends.
4537 * However, if the backend did not specify any option, we have to rely
4538 * on the frontend's header name.
4539 */
4540 if (s->be->orgto_hdr_len) {
4541 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004542 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004543 } else {
4544 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004546 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004547 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 +02004548
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004550 goto return_bad_req;
4551 }
4552 }
4553 }
4554
Willy Tarreau50fc7772012-11-11 22:19:57 +01004555 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4556 * If an "Upgrade" token is found, the header is left untouched in order not to have
4557 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4558 * "Upgrade" is present in the Connection header.
4559 */
4560 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4561 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004562 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4563 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004564 unsigned int want_flags = 0;
4565
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004566 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004567 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004568 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4569 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004570 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004571 want_flags |= TX_CON_CLO_SET;
4572 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004573 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004574 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4575 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004576 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004577 want_flags |= TX_CON_KAL_SET;
4578 }
4579
4580 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004581 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004582 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004584
Willy Tarreau522d6c02009-12-06 18:49:18 +01004585 /* If we have no server assigned yet and we're balancing on url_param
4586 * with a POST request, we may be interested in checking the body for
4587 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004588 */
4589 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4590 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004591 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004592 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004593 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004594 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004595
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004596 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004597 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004598#ifdef TCP_QUICKACK
4599 /* We expect some data from the client. Unless we know for sure
4600 * we already have a full request, we have to re-enable quick-ack
4601 * in case we previously disabled it, otherwise we might cause
4602 * the client to delay further data.
4603 */
4604 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004605 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004606 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004607 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004608 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004609#endif
4610 }
Willy Tarreau03945942009-12-22 16:50:27 +01004611
Willy Tarreau59234e92008-11-30 23:51:27 +01004612 /*************************************************************
4613 * OK, that's finished for the headers. We have done what we *
4614 * could. Let's switch to the DATA state. *
4615 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004616 req->analyse_exp = TICK_ETERNITY;
4617 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004618
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004619 /* if the server closes the connection, we want to immediately react
4620 * and close the socket to save packets and syscalls.
4621 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004622 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4623 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004624
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 /* OK let's go on with the BODY now */
4627 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004628
Willy Tarreau59234e92008-11-30 23:51:27 +01004629 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004630 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004631 /* we detected a parsing error. We want to archive this request
4632 * in the dedicated proxy area for later troubleshooting.
4633 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004634 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004635 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004636
Willy Tarreau59234e92008-11-30 23:51:27 +01004637 txn->req.msg_state = HTTP_MSG_ERROR;
4638 txn->status = 400;
4639 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004640 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004641
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004642 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004643 if (s->listener->counters)
4644 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004645
Willy Tarreau59234e92008-11-30 23:51:27 +01004646 if (!(s->flags & SN_ERR_MASK))
4647 s->flags |= SN_ERR_PRXCOND;
4648 if (!(s->flags & SN_FINST_MASK))
4649 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004650 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004651}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004652
Willy Tarreau60b85b02008-11-30 23:28:40 +01004653/* This function is an analyser which processes the HTTP tarpit. It always
4654 * returns zero, at the beginning because it prevents any other processing
4655 * from occurring, and at the end because it terminates the request.
4656 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004657int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004658{
4659 struct http_txn *txn = &s->txn;
4660
4661 /* This connection is being tarpitted. The CLIENT side has
4662 * already set the connect expiration date to the right
4663 * timeout. We just have to check that the client is still
4664 * there and that the timeout has not expired.
4665 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004666 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004667 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004668 !tick_is_expired(req->analyse_exp, now_ms))
4669 return 0;
4670
4671 /* We will set the queue timer to the time spent, just for
4672 * logging purposes. We fake a 500 server error, so that the
4673 * attacker will not suspect his connection has been tarpitted.
4674 * It will not cause trouble to the logs because we can exclude
4675 * the tarpitted connections by filtering on the 'PT' status flags.
4676 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004677 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4678
4679 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004680 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004681 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004682
4683 req->analysers = 0;
4684 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004685
Willy Tarreau60b85b02008-11-30 23:28:40 +01004686 if (!(s->flags & SN_ERR_MASK))
4687 s->flags |= SN_ERR_PRXCOND;
4688 if (!(s->flags & SN_FINST_MASK))
4689 s->flags |= SN_FINST_T;
4690 return 0;
4691}
4692
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004693/* This function is an analyser which waits for the HTTP request body. It waits
4694 * for either the buffer to be full, or the full advertised contents to have
4695 * reached the buffer. It must only be called after the standard HTTP request
4696 * processing has occurred, because it expects the request to be parsed and will
4697 * look for the Expect header. It may send a 100-Continue interim response. It
4698 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4699 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4700 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004701 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004702int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004703{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004704 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004705 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004706
4707 /* We have to parse the HTTP request body to find any required data.
4708 * "balance url_param check_post" should have been the only way to get
4709 * into this. We were brought here after HTTP header analysis, so all
4710 * related structures are ready.
4711 */
4712
Willy Tarreau890988f2014-04-10 11:59:33 +02004713 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4714 /* This is the first call */
4715 if (msg->msg_state < HTTP_MSG_BODY)
4716 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717
Willy Tarreau890988f2014-04-10 11:59:33 +02004718 if (msg->msg_state < HTTP_MSG_100_SENT) {
4719 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4720 * send an HTTP/1.1 100 Continue intermediate response.
4721 */
4722 if (msg->flags & HTTP_MSGF_VER_11) {
4723 struct hdr_ctx ctx;
4724 ctx.idx = 0;
4725 /* Expect is allowed in 1.1, look for it */
4726 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4727 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4728 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4729 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004730 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004731 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004732 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004733
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004734 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004735 * req->buf->p still points to the beginning of the message. We
4736 * must save the body in msg->next because it survives buffer
4737 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004738 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004739 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004740
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004741 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004742 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4743 else
4744 msg->msg_state = HTTP_MSG_DATA;
4745 }
4746
Willy Tarreau890988f2014-04-10 11:59:33 +02004747 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4748 /* We're in content-length mode, we just have to wait for enough data. */
4749 if (req->buf->i - msg->sov < msg->body_len)
4750 goto missing_data;
4751
4752 /* OK we have everything we need now */
4753 goto http_end;
4754 }
4755
4756 /* OK here we're parsing a chunked-encoded message */
4757
Willy Tarreau522d6c02009-12-06 18:49:18 +01004758 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004759 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004760 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004761 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004762 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004763 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004764
Willy Tarreau115acb92009-12-26 13:56:06 +01004765 if (!ret)
4766 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004767 else if (ret < 0) {
4768 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004769 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004770 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004771 }
4772
Willy Tarreaud98cf932009-12-27 22:54:55 +01004773 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004774 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004775 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004776 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004777 if (msg->msg_state == HTTP_MSG_TRAILERS)
4778 goto http_end;
4779
Willy Tarreau226071e2014-04-10 11:55:45 +02004780 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004781 goto http_end;
4782
4783 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004784 /* we get here if we need to wait for more data. If the buffer is full,
4785 * we have the maximum we can expect.
4786 */
4787 if (buffer_full(req->buf, global.tune.maxrewrite))
4788 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004789
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004790 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004792 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004793
4794 if (!(s->flags & SN_ERR_MASK))
4795 s->flags |= SN_ERR_CLITO;
4796 if (!(s->flags & SN_FINST_MASK))
4797 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004798 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004799 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004800
4801 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004802 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004803 /* Not enough data. We'll re-use the http-request
4804 * timeout here. Ideally, we should set the timeout
4805 * relative to the accept() date. We just set the
4806 * request timeout once at the beginning of the
4807 * request.
4808 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004809 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004810 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004811 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004812 return 0;
4813 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004814
4815 http_end:
4816 /* The situation will not evolve, so let's give up on the analysis. */
4817 s->logs.tv_request = now; /* update the request timer to reflect full request */
4818 req->analysers &= ~an_bit;
4819 req->analyse_exp = TICK_ETERNITY;
4820 return 1;
4821
4822 return_bad_req: /* let's centralize all bad requests */
4823 txn->req.msg_state = HTTP_MSG_ERROR;
4824 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004825 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004826
Willy Tarreau79ebac62010-06-07 13:47:49 +02004827 if (!(s->flags & SN_ERR_MASK))
4828 s->flags |= SN_ERR_PRXCOND;
4829 if (!(s->flags & SN_FINST_MASK))
4830 s->flags |= SN_FINST_R;
4831
Willy Tarreau522d6c02009-12-06 18:49:18 +01004832 return_err_msg:
4833 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004834 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004835 if (s->listener->counters)
4836 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004837 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004838}
4839
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004840/* send a server's name with an outgoing request over an established connection.
4841 * Note: this function is designed to be called once the request has been scheduled
4842 * for being forwarded. This is the reason why it rewinds the buffer before
4843 * proceeding.
4844 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004845int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004846
4847 struct hdr_ctx ctx;
4848
Mark Lamourinec2247f02012-01-04 13:02:01 -05004849 char *hdr_name = be->server_id_hdr_name;
4850 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004851 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004852 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004853 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004854
William Lallemandd9e90662012-01-30 17:27:17 +01004855 ctx.idx = 0;
4856
Willy Tarreau211cdec2014-04-17 20:18:08 +02004857 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004858 if (old_o) {
4859 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004860 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004861 txn->req.next += old_o;
4862 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004863 }
4864
Willy Tarreau9b28e032012-10-12 23:49:43 +02004865 old_i = chn->buf->i;
4866 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 -05004867 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004868 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004869 }
4870
4871 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004872 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004873 memcpy(hdr_val, hdr_name, hdr_name_len);
4874 hdr_val += hdr_name_len;
4875 *hdr_val++ = ':';
4876 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004877 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4878 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004879
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004880 if (old_o) {
4881 /* If this was a forwarded request, we must readjust the amount of
4882 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004883 * variations. Note that the current state is >= HTTP_MSG_BODY,
4884 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004885 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004886 old_o += chn->buf->i - old_i;
4887 b_adv(chn->buf, old_o);
4888 txn->req.next -= old_o;
4889 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004890 }
4891
Mark Lamourinec2247f02012-01-04 13:02:01 -05004892 return 0;
4893}
4894
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895/* Terminate current transaction and prepare a new one. This is very tricky
4896 * right now but it works.
4897 */
4898void http_end_txn_clean_session(struct session *s)
4899{
Willy Tarreau068621e2013-12-23 15:11:25 +01004900 int prev_status = s->txn.status;
4901
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 /* FIXME: We need a more portable way of releasing a backend's and a
4903 * server's connections. We need a safer way to reinitialize buffer
4904 * flags. We also need a more accurate method for computing per-request
4905 * data.
4906 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907
Willy Tarreau4213a112013-12-15 10:25:42 +01004908 /* unless we're doing keep-alive, we want to quickly close the connection
4909 * to the server.
4910 */
4911 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4912 !si_conn_ready(s->req->cons)) {
4913 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4914 si_shutr(s->req->cons);
4915 si_shutw(s->req->cons);
4916 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004917
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004918 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004920 if (unlikely(s->srv_conn))
4921 sess_change_server(s, NULL);
4922 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923
4924 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4925 session_process_counters(s);
4926
4927 if (s->txn.status) {
4928 int n;
4929
4930 n = s->txn.status / 100;
4931 if (n < 1 || n > 5)
4932 n = 0;
4933
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004934 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004935 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004936 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004937 s->fe->fe_counters.p.http.comp_rsp++;
4938 }
Willy Tarreau24657792010-02-26 10:30:28 +01004939 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004940 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004941 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004942 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004943 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004944 s->be->be_counters.p.http.comp_rsp++;
4945 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946 }
4947
4948 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004949 s->logs.bytes_in -= s->req->buf->i;
4950 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951
4952 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004953 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004954 !(s->flags & SN_MONITOR) &&
4955 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4956 s->do_log(s);
4957 }
4958
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004959 /* stop tracking content-based counters */
4960 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004961 session_update_time_stats(s);
4962
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 s->logs.accept_date = date; /* user-visible date for logging */
4964 s->logs.tv_accept = now; /* corrected date for internal use */
4965 tv_zero(&s->logs.tv_request);
4966 s->logs.t_queue = -1;
4967 s->logs.t_connect = -1;
4968 s->logs.t_data = -1;
4969 s->logs.t_close = 0;
4970 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4971 s->logs.srv_queue_size = 0; /* we will get this number soon */
4972
Willy Tarreau9b28e032012-10-12 23:49:43 +02004973 s->logs.bytes_in = s->req->total = s->req->buf->i;
4974 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975
4976 if (s->pend_pos)
4977 pendconn_free(s->pend_pos);
4978
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004979 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980 if (s->flags & SN_CURR_SESS) {
4981 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004982 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004983 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004984 if (may_dequeue_tasks(objt_server(s->target), s->be))
4985 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 }
4987
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004988 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989
Willy Tarreau4213a112013-12-15 10:25:42 +01004990 /* only release our endpoint if we don't intend to reuse the
4991 * connection.
4992 */
4993 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4994 !si_conn_ready(s->req->cons)) {
4995 si_release_endpoint(s->req->cons);
4996 }
4997
Willy Tarreau610ecce2010-01-04 21:15:02 +01004998 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02005000 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01005002 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005003 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);
5004 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005005 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 +01005006 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02005007 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005008
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 s->txn.meth = 0;
5010 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005011 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005012
5013 if (prev_status == 401 || prev_status == 407) {
5014 /* In HTTP keep-alive mode, if we receive a 401, we still have
5015 * a chance of being able to send the visitor again to the same
5016 * server over the same connection. This is required by some
5017 * broken protocols such as NTLM, and anyway whenever there is
5018 * an opportunity for sending the challenge to the proper place,
5019 * it's better to do it (at least it helps with debugging).
5020 */
5021 s->txn.flags |= TX_PREFER_LAST;
5022 }
5023
Willy Tarreauee55dc02010-06-01 10:56:34 +02005024 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025 s->req->cons->flags |= SI_FL_INDEP_STR;
5026
Willy Tarreau96e31212011-05-30 18:10:30 +02005027 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005028 s->req->flags |= CF_NEVER_WAIT;
5029 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005030 }
5031
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 /* if the request buffer is not empty, it means we're
5033 * about to process another request, so send pending
5034 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005035 * Just don't do this if the buffer is close to be full,
5036 * because the request will wait for it to flush a little
5037 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005039 if (s->req->buf->i) {
5040 if (s->rep->buf->o &&
5041 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
5042 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005043 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005044 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005045
5046 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005047 channel_auto_read(s->req);
5048 channel_auto_close(s->req);
5049 channel_auto_read(s->rep);
5050 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005051
Willy Tarreau27375622013-12-17 00:00:28 +01005052 /* we're in keep-alive with an idle connection, monitor it */
5053 si_idle_conn(s->req->cons);
5054
Willy Tarreau342b11c2010-11-24 16:22:09 +01005055 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005056 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057}
5058
5059
5060/* This function updates the request state machine according to the response
5061 * state machine and buffer flags. It returns 1 if it changes anything (flag
5062 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5063 * it is only used to find when a request/response couple is complete. Both
5064 * this function and its equivalent should loop until both return zero. It
5065 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5066 */
5067int http_sync_req_state(struct session *s)
5068{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005069 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005070 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005071 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072 unsigned int old_state = txn->req.msg_state;
5073
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5075 return 0;
5076
5077 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005078 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005079 * We can shut the read side unless we want to abort_on_close,
5080 * or we have a POST request. The issue with POST requests is
5081 * that some browsers still send a CRLF after the request, and
5082 * this CRLF must be read so that it does not remain in the kernel
5083 * buffers, otherwise a close could cause an RST on some systems
5084 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005085 * Note that if we're using keep-alive on the client side, we'd
5086 * rather poll now and keep the polling enabled for the whole
5087 * session's life than enabling/disabling it between each
5088 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005089 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005090 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5091 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5092 !(s->be->options & PR_O_ABRT_CLOSE) &&
5093 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005094 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095
Willy Tarreau40f151a2012-12-20 12:10:09 +01005096 /* if the server closes the connection, we want to immediately react
5097 * and close the socket to save packets and syscalls.
5098 */
5099 chn->cons->flags |= SI_FL_NOHALF;
5100
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5102 goto wait_other_side;
5103
5104 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5105 /* The server has not finished to respond, so we
5106 * don't want to move in order not to upset it.
5107 */
5108 goto wait_other_side;
5109 }
5110
5111 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5112 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005113 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005115 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116 goto wait_other_side;
5117 }
5118
5119 /* When we get here, it means that both the request and the
5120 * response have finished receiving. Depending on the connection
5121 * mode, we'll have to wait for the last bytes to leave in either
5122 * direction, and sometimes for a close to be effective.
5123 */
5124
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005125 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5126 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005127 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5128 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005129 }
5130 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5131 /* Option forceclose is set, or either side wants to close,
5132 * let's enforce it now that we're not expecting any new
5133 * data to come. The caller knows the session is complete
5134 * once both states are CLOSED.
5135 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005136 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5137 channel_shutr_now(chn);
5138 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005140 }
5141 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005142 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5143 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005144 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005145 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5146 channel_auto_read(chn);
5147 txn->req.msg_state = HTTP_MSG_TUNNEL;
5148 chn->flags |= CF_NEVER_WAIT;
5149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150 }
5151
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005152 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005154 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005155
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005156 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 txn->req.msg_state = HTTP_MSG_CLOSING;
5158 goto http_msg_closing;
5159 }
5160 else {
5161 txn->req.msg_state = HTTP_MSG_CLOSED;
5162 goto http_msg_closed;
5163 }
5164 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005165 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 }
5167
5168 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5169 http_msg_closing:
5170 /* nothing else to forward, just waiting for the output buffer
5171 * to be empty and for the shutw_now to take effect.
5172 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005173 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 txn->req.msg_state = HTTP_MSG_CLOSED;
5175 goto http_msg_closed;
5176 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005177 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 txn->req.msg_state = HTTP_MSG_ERROR;
5179 goto wait_other_side;
5180 }
5181 }
5182
5183 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5184 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005185 /* see above in MSG_DONE why we only do this in these states */
5186 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5187 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5188 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005189 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005190 goto wait_other_side;
5191 }
5192
5193 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005194 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195}
5196
5197
5198/* This function updates the response state machine according to the request
5199 * state machine and buffer flags. It returns 1 if it changes anything (flag
5200 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5201 * it is only used to find when a request/response couple is complete. Both
5202 * this function and its equivalent should loop until both return zero. It
5203 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5204 */
5205int http_sync_res_state(struct session *s)
5206{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005207 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005209 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 unsigned int old_state = txn->rsp.msg_state;
5211
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5213 return 0;
5214
5215 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5216 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005217 * still monitor the server connection for a possible close
5218 * while the request is being uploaded, so we don't disable
5219 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005221 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222
5223 if (txn->req.msg_state == HTTP_MSG_ERROR)
5224 goto wait_other_side;
5225
5226 if (txn->req.msg_state < HTTP_MSG_DONE) {
5227 /* The client seems to still be sending data, probably
5228 * because we got an error response during an upload.
5229 * We have the choice of either breaking the connection
5230 * or letting it pass through. Let's do the later.
5231 */
5232 goto wait_other_side;
5233 }
5234
5235 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5236 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005237 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005239 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240 goto wait_other_side;
5241 }
5242
5243 /* When we get here, it means that both the request and the
5244 * response have finished receiving. Depending on the connection
5245 * mode, we'll have to wait for the last bytes to leave in either
5246 * direction, and sometimes for a close to be effective.
5247 */
5248
5249 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5250 /* Server-close mode : shut read and wait for the request
5251 * side to close its output buffer. The caller will detect
5252 * when we're in DONE and the other is in CLOSED and will
5253 * catch that for the final cleanup.
5254 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005255 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5256 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005257 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005258 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5259 /* Option forceclose is set, or either side wants to close,
5260 * let's enforce it now that we're not expecting any new
5261 * data to come. The caller knows the session is complete
5262 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005264 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5265 channel_shutr_now(chn);
5266 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005267 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 }
5269 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005270 /* The last possible modes are keep-alive and tunnel. Tunnel will
5271 * need to forward remaining data. Keep-alive will need to monitor
5272 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005274 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005275 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005276 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5277 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 }
5279
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005280 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005281 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005282 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5284 goto http_msg_closing;
5285 }
5286 else {
5287 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5288 goto http_msg_closed;
5289 }
5290 }
5291 goto wait_other_side;
5292 }
5293
5294 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5295 http_msg_closing:
5296 /* nothing else to forward, just waiting for the output buffer
5297 * to be empty and for the shutw_now to take effect.
5298 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005299 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5301 goto http_msg_closed;
5302 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005305 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005306 if (objt_server(s->target))
5307 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 goto wait_other_side;
5309 }
5310 }
5311
5312 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5313 http_msg_closed:
5314 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005315 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005316 channel_auto_close(chn);
5317 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 goto wait_other_side;
5319 }
5320
5321 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005322 /* We force the response to leave immediately if we're waiting for the
5323 * other side, since there is no pending shutdown to push it out.
5324 */
5325 if (!channel_is_empty(chn))
5326 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005327 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005328}
5329
5330
5331/* Resync the request and response state machines. Return 1 if either state
5332 * changes.
5333 */
5334int http_resync_states(struct session *s)
5335{
5336 struct http_txn *txn = &s->txn;
5337 int old_req_state = txn->req.msg_state;
5338 int old_res_state = txn->rsp.msg_state;
5339
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 http_sync_req_state(s);
5341 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 if (!http_sync_res_state(s))
5343 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 if (!http_sync_req_state(s))
5345 break;
5346 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005347
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 /* OK, both state machines agree on a compatible state.
5349 * There are a few cases we're interested in :
5350 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5351 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5352 * directions, so let's simply disable both analysers.
5353 * - HTTP_MSG_CLOSED on the response only means we must abort the
5354 * request.
5355 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5356 * with server-close mode means we've completed one request and we
5357 * must re-initialize the server connection.
5358 */
5359
5360 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5361 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5362 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5363 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5364 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005365 channel_auto_close(s->req);
5366 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005368 channel_auto_close(s->rep);
5369 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005371 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5372 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005373 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005374 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005375 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005376 channel_auto_close(s->rep);
5377 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005378 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005379 channel_abort(s->req);
5380 channel_auto_close(s->req);
5381 channel_auto_read(s->req);
Willy Tarreau319f7452015-01-14 20:32:59 +01005382 channel_truncate(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005384 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5385 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005386 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005387 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5388 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5389 /* server-close/keep-alive: terminate this transaction,
5390 * possibly killing the server connection and reinitialize
5391 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 */
5393 http_end_txn_clean_session(s);
5394 }
5395
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 return txn->req.msg_state != old_req_state ||
5397 txn->rsp.msg_state != old_res_state;
5398}
5399
Willy Tarreaud98cf932009-12-27 22:54:55 +01005400/* This function is an analyser which forwards request body (including chunk
5401 * sizes if any). It is called as soon as we must forward, even if we forward
5402 * zero byte. The only situation where it must not be called is when we're in
5403 * tunnel mode and we want to forward till the close. It's used both to forward
5404 * remaining data and to resync after end of body. It expects the msg_state to
5405 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5406 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005407 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005408 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005409 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005410int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411{
5412 struct http_txn *txn = &s->txn;
5413 struct http_msg *msg = &s->txn.req;
5414
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005415 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5416 return 0;
5417
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005418 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005419 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005420 /* Output closed while we were sending data. We must abort and
5421 * wake the other side up.
5422 */
5423 msg->msg_state = HTTP_MSG_ERROR;
5424 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005425 return 1;
5426 }
5427
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 /* Note that we don't have to send 100-continue back because we don't
5429 * need the data to complete our job, and it's up to the server to
5430 * decide whether to return 100, 417 or anything else in return of
5431 * an "Expect: 100-continue" header.
5432 */
5433
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005434 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005435 /* we have msg->sov which points to the first byte of message
5436 * body, and req->buf.p still points to the beginning of the
5437 * message. We forward the headers now, as we don't need them
5438 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005440 b_adv(req->buf, msg->sov);
5441 msg->next -= msg->sov;
5442 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005443
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005444 /* The previous analysers guarantee that the state is somewhere
5445 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5446 * msg->next are always correct.
5447 */
5448 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5449 if (msg->flags & HTTP_MSGF_TE_CHNK)
5450 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5451 else
5452 msg->msg_state = HTTP_MSG_DATA;
5453 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 }
5455
Willy Tarreau7ba23542014-04-17 21:50:00 +02005456 /* Some post-connect processing might want us to refrain from starting to
5457 * forward data. Currently, the only reason for this is "balance url_param"
5458 * whichs need to parse/process the request after we've enabled forwarding.
5459 */
5460 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5461 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5462 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005463 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005464 goto missing_data;
5465 }
5466 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5467 }
5468
Willy Tarreau80a92c02014-03-12 10:41:13 +01005469 /* in most states, we should abort in case of early close */
5470 channel_auto_close(req);
5471
Willy Tarreauefdf0942014-04-24 20:08:57 +02005472 if (req->to_forward) {
5473 /* We can't process the buffer's contents yet */
5474 req->flags |= CF_WAKE_WRITE;
5475 goto missing_data;
5476 }
5477
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005479 if (msg->msg_state == HTTP_MSG_DATA) {
5480 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005481 /* we may have some pending data starting at req->buf->p */
5482 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005483 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005484 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005485 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005486 msg->next += msg->chunk_len;
5487 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005488
5489 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005490 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005491 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005493 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005494 }
5495 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005496 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005497 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498 * TRAILERS state.
5499 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005500 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501
Willy Tarreau54d23df2012-10-25 19:04:45 +02005502 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005504 else if (ret < 0) {
5505 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005506 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005507 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005508 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005509 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005512 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005514 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515
5516 if (ret == 0)
5517 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005518 else if (ret < 0) {
5519 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005520 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005521 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005523 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we're in MSG_CHUNK_SIZE now */
5525 }
5526 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005527 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528
5529 if (ret == 0)
5530 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005531 else if (ret < 0) {
5532 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005533 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005534 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005535 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005536 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537 /* we're in HTTP_MSG_DONE now */
5538 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005540 int old_state = msg->msg_state;
5541
Willy Tarreau610ecce2010-01-04 21:15:02 +01005542 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005543
5544 /* we may have some pending data starting at req->buf->p
5545 * such as last chunk of data or trailers.
5546 */
5547 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005548 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005549 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005550 msg->next = 0;
5551
Willy Tarreau4fe41902010-06-07 22:27:41 +02005552 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005553 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5554 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005555 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 if (http_resync_states(s)) {
5557 /* some state changes occurred, maybe the analyser
5558 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005559 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005560 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005561 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005562 /* request errors are most likely due to
5563 * the server aborting the transfer.
5564 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005565 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005566 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005567 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005568 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005570 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005572 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005573
5574 /* If "option abortonclose" is set on the backend, we
5575 * want to monitor the client's connection and forward
5576 * any shutdown notification to the server, which will
5577 * decide whether to close or to go on processing the
5578 * request.
5579 */
5580 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005581 channel_auto_read(req);
5582 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005583 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005584 else if (s->txn.meth == HTTP_METH_POST) {
5585 /* POST requests may require to read extra CRLF
5586 * sent by broken browsers and which could cause
5587 * an RST to be sent upon close on some systems
5588 * (eg: Linux).
5589 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005590 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005591 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005592
Willy Tarreau610ecce2010-01-04 21:15:02 +01005593 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594 }
5595 }
5596
Willy Tarreaud98cf932009-12-27 22:54:55 +01005597 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005598 /* we may have some pending data starting at req->buf->p */
5599 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005600 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005601 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5602
Willy Tarreaubed410e2014-04-22 08:19:34 +02005603 msg->next = 0;
5604 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5605
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005606 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005607 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005608 if (!(s->flags & SN_ERR_MASK))
5609 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005610 if (!(s->flags & SN_FINST_MASK)) {
5611 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5612 s->flags |= SN_FINST_H;
5613 else
5614 s->flags |= SN_FINST_D;
5615 }
5616
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005617 s->fe->fe_counters.cli_aborts++;
5618 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005619 if (objt_server(s->target))
5620 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621
5622 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005623 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005624
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005625 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005626 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005627 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005629 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005630 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005631 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005632 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005633 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005634
Willy Tarreau5c620922011-05-11 19:56:11 +02005635 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005636 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005637 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005638 * modes are already handled by the stream sock layer. We must not do
5639 * this in content-length mode because it could present the MSG_MORE
5640 * flag with the last block of forwarded data, which would cause an
5641 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005642 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005643 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005644 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005645
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005646 return 0;
5647
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005649 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005650 if (s->listener->counters)
5651 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005652
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005653 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005654 /* we may have some pending data starting at req->buf->p */
5655 b_adv(req->buf, msg->next);
5656 msg->next = 0;
5657
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005658 txn->req.msg_state = HTTP_MSG_ERROR;
5659 if (txn->status) {
5660 /* Note: we don't send any error if some data were already sent */
5661 stream_int_retnclose(req->prod, NULL);
5662 } else {
5663 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005664 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005665 }
5666 req->analysers = 0;
5667 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005668
5669 if (!(s->flags & SN_ERR_MASK))
5670 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005671 if (!(s->flags & SN_FINST_MASK)) {
5672 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5673 s->flags |= SN_FINST_H;
5674 else
5675 s->flags |= SN_FINST_D;
5676 }
5677 return 0;
5678
5679 aborted_xfer:
5680 txn->req.msg_state = HTTP_MSG_ERROR;
5681 if (txn->status) {
5682 /* Note: we don't send any error if some data were already sent */
5683 stream_int_retnclose(req->prod, NULL);
5684 } else {
5685 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005686 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005687 }
5688 req->analysers = 0;
5689 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5690
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005691 s->fe->fe_counters.srv_aborts++;
5692 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005693 if (objt_server(s->target))
5694 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005695
5696 if (!(s->flags & SN_ERR_MASK))
5697 s->flags |= SN_ERR_SRVCL;
5698 if (!(s->flags & SN_FINST_MASK)) {
5699 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5700 s->flags |= SN_FINST_H;
5701 else
5702 s->flags |= SN_FINST_D;
5703 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005704 return 0;
5705}
5706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5708 * processing can continue on next analysers, or zero if it either needs more
5709 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5710 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5711 * when it has nothing left to do, and may remove any analyser when it wants to
5712 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005713 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005714int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005715{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005716 struct http_txn *txn = &s->txn;
5717 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005718 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005719 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005720 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005721 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005722
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005723 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 +02005724 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005726 rep,
5727 rep->rex, rep->wex,
5728 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005729 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005730 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005731
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005732 /*
5733 * Now parse the partial (or complete) lines.
5734 * We will check the response syntax, and also join multi-line
5735 * headers. An index of all the lines will be elaborated while
5736 * parsing.
5737 *
5738 * For the parsing, we use a 28 states FSM.
5739 *
5740 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005741 * rep->buf->p = beginning of response
5742 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5743 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005744 * msg->eol = end of current header or line (LF or CRLF)
5745 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746 */
5747
Willy Tarreau628c40c2014-04-24 19:11:26 +02005748 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005749 /* There's a protected area at the end of the buffer for rewriting
5750 * purposes. We don't want to start to parse the request if the
5751 * protected area is affected, because we may have to move processed
5752 * data later, which is much more complicated.
5753 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005754 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005755 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005756 /* some data has still not left the buffer, wake us once that's done */
5757 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5758 goto abort_response;
5759 channel_dont_close(rep);
5760 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005761 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005762 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005763 }
5764
Willy Tarreau379357a2013-06-08 12:55:46 +02005765 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5766 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5767 buffer_slow_realign(rep->buf);
5768
Willy Tarreau9b28e032012-10-12 23:49:43 +02005769 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005770 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005771 }
5772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773 /* 1: we might have to print this header in debug mode */
5774 if (unlikely((global.mode & MODE_DEBUG) &&
5775 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005776 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005777 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005778
Willy Tarreau9b28e032012-10-12 23:49:43 +02005779 sol = rep->buf->p;
5780 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005782
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 sol += hdr_idx_first_pos(&txn->hdr_idx);
5784 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 while (cur_idx) {
5787 eol = sol + txn->hdr_idx.v[cur_idx].len;
5788 debug_hdr("srvhdr", s, sol, eol);
5789 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5790 cur_idx = txn->hdr_idx.v[cur_idx].next;
5791 }
5792 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005794 /*
5795 * Now we quickly check if we have found a full valid response.
5796 * If not so, we check the FD and buffer states before leaving.
5797 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005798 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005799 * responses are checked first.
5800 *
5801 * Depending on whether the client is still there or not, we
5802 * may send an error response back or not. Note that normally
5803 * we should only check for HTTP status there, and check I/O
5804 * errors somewhere else.
5805 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806
Willy Tarreau655dce92009-11-08 13:10:58 +01005807 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005808 /* Invalid response */
5809 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5810 /* we detected a parsing error. We want to archive this response
5811 * in the dedicated proxy area for later troubleshooting.
5812 */
5813 hdr_response_bad:
5814 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005815 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005817 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005818 if (objt_server(s->target)) {
5819 objt_server(s->target)->counters.failed_resp++;
5820 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005821 }
Willy Tarreau64648412010-03-05 10:41:54 +01005822 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005823 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005824 rep->analysers = 0;
5825 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005826 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005827 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005828 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005829
5830 if (!(s->flags & SN_ERR_MASK))
5831 s->flags |= SN_ERR_PRXCOND;
5832 if (!(s->flags & SN_FINST_MASK))
5833 s->flags |= SN_FINST_H;
5834
5835 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005836 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005837
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005838 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005839 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005840 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005841 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005842 goto hdr_response_bad;
5843 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005844
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005846 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005847 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005848 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005849 else if (txn->flags & TX_NOT_FIRST)
5850 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005851
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005852 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005853 if (objt_server(s->target)) {
5854 objt_server(s->target)->counters.failed_resp++;
5855 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005856 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005857
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005858 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005859 rep->analysers = 0;
5860 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005861 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005862 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005863 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005864
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 if (!(s->flags & SN_ERR_MASK))
5866 s->flags |= SN_ERR_SRVCL;
5867 if (!(s->flags & SN_FINST_MASK))
5868 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005869 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005870 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005871
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005872 /* read timeout : return a 504 to the client. */
5873 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005875 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005876 else if (txn->flags & TX_NOT_FIRST)
5877 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005878
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005879 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005880 if (objt_server(s->target)) {
5881 objt_server(s->target)->counters.failed_resp++;
5882 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005883 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005884
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005885 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 rep->analysers = 0;
5887 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005888 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005889 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005890 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005891
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 if (!(s->flags & SN_ERR_MASK))
5893 s->flags |= SN_ERR_SRVTO;
5894 if (!(s->flags & SN_FINST_MASK))
5895 s->flags |= SN_FINST_H;
5896 return 0;
5897 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005898
Willy Tarreauf003d372012-11-26 13:35:37 +01005899 /* client abort with an abortonclose */
5900 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5901 s->fe->fe_counters.cli_aborts++;
5902 s->be->be_counters.cli_aborts++;
5903 if (objt_server(s->target))
5904 objt_server(s->target)->counters.cli_aborts++;
5905
5906 rep->analysers = 0;
5907 channel_auto_close(rep);
5908
5909 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005910 channel_truncate(rep);
Willy Tarreauf003d372012-11-26 13:35:37 +01005911 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5912
5913 if (!(s->flags & SN_ERR_MASK))
5914 s->flags |= SN_ERR_CLICL;
5915 if (!(s->flags & SN_FINST_MASK))
5916 s->flags |= SN_FINST_H;
5917
5918 /* process_session() will take care of the error */
5919 return 0;
5920 }
5921
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005922 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005923 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005924 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005925 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005926 else if (txn->flags & TX_NOT_FIRST)
5927 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005928
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005929 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005930 if (objt_server(s->target)) {
5931 objt_server(s->target)->counters.failed_resp++;
5932 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005933 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005934
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005935 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 rep->analysers = 0;
5937 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005938 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005939 channel_truncate(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005940 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005942 if (!(s->flags & SN_ERR_MASK))
5943 s->flags |= SN_ERR_SRVCL;
5944 if (!(s->flags & SN_FINST_MASK))
5945 s->flags |= SN_FINST_H;
5946 return 0;
5947 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005950 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005952 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005953 else if (txn->flags & TX_NOT_FIRST)
5954 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005955
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005956 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005957 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005958 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959
5960 if (!(s->flags & SN_ERR_MASK))
5961 s->flags |= SN_ERR_CLICL;
5962 if (!(s->flags & SN_FINST_MASK))
5963 s->flags |= SN_FINST_H;
5964
5965 /* process_session() will take care of the error */
5966 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005968
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005969 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005970 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971 return 0;
5972 }
5973
5974 /* More interesting part now : we know that we have a complete
5975 * response which at least looks like HTTP. We have an indicator
5976 * of each header's length, so we can parse them quickly.
5977 */
5978
5979 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005980 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 /*
5983 * 1: get the status code
5984 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005985 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 if (n < 1 || n > 5)
5987 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005988 /* when the client triggers a 4xx from the server, it's most often due
5989 * to a missing object or permission. These events should be tracked
5990 * because if they happen often, it may indicate a brute force or a
5991 * vulnerability scan.
5992 */
5993 if (n == 4)
5994 session_inc_http_err_ctr(s);
5995
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005996 if (objt_server(s->target))
5997 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998
Willy Tarreau5b154472009-12-21 20:11:07 +01005999 /* check if the response is HTTP/1.1 or above */
6000 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006001 ((rep->buf->p[5] > '1') ||
6002 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006003 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006004
6005 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006006 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 +01006007
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006008 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006009 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006010
Willy Tarreau9b28e032012-10-12 23:49:43 +02006011 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012
Willy Tarreau39650402010-03-15 19:44:39 +01006013 /* Adjust server's health based on status code. Note: status codes 501
6014 * and 505 are triggered on demand by client request, so we must not
6015 * count them as server failures.
6016 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006017 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006018 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006019 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006020 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006021 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006022 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006023
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 /*
6025 * 2: check for cacheability.
6026 */
6027
6028 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006029 case 100:
6030 /*
6031 * We may be facing a 100-continue response, in which case this
6032 * is not the right response, and we're waiting for the next one.
6033 * Let's allow this response to go to the client and wait for the
6034 * next one.
6035 */
6036 hdr_idx_init(&txn->hdr_idx);
6037 msg->next -= channel_forward(rep, msg->next);
6038 msg->msg_state = HTTP_MSG_RPBEFORE;
6039 txn->status = 0;
6040 s->logs.t_data = -1; /* was not a response yet */
6041 goto next_one;
6042
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 case 200:
6044 case 203:
6045 case 206:
6046 case 300:
6047 case 301:
6048 case 410:
6049 /* RFC2616 @13.4:
6050 * "A response received with a status code of
6051 * 200, 203, 206, 300, 301 or 410 MAY be stored
6052 * by a cache (...) unless a cache-control
6053 * directive prohibits caching."
6054 *
6055 * RFC2616 @9.5: POST method :
6056 * "Responses to this method are not cacheable,
6057 * unless the response includes appropriate
6058 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006059 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006061 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006062 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6063 break;
6064 default:
6065 break;
6066 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 /*
6069 * 3: we may need to capture headers
6070 */
6071 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006072 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006073 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006074 txn->rsp.cap, s->fe->rsp_cap);
6075
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006076 /* 4: determine the transfer-length.
6077 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6078 * the presence of a message-body in a RESPONSE and its transfer length
6079 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006080 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006081 * All responses to the HEAD request method MUST NOT include a
6082 * message-body, even though the presence of entity-header fields
6083 * might lead one to believe they do. All 1xx (informational), 204
6084 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6085 * message-body. All other responses do include a message-body,
6086 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006087 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006088 * 1. Any response which "MUST NOT" include a message-body (such as the
6089 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6090 * always terminated by the first empty line after the header fields,
6091 * regardless of the entity-header fields present in the message.
6092 *
6093 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6094 * the "chunked" transfer-coding (Section 6.2) is used, the
6095 * transfer-length is defined by the use of this transfer-coding.
6096 * If a Transfer-Encoding header field is present and the "chunked"
6097 * transfer-coding is not present, the transfer-length is defined by
6098 * the sender closing the connection.
6099 *
6100 * 3. If a Content-Length header field is present, its decimal value in
6101 * OCTETs represents both the entity-length and the transfer-length.
6102 * If a message is received with both a Transfer-Encoding header
6103 * field and a Content-Length header field, the latter MUST be ignored.
6104 *
6105 * 4. If the message uses the media type "multipart/byteranges", and
6106 * the transfer-length is not otherwise specified, then this self-
6107 * delimiting media type defines the transfer-length. This media
6108 * type MUST NOT be used unless the sender knows that the recipient
6109 * can parse it; the presence in a request of a Range header with
6110 * multiple byte-range specifiers from a 1.1 client implies that the
6111 * client can parse multipart/byteranges responses.
6112 *
6113 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006114 */
6115
6116 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006117 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006118 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006119 * FIXME: should we parse anyway and return an error on chunked encoding ?
6120 */
6121 if (txn->meth == HTTP_METH_HEAD ||
6122 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006123 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006124 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006125 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006126 goto skip_content_length;
6127 }
6128
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006129 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006130 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006131 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006132 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006133 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006134 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6135 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006136 /* bad transfer-encoding (chunked followed by something else) */
6137 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006138 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006139 break;
6140 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006141 }
6142
6143 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6144 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006145 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006146 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006147 signed long long cl;
6148
Willy Tarreauad14f752011-09-02 20:33:27 +02006149 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006150 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006151 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006152 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153
Willy Tarreauad14f752011-09-02 20:33:27 +02006154 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006155 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006156 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006157 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158
Willy Tarreauad14f752011-09-02 20:33:27 +02006159 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006160 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006161 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006162 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006163
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006164 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006165 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006166 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006167 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006168
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006169 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006170 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006171 }
6172
William Lallemand82fe75c2012-10-23 10:25:10 +02006173 if (s->fe->comp || s->be->comp)
6174 select_compression_response_header(s, rep->buf);
6175
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006176skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006177 /* Now we have to check if we need to modify the Connection header.
6178 * This is more difficult on the response than it is on the request,
6179 * because we can have two different HTTP versions and we don't know
6180 * how the client will interprete a response. For instance, let's say
6181 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6182 * HTTP/1.1 response without any header. Maybe it will bound itself to
6183 * HTTP/1.0 because it only knows about it, and will consider the lack
6184 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6185 * the lack of header as a keep-alive. Thus we will use two flags
6186 * indicating how a request MAY be understood by the client. In case
6187 * of multiple possibilities, we'll fix the header to be explicit. If
6188 * ambiguous cases such as both close and keepalive are seen, then we
6189 * will fall back to explicit close. Note that we won't take risks with
6190 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006191 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006192 */
6193
Willy Tarreaudc008c52010-02-01 16:20:08 +01006194 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6195 txn->status == 101)) {
6196 /* Either we've established an explicit tunnel, or we're
6197 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006198 * to understand the next protocols. We have to switch to tunnel
6199 * mode, so that we transfer the request and responses then let
6200 * this protocol pass unmodified. When we later implement specific
6201 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006202 * header which contains information about that protocol for
6203 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006204 */
6205 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6206 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006207 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6208 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006209 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6210 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006211 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006212
Willy Tarreau70dffda2014-01-30 03:07:23 +01006213 /* this situation happens when combining pretend-keepalive with httpclose. */
6214 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006215 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6216 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006217 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6218
Willy Tarreau60466522010-01-18 19:08:45 +01006219 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006220 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006221 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6222 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006223
Willy Tarreau60466522010-01-18 19:08:45 +01006224 /* now adjust header transformations depending on current state */
6225 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6226 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6227 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006228 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006229 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006230 }
Willy Tarreau60466522010-01-18 19:08:45 +01006231 else { /* SCL / KAL */
6232 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006233 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006234 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006235 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006236
Willy Tarreau60466522010-01-18 19:08:45 +01006237 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006238 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006239
Willy Tarreau60466522010-01-18 19:08:45 +01006240 /* Some keep-alive responses are converted to Server-close if
6241 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006242 */
Willy Tarreau60466522010-01-18 19:08:45 +01006243 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6244 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006245 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006246 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006247 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006248 }
6249
Willy Tarreau7959a552013-09-23 16:44:27 +02006250 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006251 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006252
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006253 /* end of job, return OK */
6254 rep->analysers &= ~an_bit;
6255 rep->analyse_exp = TICK_ETERNITY;
6256 channel_auto_close(rep);
6257 return 1;
6258
6259 abort_keep_alive:
6260 /* A keep-alive request to the server failed on a network error.
6261 * The client is required to retry. We need to close without returning
6262 * any other information so that the client retries.
6263 */
6264 txn->status = 0;
6265 rep->analysers = 0;
6266 s->req->analysers = 0;
6267 channel_auto_close(rep);
6268 s->logs.logwait = 0;
6269 s->logs.level = 0;
6270 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006271 channel_truncate(rep);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006272 stream_int_retnclose(rep->cons, NULL);
6273 return 0;
6274}
6275
6276/* This function performs all the processing enabled for the current response.
6277 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6278 * and updates s->rep->analysers. It might make sense to explode it into several
6279 * other functions. It works like process_request (see indications above).
6280 */
6281int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6282{
6283 struct http_txn *txn = &s->txn;
6284 struct http_msg *msg = &txn->rsp;
6285 struct proxy *cur_proxy;
6286 struct cond_wordlist *wl;
6287 struct http_res_rule *http_res_last_rule = NULL;
6288
6289 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6290 now_ms, __FUNCTION__,
6291 s,
6292 rep,
6293 rep->rex, rep->wex,
6294 rep->flags,
6295 rep->buf->i,
6296 rep->analysers);
6297
6298 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6299 return 0;
6300
6301 rep->analysers &= ~an_bit;
6302 rep->analyse_exp = TICK_ETERNITY;
6303
Willy Tarreau70730dd2014-04-24 18:06:27 +02006304 /* The stats applet needs to adjust the Connection header but we don't
6305 * apply any filter there.
6306 */
6307 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6308 goto skip_filters;
6309
Willy Tarreau58975672014-04-24 21:13:57 +02006310 /*
6311 * We will have to evaluate the filters.
6312 * As opposed to version 1.2, now they will be evaluated in the
6313 * filters order and not in the header order. This means that
6314 * each filter has to be validated among all headers.
6315 *
6316 * Filters are tried with ->be first, then with ->fe if it is
6317 * different from ->be.
6318 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006319
Willy Tarreau58975672014-04-24 21:13:57 +02006320 cur_proxy = s->be;
6321 while (1) {
6322 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006323
Willy Tarreau58975672014-04-24 21:13:57 +02006324 /* evaluate http-response rules */
6325 if (!http_res_last_rule)
6326 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006327
Willy Tarreau58975672014-04-24 21:13:57 +02006328 /* try headers filters */
6329 if (rule_set->rsp_exp != NULL) {
6330 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6331 return_bad_resp:
6332 if (objt_server(s->target)) {
6333 objt_server(s->target)->counters.failed_resp++;
6334 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006335 }
Willy Tarreau58975672014-04-24 21:13:57 +02006336 s->be->be_counters.failed_resp++;
6337 return_srv_prx_502:
6338 rep->analysers = 0;
6339 txn->status = 502;
6340 s->logs.t_data = -1; /* was not a valid response */
6341 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006342 channel_truncate(rep);
Willy Tarreau58975672014-04-24 21:13:57 +02006343 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6344 if (!(s->flags & SN_ERR_MASK))
6345 s->flags |= SN_ERR_PRXCOND;
6346 if (!(s->flags & SN_FINST_MASK))
6347 s->flags |= SN_FINST_H;
6348 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006349 }
Willy Tarreau58975672014-04-24 21:13:57 +02006350 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006351
Willy Tarreau58975672014-04-24 21:13:57 +02006352 /* has the response been denied ? */
6353 if (txn->flags & TX_SVDENY) {
6354 if (objt_server(s->target))
6355 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006356
Willy Tarreau58975672014-04-24 21:13:57 +02006357 s->be->be_counters.denied_resp++;
6358 s->fe->fe_counters.denied_resp++;
6359 if (s->listener->counters)
6360 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006361
Willy Tarreau58975672014-04-24 21:13:57 +02006362 goto return_srv_prx_502;
6363 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006364
Willy Tarreau58975672014-04-24 21:13:57 +02006365 /* add response headers from the rule sets in the same order */
6366 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006367 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006368 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006369 if (wl->cond) {
6370 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6371 ret = acl_pass(ret);
6372 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6373 ret = !ret;
6374 if (!ret)
6375 continue;
6376 }
6377 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6378 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006379 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006380
Willy Tarreau58975672014-04-24 21:13:57 +02006381 /* check whether we're already working on the frontend */
6382 if (cur_proxy == s->fe)
6383 break;
6384 cur_proxy = s->fe;
6385 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006386
Willy Tarreau58975672014-04-24 21:13:57 +02006387 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006388 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006389 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006390
Willy Tarreau58975672014-04-24 21:13:57 +02006391 /*
6392 * Now check for a server cookie.
6393 */
6394 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6395 (s->be->options & PR_O_CHK_CACHE))
6396 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006397
Willy Tarreau58975672014-04-24 21:13:57 +02006398 /*
6399 * Check for cache-control or pragma headers if required.
6400 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006401 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 +02006402 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006403
Willy Tarreau58975672014-04-24 21:13:57 +02006404 /*
6405 * Add server cookie in the response if needed
6406 */
6407 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6408 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6409 (!(s->flags & SN_DIRECT) ||
6410 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6411 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6412 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6413 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6414 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6415 !(s->flags & SN_IGNORE_PRST)) {
6416 /* the server is known, it's not the one the client requested, or the
6417 * cookie's last seen date needs to be refreshed. We have to
6418 * insert a set-cookie here, except if we want to insert only on POST
6419 * requests and this one isn't. Note that servers which don't have cookies
6420 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006421 */
Willy Tarreau58975672014-04-24 21:13:57 +02006422 if (!objt_server(s->target)->cookie) {
6423 chunk_printf(&trash,
6424 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6425 s->be->cookie_name);
6426 }
6427 else {
6428 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006429
Willy Tarreau58975672014-04-24 21:13:57 +02006430 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6431 /* emit last_date, which is mandatory */
6432 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6433 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6434 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006435
Willy Tarreau58975672014-04-24 21:13:57 +02006436 if (s->be->cookie_maxlife) {
6437 /* emit first_date, which is either the original one or
6438 * the current date.
6439 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006440 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006441 s30tob64(txn->cookie_first_date ?
6442 txn->cookie_first_date >> 2 :
6443 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006444 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006445 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006446 }
Willy Tarreau58975672014-04-24 21:13:57 +02006447 chunk_appendf(&trash, "; path=/");
6448 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006449
Willy Tarreau58975672014-04-24 21:13:57 +02006450 if (s->be->cookie_domain)
6451 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006452
Willy Tarreau58975672014-04-24 21:13:57 +02006453 if (s->be->ck_opts & PR_CK_HTTPONLY)
6454 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006455
Willy Tarreau58975672014-04-24 21:13:57 +02006456 if (s->be->ck_opts & PR_CK_SECURE)
6457 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006458
Willy Tarreau58975672014-04-24 21:13:57 +02006459 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6460 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006461
Willy Tarreau58975672014-04-24 21:13:57 +02006462 txn->flags &= ~TX_SCK_MASK;
6463 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6464 /* the server did not change, only the date was updated */
6465 txn->flags |= TX_SCK_UPDATED;
6466 else
6467 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006468
Willy Tarreau58975672014-04-24 21:13:57 +02006469 /* Here, we will tell an eventual cache on the client side that we don't
6470 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6471 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6472 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006473 */
Willy Tarreau58975672014-04-24 21:13:57 +02006474 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006475
Willy Tarreau58975672014-04-24 21:13:57 +02006476 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006477
Willy Tarreau58975672014-04-24 21:13:57 +02006478 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6479 "Cache-control: private", 22) < 0))
6480 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006481 }
Willy Tarreau58975672014-04-24 21:13:57 +02006482 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483
Willy Tarreau58975672014-04-24 21:13:57 +02006484 /*
6485 * Check if result will be cacheable with a cookie.
6486 * We'll block the response if security checks have caught
6487 * nasty things such as a cacheable cookie.
6488 */
6489 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6490 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6491 (s->be->options & PR_O_CHK_CACHE)) {
6492 /* we're in presence of a cacheable response containing
6493 * a set-cookie header. We'll block it as requested by
6494 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006495 */
Willy Tarreau58975672014-04-24 21:13:57 +02006496 if (objt_server(s->target))
6497 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006498
Willy Tarreau58975672014-04-24 21:13:57 +02006499 s->be->be_counters.denied_resp++;
6500 s->fe->fe_counters.denied_resp++;
6501 if (s->listener->counters)
6502 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503
Willy Tarreau58975672014-04-24 21:13:57 +02006504 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6505 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6506 send_log(s->be, LOG_ALERT,
6507 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6508 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6509 goto return_srv_prx_502;
6510 }
Willy Tarreau03945942009-12-22 16:50:27 +01006511
Willy Tarreau70730dd2014-04-24 18:06:27 +02006512 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006513 /*
6514 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6515 * If an "Upgrade" token is found, the header is left untouched in order
6516 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006517 * if anything but "Upgrade" is present in the Connection header. We don't
6518 * want to touch any 101 response either since it's switching to another
6519 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006520 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006521 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006522 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6523 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6525 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6528 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6529 /* we want a keep-alive response here. Keep-alive header
6530 * required if either side is not 1.1.
6531 */
6532 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6533 want_flags |= TX_CON_KAL_SET;
6534 }
6535 else {
6536 /* we want a close response here. Close header required if
6537 * the server is 1.1, regardless of the client.
6538 */
6539 if (msg->flags & HTTP_MSGF_VER_11)
6540 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006541 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542
Willy Tarreau58975672014-04-24 21:13:57 +02006543 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6544 http_change_connection_header(txn, msg, want_flags);
6545 }
6546
6547 skip_header_mangling:
6548 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6549 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6550 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551
Willy Tarreau58975672014-04-24 21:13:57 +02006552 /* if the user wants to log as soon as possible, without counting
6553 * bytes from the server, then this is the right moment. We have
6554 * to temporarily assign bytes_out to log what we currently have.
6555 */
6556 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6557 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6558 s->logs.bytes_out = txn->rsp.eoh;
6559 s->do_log(s);
6560 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006561 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006562 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006563}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564
Willy Tarreaud98cf932009-12-27 22:54:55 +01006565/* This function is an analyser which forwards response body (including chunk
6566 * sizes if any). It is called as soon as we must forward, even if we forward
6567 * zero byte. The only situation where it must not be called is when we're in
6568 * tunnel mode and we want to forward till the close. It's used both to forward
6569 * remaining data and to resync after end of body. It expects the msg_state to
6570 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6571 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006572 *
6573 * It is capable of compressing response data both in content-length mode and
6574 * in chunked mode. The state machines follows different flows depending on
6575 * whether content-length and chunked modes are used, since there are no
6576 * trailers in content-length :
6577 *
6578 * chk-mode cl-mode
6579 * ,----- BODY -----.
6580 * / \
6581 * V size > 0 V chk-mode
6582 * .--> SIZE -------------> DATA -------------> CRLF
6583 * | | size == 0 | last byte |
6584 * | v final crlf v inspected |
6585 * | TRAILERS -----------> DONE |
6586 * | |
6587 * `----------------------------------------------'
6588 *
6589 * Compression only happens in the DATA state, and must be flushed in final
6590 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6591 * is performed at once on final states for all bytes parsed, or when leaving
6592 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006593 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006594int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006595{
6596 struct http_txn *txn = &s->txn;
6597 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006598 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006599 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006600 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006601
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006602 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6603 return 0;
6604
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006605 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006606 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006607 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006608 /* Output closed while we were sending data. We must abort and
6609 * wake the other side up.
6610 */
6611 msg->msg_state = HTTP_MSG_ERROR;
6612 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006613 return 1;
6614 }
6615
Willy Tarreau4fe41902010-06-07 22:27:41 +02006616 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006617 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006618
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006619 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006620 /* we have msg->sov which points to the first byte of message
6621 * body, and res->buf.p still points to the beginning of the
6622 * message. We forward the headers now, as we don't need them
6623 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006624 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006625 b_adv(res->buf, msg->sov);
6626 msg->next -= msg->sov;
6627 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006628
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006629 /* The previous analysers guarantee that the state is somewhere
6630 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6631 * msg->next are always correct.
6632 */
6633 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6634 if (msg->flags & HTTP_MSGF_TE_CHNK)
6635 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6636 else
6637 msg->msg_state = HTTP_MSG_DATA;
6638 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006639 }
6640
Willy Tarreauefdf0942014-04-24 20:08:57 +02006641 if (res->to_forward) {
6642 /* We can't process the buffer's contents yet */
6643 res->flags |= CF_WAKE_WRITE;
6644 goto missing_data;
6645 }
6646
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006647 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6648 /* We need a compression buffer in the DATA state to put the
6649 * output of compressed data, and in CRLF state to let the
6650 * TRAILERS state finish the job of removing the trailing CRLF.
6651 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006652 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006653 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006654 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006655 goto aborted_xfer; /* no memory */
6656 }
6657
6658 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006659 if (ret < 0) {
6660 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006661 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006662 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006663 compressing = 1;
6664 }
6665
Willy Tarreaud98cf932009-12-27 22:54:55 +01006666 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006667 switch (msg->msg_state - HTTP_MSG_DATA) {
6668 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006669 /* we may have some pending data starting at res->buf->p */
6670 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006671 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006672 if (ret < 0)
6673 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006674
Willy Tarreaud5a67832014-04-21 10:54:27 +02006675 if (msg->chunk_len) {
6676 /* input empty or output full */
6677 if (res->buf->i > msg->next)
6678 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006679 goto missing_data;
6680 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006681 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006682 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006683 if (msg->chunk_len > res->buf->i - msg->next) {
6684 /* output full */
6685 res->flags |= CF_WAKE_WRITE;
6686 goto missing_data;
6687 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006688 msg->next += msg->chunk_len;
6689 msg->chunk_len = 0;
6690 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006691
6692 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006693 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006694 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006695 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006696 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006697 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006698 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006699 /* fall through for HTTP_MSG_CHUNK_CRLF */
6700
6701 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6702 /* we want the CRLF after the data */
6703
6704 ret = http_skip_chunk_crlf(msg);
6705 if (ret == 0)
6706 goto missing_data;
6707 else if (ret < 0) {
6708 if (msg->err_pos >= 0)
6709 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6710 goto return_bad_res;
6711 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006712 /* we're in MSG_CHUNK_SIZE now, fall through */
6713
6714 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006715 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006716 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006717 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006718 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006719
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006720 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006721 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006722 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006723 else if (ret < 0) {
6724 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006725 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006726 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006727 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006728 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006729 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006730
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006731 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006732 if (unlikely(compressing)) {
6733 /* we need to flush output contents before syncing FSMs */
6734 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6735 compressing = 0;
6736 }
6737
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006738 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006739 if (ret == 0)
6740 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006741 else if (ret < 0) {
6742 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006743 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006744 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006745 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006746 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006747
6748 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006749 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006750 if (unlikely(compressing)) {
6751 /* we need to flush output contents before syncing FSMs */
6752 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6753 compressing = 0;
6754 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006755
Willy Tarreauc623c172014-04-18 09:53:50 +02006756 /* we may have some pending data starting at res->buf->p
6757 * such as a last chunk of data or trailers.
6758 */
6759 b_adv(res->buf, msg->next);
6760 msg->next = 0;
6761
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006762 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006763 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006764 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6765 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006766 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006767
Willy Tarreau610ecce2010-01-04 21:15:02 +01006768 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006769 /* some state changes occurred, maybe the analyser
6770 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006771 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006773 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006774 /* response errors are most likely due to
6775 * the client aborting the transfer.
6776 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006777 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006778 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006779 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006780 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006781 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006782 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006783 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006784 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006785 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006786 }
6787 }
6788
Willy Tarreaud98cf932009-12-27 22:54:55 +01006789 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006790 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006791 if (unlikely(compressing)) {
6792 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006793 compressing = 0;
6794 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006795
Willy Tarreauc623c172014-04-18 09:53:50 +02006796 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6797 b_adv(res->buf, msg->next);
6798 msg->next = 0;
6799 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6800 }
6801
Willy Tarreauf003d372012-11-26 13:35:37 +01006802 if (res->flags & CF_SHUTW)
6803 goto aborted_xfer;
6804
6805 /* stop waiting for data if the input is closed before the end. If the
6806 * client side was already closed, it means that the client has aborted,
6807 * so we don't want to count this as a server abort. Otherwise it's a
6808 * server abort.
6809 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006810 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006811 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006812 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006813 if (!(s->flags & SN_ERR_MASK))
6814 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006815 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006816 if (objt_server(s->target))
6817 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006818 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006819 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006820
Willy Tarreau40dba092010-03-04 18:14:51 +01006821 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006822 if (!s->req->analysers)
6823 goto return_bad_res;
6824
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006825 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006826 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006827 * Similarly, with keep-alive on the client side, we don't want to forward a
6828 * close.
6829 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006830 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006831 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6832 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006833 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006834
Willy Tarreau5c620922011-05-11 19:56:11 +02006835 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006836 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006837 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006838 * modes are already handled by the stream sock layer. We must not do
6839 * this in content-length mode because it could present the MSG_MORE
6840 * flag with the last block of forwarded data, which would cause an
6841 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006842 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006843 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006844 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006845
Willy Tarreaud98cf932009-12-27 22:54:55 +01006846 /* the session handler will take care of timeouts and errors */
6847 return 0;
6848
Willy Tarreau40dba092010-03-04 18:14:51 +01006849 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006850 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006851 if (objt_server(s->target))
6852 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006853
6854 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006855 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006856 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006857 compressing = 0;
6858 }
6859
Willy Tarreauc623c172014-04-18 09:53:50 +02006860 /* we may have some pending data starting at res->buf->p */
6861 if (s->comp_algo == NULL) {
6862 b_adv(res->buf, msg->next);
6863 msg->next = 0;
6864 }
6865
Willy Tarreaud98cf932009-12-27 22:54:55 +01006866 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006867 /* don't send any error message as we're in the body */
6868 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006869 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006870 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006871 if (objt_server(s->target))
6872 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006873
6874 if (!(s->flags & SN_ERR_MASK))
6875 s->flags |= SN_ERR_PRXCOND;
6876 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006877 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006878 return 0;
6879
6880 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006881 if (unlikely(compressing)) {
6882 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6883 compressing = 0;
6884 }
6885
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006886 txn->rsp.msg_state = HTTP_MSG_ERROR;
6887 /* don't send any error message as we're in the body */
6888 stream_int_retnclose(res->cons, NULL);
6889 res->analysers = 0;
6890 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6891
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006892 s->fe->fe_counters.cli_aborts++;
6893 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006894 if (objt_server(s->target))
6895 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006896
6897 if (!(s->flags & SN_ERR_MASK))
6898 s->flags |= SN_ERR_CLICL;
6899 if (!(s->flags & SN_FINST_MASK))
6900 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006901 return 0;
6902}
6903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006904/* Iterate the same filter through all request headers.
6905 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 * Since it can manage the switch to another backend, it updates the per-proxy
6907 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006909int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 char *cur_ptr, *cur_end, *cur_next;
6912 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006913 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006914 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006915 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006916
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006917 last_hdr = 0;
6918
Willy Tarreau9b28e032012-10-12 23:49:43 +02006919 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 old_idx = 0;
6921
6922 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006923 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006925 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006926 (exp->action == ACT_ALLOW ||
6927 exp->action == ACT_DENY ||
6928 exp->action == ACT_TARPIT))
6929 return 0;
6930
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006931 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006932 if (!cur_idx)
6933 break;
6934
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006935 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006936 cur_ptr = cur_next;
6937 cur_end = cur_ptr + cur_hdr->len;
6938 cur_next = cur_end + cur_hdr->cr + 1;
6939
6940 /* Now we have one header between cur_ptr and cur_end,
6941 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006942 */
6943
Willy Tarreau15a53a42015-01-21 13:39:42 +01006944 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006945 switch (exp->action) {
6946 case ACT_SETBE:
6947 /* It is not possible to jump a second time.
6948 * FIXME: should we return an HTTP/500 here so that
6949 * the admin knows there's a problem ?
6950 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006951 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006952 break;
6953
6954 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006955 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006956 last_hdr = 1;
6957 break;
6958
6959 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006960 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006961 last_hdr = 1;
6962 break;
6963
6964 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006965 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006967 break;
6968
6969 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006970 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006971 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006972 break;
6973
6974 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006975 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6976 if (trash.len < 0)
6977 return -1;
6978
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006979 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 /* FIXME: if the user adds a newline in the replacement, the
6981 * index will not be recalculated for now, and the new line
6982 * will not be counted as a new header.
6983 */
6984
6985 cur_end += delta;
6986 cur_next += delta;
6987 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006988 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 break;
6990
6991 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006992 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 cur_next += delta;
6994
Willy Tarreaufa355d42009-11-29 18:12:29 +01006995 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006996 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6997 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998 cur_hdr->len = 0;
6999 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007000 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007001 break;
7002
7003 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004 }
7005
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 /* keep the link from this header to next one in case of later
7007 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007008 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 old_idx = cur_idx;
7010 }
7011 return 0;
7012}
7013
7014
7015/* Apply the filter to the request line.
7016 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7017 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018 * Since it can manage the switch to another backend, it updates the per-proxy
7019 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007020 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007021int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007023 char *cur_ptr, *cur_end;
7024 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007025 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007026 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007027
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 (exp->action == ACT_ALLOW ||
7032 exp->action == ACT_DENY ||
7033 exp->action == ACT_TARPIT))
7034 return 0;
7035 else if (exp->action == ACT_REMOVE)
7036 return 0;
7037
7038 done = 0;
7039
Willy Tarreau9b28e032012-10-12 23:49:43 +02007040 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007041 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007042
7043 /* Now we have the request line between cur_ptr and cur_end */
7044
Willy Tarreau15a53a42015-01-21 13:39:42 +01007045 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 switch (exp->action) {
7047 case ACT_SETBE:
7048 /* It is not possible to jump a second time.
7049 * FIXME: should we return an HTTP/500 here so that
7050 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007052 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 break;
7054
7055 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 done = 1;
7058 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007061 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062 done = 1;
7063 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007065 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007066 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007067 done = 1;
7068 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007070 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007071 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007072 done = 1;
7073 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007076 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7077 if (trash.len < 0)
7078 return -1;
7079
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007080 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 /* FIXME: if the user adds a newline in the replacement, the
7082 * index will not be recalculated for now, and the new line
7083 * will not be counted as a new header.
7084 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007085
Willy Tarreaufa355d42009-11-29 18:12:29 +01007086 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007087 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007088 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 HTTP_MSG_RQMETH,
7090 cur_ptr, cur_end + 1,
7091 NULL, NULL);
7092 if (unlikely(!cur_end))
7093 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095 /* we have a full request and we know that we have either a CR
7096 * or an LF at <ptr>.
7097 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007098 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7099 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 /* there is no point trying this regex on headers */
7101 return 1;
7102 }
7103 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 return done;
7105}
Willy Tarreau97de6242006-12-27 17:18:38 +01007106
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007110 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112 * unparsable request. Since it can manage the switch to another backend, it
7113 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007114 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007115int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007116{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007117 struct http_txn *txn = &s->txn;
7118 struct hdr_exp *exp;
7119
7120 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 /*
7124 * The interleaving of transformations and verdicts
7125 * makes it difficult to decide to continue or stop
7126 * the evaluation.
7127 */
7128
Willy Tarreau6c123b12010-01-28 20:22:06 +01007129 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7130 break;
7131
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007134 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007136
7137 /* if this filter had a condition, evaluate it now and skip to
7138 * next filter if the condition does not match.
7139 */
7140 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007141 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007142 ret = acl_pass(ret);
7143 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7144 ret = !ret;
7145
7146 if (!ret)
7147 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 }
7149
7150 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007151 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 if (unlikely(ret < 0))
7153 return -1;
7154
7155 if (likely(ret == 0)) {
7156 /* The filter did not match the request, it can be
7157 * iterated through all headers.
7158 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007159 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7160 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007163 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164}
7165
7166
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007169 * Try to retrieve the server associated to the appsession.
7170 * If the server is found, it's assigned to the session.
7171 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007172void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7173 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007174 appsess *asession = NULL;
7175 char *sessid_temp = NULL;
7176
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007177 if (len > s->be->appsession_len) {
7178 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007179 }
7180
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007181 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007182 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007183 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007184 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007185 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007186 }
7187
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007188 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007189 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007190 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007191 return;
7192 }
7193
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007194 memcpy(txn->sessid, buf, len);
7195 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007196 }
7197
7198 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7199 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007200 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007201 return;
7202 }
7203
Cyril Bontéb21570a2009-11-29 20:04:48 +01007204 memcpy(sessid_temp, buf, len);
7205 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007206
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007207 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007208 /* free previously allocated memory */
7209 pool_free2(apools.sessid, sessid_temp);
7210
7211 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007212 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7213 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007214 asession->request_count++;
7215
7216 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007217 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007218
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007219 while (srv) {
7220 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007221 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007222 (s->be->options & PR_O_PERSIST) ||
7223 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007224 /* we found the server and it's usable */
7225 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007226 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007227 s->flags |= SN_DIRECT | SN_ASSIGNED;
7228 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007229
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007230 break;
7231 } else {
7232 txn->flags &= ~TX_CK_MASK;
7233 txn->flags |= TX_CK_DOWN;
7234 }
7235 }
7236 srv = srv->next;
7237 }
7238 }
7239 }
7240}
7241
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242/* Find the end of a cookie value contained between <s> and <e>. It works the
7243 * same way as with headers above except that the semi-colon also ends a token.
7244 * See RFC2965 for more information. Note that it requires a valid header to
7245 * return a valid result.
7246 */
7247char *find_cookie_value_end(char *s, const char *e)
7248{
7249 int quoted, qdpair;
7250
7251 quoted = qdpair = 0;
7252 for (; s < e; s++) {
7253 if (qdpair) qdpair = 0;
7254 else if (quoted) {
7255 if (*s == '\\') qdpair = 1;
7256 else if (*s == '"') quoted = 0;
7257 }
7258 else if (*s == '"') quoted = 1;
7259 else if (*s == ',' || *s == ';') return s;
7260 }
7261 return s;
7262}
7263
7264/* Delete a value in a header between delimiters <from> and <next> in buffer
7265 * <buf>. The number of characters displaced is returned, and the pointer to
7266 * the first delimiter is updated if required. The function tries as much as
7267 * possible to respect the following principles :
7268 * - replace <from> delimiter by the <next> one unless <from> points to a
7269 * colon, in which case <next> is simply removed
7270 * - set exactly one space character after the new first delimiter, unless
7271 * there are not enough characters in the block being moved to do so.
7272 * - remove unneeded spaces before the previous delimiter and after the new
7273 * one.
7274 *
7275 * It is the caller's responsibility to ensure that :
7276 * - <from> points to a valid delimiter or the colon ;
7277 * - <next> points to a valid delimiter or the final CR/LF ;
7278 * - there are non-space chars before <from> ;
7279 * - there is a CR/LF at or after <next>.
7280 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007281int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007282{
7283 char *prev = *from;
7284
7285 if (*prev == ':') {
7286 /* We're removing the first value, preserve the colon and add a
7287 * space if possible.
7288 */
7289 if (!http_is_crlf[(unsigned char)*next])
7290 next++;
7291 prev++;
7292 if (prev < next)
7293 *prev++ = ' ';
7294
7295 while (http_is_spht[(unsigned char)*next])
7296 next++;
7297 } else {
7298 /* Remove useless spaces before the old delimiter. */
7299 while (http_is_spht[(unsigned char)*(prev-1)])
7300 prev--;
7301 *from = prev;
7302
7303 /* copy the delimiter and if possible a space if we're
7304 * not at the end of the line.
7305 */
7306 if (!http_is_crlf[(unsigned char)*next]) {
7307 *prev++ = *next++;
7308 if (prev + 1 < next)
7309 *prev++ = ' ';
7310 while (http_is_spht[(unsigned char)*next])
7311 next++;
7312 }
7313 }
7314 return buffer_replace2(buf, prev, next, NULL, 0);
7315}
7316
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007317/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007318 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007319 * desirable to call it only when needed. This code is quite complex because
7320 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7321 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007323void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007325 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007326 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007328 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7329 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007331 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007332 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007333 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007335 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007337 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007339 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007340 hdr_beg = hdr_next;
7341 hdr_end = hdr_beg + cur_hdr->len;
7342 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007344 /* We have one full header between hdr_beg and hdr_end, and the
7345 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 * "Cookie:" headers.
7347 */
7348
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007349 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007350 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007351 old_idx = cur_idx;
7352 continue;
7353 }
7354
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007355 del_from = NULL; /* nothing to be deleted */
7356 preserve_hdr = 0; /* assume we may kill the whole header */
7357
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358 /* Now look for cookies. Conforming to RFC2109, we have to support
7359 * attributes whose name begin with a '$', and associate them with
7360 * the right cookie, if we want to delete this cookie.
7361 * So there are 3 cases for each cookie read :
7362 * 1) it's a special attribute, beginning with a '$' : ignore it.
7363 * 2) it's a server id cookie that we *MAY* want to delete : save
7364 * some pointers on it (last semi-colon, beginning of cookie...)
7365 * 3) it's an application cookie : we *MAY* have to delete a previous
7366 * "special" cookie.
7367 * At the end of loop, if a "special" cookie remains, we may have to
7368 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007369 * *MUST* delete it.
7370 *
7371 * Note: RFC2965 is unclear about the processing of spaces around
7372 * the equal sign in the ATTR=VALUE form. A careful inspection of
7373 * the RFC explicitly allows spaces before it, and not within the
7374 * tokens (attrs or values). An inspection of RFC2109 allows that
7375 * too but section 10.1.3 lets one think that spaces may be allowed
7376 * after the equal sign too, resulting in some (rare) buggy
7377 * implementations trying to do that. So let's do what servers do.
7378 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7379 * allowed quoted strings in values, with any possible character
7380 * after a backslash, including control chars and delimitors, which
7381 * causes parsing to become ambiguous. Browsers also allow spaces
7382 * within values even without quotes.
7383 *
7384 * We have to keep multiple pointers in order to support cookie
7385 * removal at the beginning, middle or end of header without
7386 * corrupting the header. All of these headers are valid :
7387 *
7388 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7389 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7390 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7391 * | | | | | | | | |
7392 * | | | | | | | | hdr_end <--+
7393 * | | | | | | | +--> next
7394 * | | | | | | +----> val_end
7395 * | | | | | +-----------> val_beg
7396 * | | | | +--------------> equal
7397 * | | | +----------------> att_end
7398 * | | +---------------------> att_beg
7399 * | +--------------------------> prev
7400 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007401 */
7402
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007403 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7404 /* Iterate through all cookies on this line */
7405
7406 /* find att_beg */
7407 att_beg = prev + 1;
7408 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7409 att_beg++;
7410
7411 /* find att_end : this is the first character after the last non
7412 * space before the equal. It may be equal to hdr_end.
7413 */
7414 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007415
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007416 while (equal < hdr_end) {
7417 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007418 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007419 if (http_is_spht[(unsigned char)*equal++])
7420 continue;
7421 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007422 }
7423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007424 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7425 * is between <att_beg> and <equal>, both may be identical.
7426 */
7427
7428 /* look for end of cookie if there is an equal sign */
7429 if (equal < hdr_end && *equal == '=') {
7430 /* look for the beginning of the value */
7431 val_beg = equal + 1;
7432 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7433 val_beg++;
7434
7435 /* find the end of the value, respecting quotes */
7436 next = find_cookie_value_end(val_beg, hdr_end);
7437
7438 /* make val_end point to the first white space or delimitor after the value */
7439 val_end = next;
7440 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7441 val_end--;
7442 } else {
7443 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007444 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007446 /* We have nothing to do with attributes beginning with '$'. However,
7447 * they will automatically be removed if a header before them is removed,
7448 * since they're supposed to be linked together.
7449 */
7450 if (*att_beg == '$')
7451 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007453 /* Ignore cookies with no equal sign */
7454 if (equal == next) {
7455 /* This is not our cookie, so we must preserve it. But if we already
7456 * scheduled another cookie for removal, we cannot remove the
7457 * complete header, but we can remove the previous block itself.
7458 */
7459 preserve_hdr = 1;
7460 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007461 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007462 val_end += delta;
7463 next += delta;
7464 hdr_end += delta;
7465 hdr_next += delta;
7466 cur_hdr->len += delta;
7467 http_msg_move_end(&txn->req, delta);
7468 prev = del_from;
7469 del_from = NULL;
7470 }
7471 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007472 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007474 /* if there are spaces around the equal sign, we need to
7475 * strip them otherwise we'll get trouble for cookie captures,
7476 * or even for rewrites. Since this happens extremely rarely,
7477 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007478 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7480 int stripped_before = 0;
7481 int stripped_after = 0;
7482
7483 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007484 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 equal += stripped_before;
7486 val_beg += stripped_before;
7487 }
7488
7489 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007490 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 val_beg += stripped_after;
7492 stripped_before += stripped_after;
7493 }
7494
7495 val_end += stripped_before;
7496 next += stripped_before;
7497 hdr_end += stripped_before;
7498 hdr_next += stripped_before;
7499 cur_hdr->len += stripped_before;
7500 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007501 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007502 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 /* First, let's see if we want to capture this cookie. We check
7505 * that we don't already have a client side cookie, because we
7506 * can only capture one. Also as an optimisation, we ignore
7507 * cookies shorter than the declared name.
7508 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007509 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7510 (val_end - att_beg >= s->fe->capture_namelen) &&
7511 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512 int log_len = val_end - att_beg;
7513
7514 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7515 Alert("HTTP logging : out of memory.\n");
7516 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007517 if (log_len > s->fe->capture_len)
7518 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007519 memcpy(txn->cli_cookie, att_beg, log_len);
7520 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007521 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523
Willy Tarreaubca99692010-10-06 19:25:55 +02007524 /* Persistence cookies in passive, rewrite or insert mode have the
7525 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007527 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007529 * For cookies in prefix mode, the form is :
7530 *
7531 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007533 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7534 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7535 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007536 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007537
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7539 * have the server ID between val_beg and delim, and the original cookie between
7540 * delim+1 and val_end. Otherwise, delim==val_end :
7541 *
7542 * Cookie: NAME=SRV; # in all but prefix modes
7543 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7544 * | || || | |+-> next
7545 * | || || | +--> val_end
7546 * | || || +---------> delim
7547 * | || |+------------> val_beg
7548 * | || +-------------> att_end = equal
7549 * | |+-----------------> att_beg
7550 * | +------------------> prev
7551 * +-------------------------> hdr_beg
7552 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007553
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007554 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 for (delim = val_beg; delim < val_end; delim++)
7556 if (*delim == COOKIE_DELIM)
7557 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007558 } else {
7559 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007561 /* Now check if the cookie contains a date field, which would
7562 * appear after a vertical bar ('|') just after the server name
7563 * and before the delimiter.
7564 */
7565 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7566 if (vbar1) {
7567 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007568 * right is the last seen date. It is a base64 encoded
7569 * 30-bit value representing the UNIX date since the
7570 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007571 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007572 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007573 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007574 if (val_end - vbar1 >= 5) {
7575 val = b64tos30(vbar1);
7576 if (val > 0)
7577 txn->cookie_last_date = val << 2;
7578 }
7579 /* look for a second vertical bar */
7580 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7581 if (vbar1 && (val_end - vbar1 > 5)) {
7582 val = b64tos30(vbar1 + 1);
7583 if (val > 0)
7584 txn->cookie_first_date = val << 2;
7585 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007586 }
7587 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588
Willy Tarreauf64d1412010-10-07 20:06:11 +02007589 /* if the cookie has an expiration date and the proxy wants to check
7590 * it, then we do that now. We first check if the cookie is too old,
7591 * then only if it has expired. We detect strict overflow because the
7592 * time resolution here is not great (4 seconds). Cookies with dates
7593 * in the future are ignored if their offset is beyond one day. This
7594 * allows an admin to fix timezone issues without expiring everyone
7595 * and at the same time avoids keeping unwanted side effects for too
7596 * long.
7597 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007598 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7599 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007600 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007601 txn->flags &= ~TX_CK_MASK;
7602 txn->flags |= TX_CK_OLD;
7603 delim = val_beg; // let's pretend we have not found the cookie
7604 txn->cookie_first_date = 0;
7605 txn->cookie_last_date = 0;
7606 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007607 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7608 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007609 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007610 txn->flags &= ~TX_CK_MASK;
7611 txn->flags |= TX_CK_EXPIRED;
7612 delim = val_beg; // let's pretend we have not found the cookie
7613 txn->cookie_first_date = 0;
7614 txn->cookie_last_date = 0;
7615 }
7616
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007617 /* Here, we'll look for the first running server which supports the cookie.
7618 * This allows to share a same cookie between several servers, for example
7619 * to dedicate backup servers to specific servers only.
7620 * However, to prevent clients from sticking to cookie-less backup server
7621 * when they have incidentely learned an empty cookie, we simply ignore
7622 * empty cookies and mark them as invalid.
7623 * The same behaviour is applied when persistence must be ignored.
7624 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007625 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 while (srv) {
7629 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7630 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007631 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007632 (s->be->options & PR_O_PERSIST) ||
7633 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007634 /* we found the server and we can use it */
7635 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007636 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007637 s->flags |= SN_DIRECT | SN_ASSIGNED;
7638 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 break;
7640 } else {
7641 /* we found a server, but it's down,
7642 * mark it as such and go on in case
7643 * another one is available.
7644 */
7645 txn->flags &= ~TX_CK_MASK;
7646 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007648 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 srv = srv->next;
7650 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007651
Willy Tarreauf64d1412010-10-07 20:06:11 +02007652 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007653 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007654 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007655 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007656 txn->flags |= TX_CK_UNUSED;
7657 else
7658 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 /* depending on the cookie mode, we may have to either :
7662 * - delete the complete cookie if we're in insert+indirect mode, so that
7663 * the server never sees it ;
7664 * - remove the server id from the cookie value, and tag the cookie as an
7665 * application cookie so that it does not get accidentely removed later,
7666 * if we're in cookie prefix mode
7667 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007668 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007669 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670
Willy Tarreau9b28e032012-10-12 23:49:43 +02007671 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 val_end += delta;
7673 next += delta;
7674 hdr_end += delta;
7675 hdr_next += delta;
7676 cur_hdr->len += delta;
7677 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 del_from = NULL;
7680 preserve_hdr = 1; /* we want to keep this cookie */
7681 }
7682 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007683 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 del_from = prev;
7685 }
7686 } else {
7687 /* This is not our cookie, so we must preserve it. But if we already
7688 * scheduled another cookie for removal, we cannot remove the
7689 * complete header, but we can remove the previous block itself.
7690 */
7691 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007694 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007695 if (att_beg >= del_from)
7696 att_beg += delta;
7697 if (att_end >= del_from)
7698 att_end += delta;
7699 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007700 val_end += delta;
7701 next += delta;
7702 hdr_end += delta;
7703 hdr_next += delta;
7704 cur_hdr->len += delta;
7705 http_msg_move_end(&txn->req, delta);
7706 prev = del_from;
7707 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007712 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 int cmp_len, value_len;
7714 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007715
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007716 if (s->be->options2 & PR_O2_AS_PFX) {
7717 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7718 value_begin = att_beg + s->be->appsession_name_len;
7719 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 } else {
7721 cmp_len = att_end - att_beg;
7722 value_begin = val_beg;
7723 value_len = val_end - val_beg;
7724 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007725
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007727 if (cmp_len == s->be->appsession_name_len &&
7728 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7729 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731 }
7732
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007733 /* continue with next cookie on this header line */
7734 att_beg = next;
7735 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 /* There are no more cookies on this line.
7738 * We may still have one (or several) marked for deletion at the
7739 * end of the line. We must do this now in two ways :
7740 * - if some cookies must be preserved, we only delete from the
7741 * mark to the end of line ;
7742 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007743 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007747 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749 cur_hdr->len += delta;
7750 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007751 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752
7753 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007754 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7755 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007757 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007760 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761 }
7762
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766}
7767
7768
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769/* Iterate the same filter through all response headers contained in <rtr>.
7770 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7771 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007774 char *cur_ptr, *cur_end, *cur_next;
7775 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007778 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779
7780 last_hdr = 0;
7781
Willy Tarreau9b28e032012-10-12 23:49:43 +02007782 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 old_idx = 0;
7784
7785 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007786 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007788 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789 (exp->action == ACT_ALLOW ||
7790 exp->action == ACT_DENY))
7791 return 0;
7792
7793 cur_idx = txn->hdr_idx.v[old_idx].next;
7794 if (!cur_idx)
7795 break;
7796
7797 cur_hdr = &txn->hdr_idx.v[cur_idx];
7798 cur_ptr = cur_next;
7799 cur_end = cur_ptr + cur_hdr->len;
7800 cur_next = cur_end + cur_hdr->cr + 1;
7801
7802 /* Now we have one header between cur_ptr and cur_end,
7803 * and the next header starts at cur_next.
7804 */
7805
Willy Tarreau15a53a42015-01-21 13:39:42 +01007806 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007807 switch (exp->action) {
7808 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007809 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 last_hdr = 1;
7811 break;
7812
7813 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007814 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815 last_hdr = 1;
7816 break;
7817
7818 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007819 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7820 if (trash.len < 0)
7821 return -1;
7822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007823 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 /* FIXME: if the user adds a newline in the replacement, the
7825 * index will not be recalculated for now, and the new line
7826 * will not be counted as a new header.
7827 */
7828
7829 cur_end += delta;
7830 cur_next += delta;
7831 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007832 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 break;
7834
7835 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007836 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 cur_next += delta;
7838
Willy Tarreaufa355d42009-11-29 18:12:29 +01007839 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7841 txn->hdr_idx.used--;
7842 cur_hdr->len = 0;
7843 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007844 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 break;
7846
7847 }
7848 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849
7850 /* keep the link from this header to next one in case of later
7851 * removal of next header.
7852 */
7853 old_idx = cur_idx;
7854 }
7855 return 0;
7856}
7857
7858
7859/* Apply the filter to the status line in the response buffer <rtr>.
7860 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7861 * or -1 if a replacement resulted in an invalid status line.
7862 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007863int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 char *cur_ptr, *cur_end;
7866 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007867 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007868 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869
7870
Willy Tarreau3d300592007-03-18 18:34:41 +01007871 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007873 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 (exp->action == ACT_ALLOW ||
7875 exp->action == ACT_DENY))
7876 return 0;
7877 else if (exp->action == ACT_REMOVE)
7878 return 0;
7879
7880 done = 0;
7881
Willy Tarreau9b28e032012-10-12 23:49:43 +02007882 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007883 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884
7885 /* Now we have the status line between cur_ptr and cur_end */
7886
Willy Tarreau15a53a42015-01-21 13:39:42 +01007887 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888 switch (exp->action) {
7889 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007890 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 done = 1;
7892 break;
7893
7894 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007895 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 done = 1;
7897 break;
7898
7899 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007900 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7901 if (trash.len < 0)
7902 return -1;
7903
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007904 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 /* FIXME: if the user adds a newline in the replacement, the
7906 * index will not be recalculated for now, and the new line
7907 * will not be counted as a new header.
7908 */
7909
Willy Tarreaufa355d42009-11-29 18:12:29 +01007910 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007912 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007913 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 cur_ptr, cur_end + 1,
7915 NULL, NULL);
7916 if (unlikely(!cur_end))
7917 return -1;
7918
7919 /* we have a full respnse and we know that we have either a CR
7920 * or an LF at <ptr>.
7921 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007922 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007923 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 /* there is no point trying this regex on headers */
7925 return 1;
7926 }
7927 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007928 return done;
7929}
7930
7931
7932
7933/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007934 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7936 * unparsable response.
7937 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007938int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007940 struct http_txn *txn = &s->txn;
7941 struct hdr_exp *exp;
7942
7943 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944 int ret;
7945
7946 /*
7947 * The interleaving of transformations and verdicts
7948 * makes it difficult to decide to continue or stop
7949 * the evaluation.
7950 */
7951
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007952 if (txn->flags & TX_SVDENY)
7953 break;
7954
Willy Tarreau3d300592007-03-18 18:34:41 +01007955 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7957 exp->action == ACT_PASS)) {
7958 exp = exp->next;
7959 continue;
7960 }
7961
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007962 /* if this filter had a condition, evaluate it now and skip to
7963 * next filter if the condition does not match.
7964 */
7965 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007966 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007967 ret = acl_pass(ret);
7968 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7969 ret = !ret;
7970 if (!ret)
7971 continue;
7972 }
7973
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007975 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 if (unlikely(ret < 0))
7977 return -1;
7978
7979 if (likely(ret == 0)) {
7980 /* The filter did not match the response, it can be
7981 * iterated through all headers.
7982 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007983 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7984 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 }
7987 return 0;
7988}
7989
7990
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007992 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007993 * desirable to call it only when needed. This function is also used when we
7994 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007996void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007998 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007999 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008002 char *hdr_beg, *hdr_end, *hdr_next;
8003 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005 /* Iterate through the headers.
8006 * we start with the start line.
8007 */
8008 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008009 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010
8011 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8012 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008013 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014
8015 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008016 hdr_beg = hdr_next;
8017 hdr_end = hdr_beg + cur_hdr->len;
8018 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019
Willy Tarreau24581ba2010-08-31 22:39:35 +02008020 /* We have one full header between hdr_beg and hdr_end, and the
8021 * next header starts at hdr_next. We're only interested in
8022 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 */
8024
Willy Tarreau24581ba2010-08-31 22:39:35 +02008025 is_cookie2 = 0;
8026 prev = hdr_beg + 10;
8027 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008028 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8030 if (!val) {
8031 old_idx = cur_idx;
8032 continue;
8033 }
8034 is_cookie2 = 1;
8035 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 }
8037
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8039 * <prev> points to the colon.
8040 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008041 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042
Willy Tarreau24581ba2010-08-31 22:39:35 +02008043 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8044 * check-cache is enabled) and we are not interested in checking
8045 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008046 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008047 if (s->be->cookie_name == NULL &&
8048 s->be->appsession_name == NULL &&
8049 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 return;
8051
Willy Tarreau24581ba2010-08-31 22:39:35 +02008052 /* OK so now we know we have to process this response cookie.
8053 * The format of the Set-Cookie header is slightly different
8054 * from the format of the Cookie header in that it does not
8055 * support the comma as a cookie delimiter (thus the header
8056 * cannot be folded) because the Expires attribute described in
8057 * the original Netscape's spec may contain an unquoted date
8058 * with a comma inside. We have to live with this because
8059 * many browsers don't support Max-Age and some browsers don't
8060 * support quoted strings. However the Set-Cookie2 header is
8061 * clean.
8062 *
8063 * We have to keep multiple pointers in order to support cookie
8064 * removal at the beginning, middle or end of header without
8065 * corrupting the header (in case of set-cookie2). A special
8066 * pointer, <scav> points to the beginning of the set-cookie-av
8067 * fields after the first semi-colon. The <next> pointer points
8068 * either to the end of line (set-cookie) or next unquoted comma
8069 * (set-cookie2). All of these headers are valid :
8070 *
8071 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8072 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8073 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8074 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8075 * | | | | | | | | | |
8076 * | | | | | | | | +-> next hdr_end <--+
8077 * | | | | | | | +------------> scav
8078 * | | | | | | +--------------> val_end
8079 * | | | | | +--------------------> val_beg
8080 * | | | | +----------------------> equal
8081 * | | | +------------------------> att_end
8082 * | | +----------------------------> att_beg
8083 * | +------------------------------> prev
8084 * +-----------------------------------------> hdr_beg
8085 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086
Willy Tarreau24581ba2010-08-31 22:39:35 +02008087 for (; prev < hdr_end; prev = next) {
8088 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089
Willy Tarreau24581ba2010-08-31 22:39:35 +02008090 /* find att_beg */
8091 att_beg = prev + 1;
8092 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8093 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094
Willy Tarreau24581ba2010-08-31 22:39:35 +02008095 /* find att_end : this is the first character after the last non
8096 * space before the equal. It may be equal to hdr_end.
8097 */
8098 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099
Willy Tarreau24581ba2010-08-31 22:39:35 +02008100 while (equal < hdr_end) {
8101 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8102 break;
8103 if (http_is_spht[(unsigned char)*equal++])
8104 continue;
8105 att_end = equal;
8106 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107
Willy Tarreau24581ba2010-08-31 22:39:35 +02008108 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8109 * is between <att_beg> and <equal>, both may be identical.
8110 */
8111
8112 /* look for end of cookie if there is an equal sign */
8113 if (equal < hdr_end && *equal == '=') {
8114 /* look for the beginning of the value */
8115 val_beg = equal + 1;
8116 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8117 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118
Willy Tarreau24581ba2010-08-31 22:39:35 +02008119 /* find the end of the value, respecting quotes */
8120 next = find_cookie_value_end(val_beg, hdr_end);
8121
8122 /* make val_end point to the first white space or delimitor after the value */
8123 val_end = next;
8124 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8125 val_end--;
8126 } else {
8127 /* <equal> points to next comma, semi-colon or EOL */
8128 val_beg = val_end = next = equal;
8129 }
8130
8131 if (next < hdr_end) {
8132 /* Set-Cookie2 supports multiple cookies, and <next> points to
8133 * a colon or semi-colon before the end. So skip all attr-value
8134 * pairs and look for the next comma. For Set-Cookie, since
8135 * commas are permitted in values, skip to the end.
8136 */
8137 if (is_cookie2)
8138 next = find_hdr_value_end(next, hdr_end);
8139 else
8140 next = hdr_end;
8141 }
8142
8143 /* Now everything is as on the diagram above */
8144
8145 /* Ignore cookies with no equal sign */
8146 if (equal == val_end)
8147 continue;
8148
8149 /* If there are spaces around the equal sign, we need to
8150 * strip them otherwise we'll get trouble for cookie captures,
8151 * or even for rewrites. Since this happens extremely rarely,
8152 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008154 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8155 int stripped_before = 0;
8156 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157
Willy Tarreau24581ba2010-08-31 22:39:35 +02008158 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008159 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008160 equal += stripped_before;
8161 val_beg += stripped_before;
8162 }
8163
8164 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008165 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008166 val_beg += stripped_after;
8167 stripped_before += stripped_after;
8168 }
8169
8170 val_end += stripped_before;
8171 next += stripped_before;
8172 hdr_end += stripped_before;
8173 hdr_next += stripped_before;
8174 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008175 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008176 }
8177
8178 /* First, let's see if we want to capture this cookie. We check
8179 * that we don't already have a server side cookie, because we
8180 * can only capture one. Also as an optimisation, we ignore
8181 * cookies shorter than the declared name.
8182 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008183 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008184 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008185 (val_end - att_beg >= s->fe->capture_namelen) &&
8186 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008187 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008188 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008189 Alert("HTTP logging : out of memory.\n");
8190 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008191 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008192 if (log_len > s->fe->capture_len)
8193 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008194 memcpy(txn->srv_cookie, att_beg, log_len);
8195 txn->srv_cookie[log_len] = 0;
8196 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 }
8198
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008199 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008200 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008201 if (!(s->flags & SN_IGNORE_PRST) &&
8202 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8203 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008204 /* assume passive cookie by default */
8205 txn->flags &= ~TX_SCK_MASK;
8206 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207
8208 /* If the cookie is in insert mode on a known server, we'll delete
8209 * this occurrence because we'll insert another one later.
8210 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 * a direct access.
8212 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008213 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008214 /* The "preserve" flag was set, we don't want to touch the
8215 * server's cookie.
8216 */
8217 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008218 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8219 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008220 /* this cookie must be deleted */
8221 if (*prev == ':' && next == hdr_end) {
8222 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008223 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8225 txn->hdr_idx.used--;
8226 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008227 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 hdr_next += delta;
8229 http_msg_move_end(&txn->rsp, delta);
8230 /* note: while both invalid now, <next> and <hdr_end>
8231 * are still equal, so the for() will stop as expected.
8232 */
8233 } else {
8234 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008235 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 next = prev;
8237 hdr_end += delta;
8238 hdr_next += delta;
8239 cur_hdr->len += delta;
8240 http_msg_move_end(&txn->rsp, delta);
8241 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008242 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008243 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008244 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008247 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248 * with this server since we know it.
8249 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008250 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 next += delta;
8252 hdr_end += delta;
8253 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008255 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256
Willy Tarreauf1348312010-10-07 15:54:11 +02008257 txn->flags &= ~TX_SCK_MASK;
8258 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008260 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008262 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008263 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008264 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 next += delta;
8266 hdr_end += delta;
8267 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008269 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
Willy Tarreau827aee92011-03-10 16:55:02 +01008271 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008272 txn->flags &= ~TX_SCK_MASK;
8273 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 }
8275 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008276 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008277 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008278 int cmp_len, value_len;
8279 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008280
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008281 if (s->be->options2 & PR_O2_AS_PFX) {
8282 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8283 value_begin = att_beg + s->be->appsession_name_len;
8284 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008285 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 cmp_len = att_end - att_beg;
8287 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008288 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008289 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008290
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008291 if ((cmp_len == s->be->appsession_name_len) &&
8292 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 /* free a possibly previously allocated memory */
8294 pool_free2(apools.sessid, txn->sessid);
8295
Cyril Bontéb21570a2009-11-29 20:04:48 +01008296 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008297 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008298 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008299 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008300 return;
8301 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008302 memcpy(txn->sessid, value_begin, value_len);
8303 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 }
8306 /* that's done for this cookie, check the next one on the same
8307 * line when next != hdr_end (only if is_cookie2).
8308 */
8309 }
8310 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008311 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008313
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008314 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008315 appsess *asession = NULL;
8316 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008317 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008318 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008319 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008320 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8321 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008322 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008323 return;
8324 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008325 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8326
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008327 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8328 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008329 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8330 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008331 return;
8332 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008333 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8334 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008335
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008336 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008337 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008338 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008339 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8340 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008341 return;
8342 }
8343 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008344 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008345
8346 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008347 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008348 }
8349
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008350 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008351 asession->request_count++;
8352 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008353}
8354
8355
Willy Tarreaua15645d2007-03-18 16:22:39 +01008356/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008357 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008359void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008360{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362 char *p1, *p2;
8363
8364 char *cur_ptr, *cur_end, *cur_next;
8365 int cur_idx;
8366
Willy Tarreau5df51872007-11-25 16:20:08 +01008367 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008368 return;
8369
8370 /* Iterate through the headers.
8371 * we start with the start line.
8372 */
8373 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008374 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375
8376 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8377 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008378 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008379
8380 cur_hdr = &txn->hdr_idx.v[cur_idx];
8381 cur_ptr = cur_next;
8382 cur_end = cur_ptr + cur_hdr->len;
8383 cur_next = cur_end + cur_hdr->cr + 1;
8384
8385 /* We have one full header between cur_ptr and cur_end, and the
8386 * next header starts at cur_next. We're only interested in
8387 * "Cookie:" headers.
8388 */
8389
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008390 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8391 if (val) {
8392 if ((cur_end - (cur_ptr + val) >= 8) &&
8393 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8394 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8395 return;
8396 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397 }
8398
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008399 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8400 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008401 continue;
8402
8403 /* OK, right now we know we have a cache-control header at cur_ptr */
8404
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008405 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406
8407 if (p1 >= cur_end) /* no more info */
8408 continue;
8409
8410 /* p1 is at the beginning of the value */
8411 p2 = p1;
8412
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008413 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414 p2++;
8415
8416 /* we have a complete value between p1 and p2 */
8417 if (p2 < cur_end && *p2 == '=') {
8418 /* we have something of the form no-cache="set-cookie" */
8419 if ((cur_end - p1 >= 21) &&
8420 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8421 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008422 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 continue;
8424 }
8425
8426 /* OK, so we know that either p2 points to the end of string or to a comma */
8427 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008428 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008429 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8430 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8431 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008432 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 return;
8434 }
8435
8436 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008437 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008438 continue;
8439 }
8440 }
8441}
8442
8443
Willy Tarreau58f10d72006-12-04 02:26:12 +01008444/*
8445 * Try to retrieve a known appsession in the URI, then the associated server.
8446 * If the server is found, it's assigned to the session.
8447 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008448void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008449{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008450 char *end_params, *first_param, *cur_param, *next_param;
8451 char separator;
8452 int value_len;
8453
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008455
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008456 if (s->be->appsession_name == NULL ||
8457 (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 +01008458 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008459 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008460
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 first_param = NULL;
8462 switch (mode) {
8463 case PR_O2_AS_M_PP:
8464 first_param = memchr(begin, ';', len);
8465 break;
8466 case PR_O2_AS_M_QS:
8467 first_param = memchr(begin, '?', len);
8468 break;
8469 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008470
Cyril Bontéb21570a2009-11-29 20:04:48 +01008471 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008472 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008473 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008474
Cyril Bontéb21570a2009-11-29 20:04:48 +01008475 switch (mode) {
8476 case PR_O2_AS_M_PP:
8477 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8478 end_params = (char *) begin + len;
8479 }
8480 separator = ';';
8481 break;
8482 case PR_O2_AS_M_QS:
8483 end_params = (char *) begin + len;
8484 separator = '&';
8485 break;
8486 default:
8487 /* unknown mode, shouldn't happen */
8488 return;
8489 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008490
Cyril Bontéb21570a2009-11-29 20:04:48 +01008491 cur_param = next_param = end_params;
8492 while (cur_param > first_param) {
8493 cur_param--;
8494 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8495 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008496 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8497 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8498 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008499 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008500 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8501 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008502 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008503 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008504 }
8505 break;
8506 }
8507 next_param = cur_param;
8508 }
8509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008510#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008511 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008512 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008513#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008514}
8515
Willy Tarreaub2513902006-12-17 14:52:38 +01008516/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008517 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008518 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008519 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008520 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008521 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008522 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008523 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008524 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008525int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008526{
8527 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008528 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008529 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008530
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008531 if (!uri_auth)
8532 return 0;
8533
Cyril Bonté70be45d2010-10-12 00:14:35 +02008534 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008535 return 0;
8536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008537 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008538 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008539 return 0;
8540
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008541 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008542 return 0;
8543
Willy Tarreaub2513902006-12-17 14:52:38 +01008544 return 1;
8545}
8546
Willy Tarreau4076a152009-04-02 15:18:36 +02008547/*
8548 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008549 * By default it tries to report the error position as msg->err_pos. However if
8550 * this one is not set, it will then report msg->next, which is the last known
8551 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008552 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008553 */
8554void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008555 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008556 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008557{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008558 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008559 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008560
Willy Tarreau9b28e032012-10-12 23:49:43 +02008561 es->len = MIN(chn->buf->i, sizeof(es->buf));
8562 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008563 len1 = MIN(len1, es->len);
8564 len2 = es->len - len1; /* remaining data if buffer wraps */
8565
Willy Tarreau9b28e032012-10-12 23:49:43 +02008566 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008567 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008568 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008569
Willy Tarreau4076a152009-04-02 15:18:36 +02008570 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008571 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008572 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008573 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008574
Willy Tarreau4076a152009-04-02 15:18:36 +02008575 es->when = date; // user-visible date
8576 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008577 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008578 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008579 if (objt_conn(s->req->prod->end))
8580 es->src = __objt_conn(s->req->prod->end)->addr.from;
8581 else
8582 memset(&es->src, 0, sizeof(es->src));
8583
Willy Tarreau078272e2010-12-12 12:46:33 +01008584 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008585 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008586 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008587 es->s_flags = s->flags;
8588 es->t_flags = s->txn.flags;
8589 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008590 es->b_out = chn->buf->o;
8591 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008592 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008593 es->m_clen = msg->chunk_len;
8594 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008595}
Willy Tarreaub2513902006-12-17 14:52:38 +01008596
Willy Tarreau294c4732011-12-16 21:35:50 +01008597/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8598 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8599 * performed over the whole headers. Otherwise it must contain a valid header
8600 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8601 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8602 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8603 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008604 * -1. The value fetch stops at commas, so this function is suited for use with
8605 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008606 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008607 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008608unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008609 struct hdr_idx *idx, int occ,
8610 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008611{
Willy Tarreau294c4732011-12-16 21:35:50 +01008612 struct hdr_ctx local_ctx;
8613 char *ptr_hist[MAX_HDR_HISTORY];
8614 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008615 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008616 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008617
Willy Tarreau294c4732011-12-16 21:35:50 +01008618 if (!ctx) {
8619 local_ctx.idx = 0;
8620 ctx = &local_ctx;
8621 }
8622
Willy Tarreaubce70882009-09-07 11:51:47 +02008623 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008624 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008625 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008626 occ--;
8627 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008628 *vptr = ctx->line + ctx->val;
8629 *vlen = ctx->vlen;
8630 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008631 }
8632 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008633 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008634 }
8635
8636 /* negative occurrence, we scan all the list then walk back */
8637 if (-occ > MAX_HDR_HISTORY)
8638 return 0;
8639
Willy Tarreau294c4732011-12-16 21:35:50 +01008640 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008641 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008642 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8643 len_hist[hist_ptr] = ctx->vlen;
8644 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008645 hist_ptr = 0;
8646 found++;
8647 }
8648 if (-occ > found)
8649 return 0;
8650 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008651 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8652 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8653 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008654 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008655 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008656 if (hist_ptr >= MAX_HDR_HISTORY)
8657 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008658 *vptr = ptr_hist[hist_ptr];
8659 *vlen = len_hist[hist_ptr];
8660 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008661}
8662
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008663/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8664 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8665 * performed over the whole headers. Otherwise it must contain a valid header
8666 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8667 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8668 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8669 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8670 * -1. This function differs from http_get_hdr() in that it only returns full
8671 * line header values and does not stop at commas.
8672 * The return value is 0 if nothing was found, or non-zero otherwise.
8673 */
8674unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8675 struct hdr_idx *idx, int occ,
8676 struct hdr_ctx *ctx, char **vptr, int *vlen)
8677{
8678 struct hdr_ctx local_ctx;
8679 char *ptr_hist[MAX_HDR_HISTORY];
8680 int len_hist[MAX_HDR_HISTORY];
8681 unsigned int hist_ptr;
8682 int found;
8683
8684 if (!ctx) {
8685 local_ctx.idx = 0;
8686 ctx = &local_ctx;
8687 }
8688
8689 if (occ >= 0) {
8690 /* search from the beginning */
8691 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8692 occ--;
8693 if (occ <= 0) {
8694 *vptr = ctx->line + ctx->val;
8695 *vlen = ctx->vlen;
8696 return 1;
8697 }
8698 }
8699 return 0;
8700 }
8701
8702 /* negative occurrence, we scan all the list then walk back */
8703 if (-occ > MAX_HDR_HISTORY)
8704 return 0;
8705
8706 found = hist_ptr = 0;
8707 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8708 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8709 len_hist[hist_ptr] = ctx->vlen;
8710 if (++hist_ptr >= MAX_HDR_HISTORY)
8711 hist_ptr = 0;
8712 found++;
8713 }
8714 if (-occ > found)
8715 return 0;
8716 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8717 * find occurrence -occ, so we have to check [hist_ptr+occ].
8718 */
8719 hist_ptr += occ;
8720 if (hist_ptr >= MAX_HDR_HISTORY)
8721 hist_ptr -= MAX_HDR_HISTORY;
8722 *vptr = ptr_hist[hist_ptr];
8723 *vlen = len_hist[hist_ptr];
8724 return 1;
8725}
8726
Willy Tarreaubaaee002006-06-26 02:48:02 +02008727/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008728 * Print a debug line with a header. Always stop at the first CR or LF char,
8729 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8730 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008731 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008732void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008733{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008734 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008735 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008736 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008737 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8738 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 +02008739
8740 for (max = 0; start + max < end; max++)
8741 if (start[max] == '\r' || start[max] == '\n')
8742 break;
8743
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008744 UBOUND(max, trash.size - trash.len - 3);
8745 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8746 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008747 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008748}
8749
Willy Tarreau0937bc42009-12-22 15:03:09 +01008750/*
8751 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8752 * the required fields are properly allocated and that we only need to (re)init
8753 * them. This should be used before processing any new request.
8754 */
8755void http_init_txn(struct session *s)
8756{
8757 struct http_txn *txn = &s->txn;
8758 struct proxy *fe = s->fe;
8759
8760 txn->flags = 0;
8761 txn->status = -1;
8762
Willy Tarreauf64d1412010-10-07 20:06:11 +02008763 txn->cookie_first_date = 0;
8764 txn->cookie_last_date = 0;
8765
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008766 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008767 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008768 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008769 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008770 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008771 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008772 txn->req.chunk_len = 0LL;
8773 txn->req.body_len = 0LL;
8774 txn->rsp.chunk_len = 0LL;
8775 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008776 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8777 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008778 txn->req.chn = s->req;
8779 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008780
8781 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008782
8783 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8784 if (fe->options2 & PR_O2_REQBUG_OK)
8785 txn->req.err_pos = -1; /* let buggy requests pass */
8786
Willy Tarreau46023632010-01-07 22:51:47 +01008787 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008788 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8789
Willy Tarreau46023632010-01-07 22:51:47 +01008790 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008791 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8792
8793 if (txn->hdr_idx.v)
8794 hdr_idx_init(&txn->hdr_idx);
8795}
8796
8797/* to be used at the end of a transaction */
8798void http_end_txn(struct session *s)
8799{
8800 struct http_txn *txn = &s->txn;
8801
Willy Tarreau75195602014-03-11 15:48:55 +01008802 /* release any possible compression context */
8803 if (s->flags & SN_COMP_READY)
8804 s->comp_algo->end(&s->comp_ctx);
8805 s->comp_algo = NULL;
8806 s->flags &= ~SN_COMP_READY;
8807
Willy Tarreau0937bc42009-12-22 15:03:09 +01008808 /* these ones will have been dynamically allocated */
8809 pool_free2(pool2_requri, txn->uri);
8810 pool_free2(pool2_capture, txn->cli_cookie);
8811 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008812 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008813 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008814
William Lallemanda73203e2012-03-12 12:48:57 +01008815 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008816 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008817 txn->uri = NULL;
8818 txn->srv_cookie = NULL;
8819 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008820
8821 if (txn->req.cap) {
8822 struct cap_hdr *h;
8823 for (h = s->fe->req_cap; h; h = h->next)
8824 pool_free2(h->pool, txn->req.cap[h->index]);
8825 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8826 }
8827
8828 if (txn->rsp.cap) {
8829 struct cap_hdr *h;
8830 for (h = s->fe->rsp_cap; h; h = h->next)
8831 pool_free2(h->pool, txn->rsp.cap[h->index]);
8832 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8833 }
8834
Willy Tarreau0937bc42009-12-22 15:03:09 +01008835}
8836
8837/* to be used at the end of a transaction to prepare a new one */
8838void http_reset_txn(struct session *s)
8839{
8840 http_end_txn(s);
8841 http_init_txn(s);
8842
8843 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008845 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008846 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008847 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008848 /* re-init store persistence */
8849 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008850 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008851
Willy Tarreau0937bc42009-12-22 15:03:09 +01008852 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008853
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008854 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008855
Willy Tarreau739cfba2010-01-25 23:11:14 +01008856 /* We must trim any excess data from the response buffer, because we
8857 * may have blocked an invalid response from a server that we don't
8858 * want to accidentely forward once we disable the analysers, nor do
8859 * we want those data to come along with next response. A typical
8860 * example of such data would be from a buggy server responding to
8861 * a HEAD with some data, or sending more than the advertised
8862 * content-length.
8863 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008864 if (unlikely(s->rep->buf->i))
8865 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008866
Willy Tarreau0937bc42009-12-22 15:03:09 +01008867 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008868 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008869
Willy Tarreaud04e8582010-05-31 12:31:35 +02008870 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008872
8873 s->req->rex = TICK_ETERNITY;
8874 s->req->wex = TICK_ETERNITY;
8875 s->req->analyse_exp = TICK_ETERNITY;
8876 s->rep->rex = TICK_ETERNITY;
8877 s->rep->wex = TICK_ETERNITY;
8878 s->rep->analyse_exp = TICK_ETERNITY;
8879}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008880
Sasha Pachev218f0642014-06-16 12:05:59 -06008881void free_http_res_rules(struct list *r)
8882{
8883 struct http_res_rule *tr, *pr;
8884
8885 list_for_each_entry_safe(pr, tr, r, list) {
8886 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008887 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008888 free(pr);
8889 }
8890}
8891
8892void free_http_req_rules(struct list *r)
8893{
Willy Tarreauff011f22011-01-06 17:51:27 +01008894 struct http_req_rule *tr, *pr;
8895
8896 list_for_each_entry_safe(pr, tr, r, list) {
8897 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008898 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008899 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008900
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008901 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008902 free(pr);
8903 }
8904}
8905
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008906/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008907struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8908{
8909 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008910 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008911 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008912 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008913
8914 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8915 if (!rule) {
8916 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008917 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008918 }
8919
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008920 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008921 rule->action = HTTP_REQ_ACT_ALLOW;
8922 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008923 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008924 rule->action = HTTP_REQ_ACT_DENY;
8925 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008926 } else if (!strcmp(args[0], "tarpit")) {
8927 rule->action = HTTP_REQ_ACT_TARPIT;
8928 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008929 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008930 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008931 cur_arg = 1;
8932
8933 while(*args[cur_arg]) {
8934 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008935 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008936 cur_arg+=2;
8937 continue;
8938 } else
8939 break;
8940 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008941 } else if (!strcmp(args[0], "set-nice")) {
8942 rule->action = HTTP_REQ_ACT_SET_NICE;
8943 cur_arg = 1;
8944
8945 if (!*args[cur_arg] ||
8946 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8947 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8948 file, linenum, args[0]);
8949 goto out_err;
8950 }
8951 rule->arg.nice = atoi(args[cur_arg]);
8952 if (rule->arg.nice < -1024)
8953 rule->arg.nice = -1024;
8954 else if (rule->arg.nice > 1024)
8955 rule->arg.nice = 1024;
8956 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008957 } else if (!strcmp(args[0], "set-tos")) {
8958#ifdef IP_TOS
8959 char *err;
8960 rule->action = HTTP_REQ_ACT_SET_TOS;
8961 cur_arg = 1;
8962
8963 if (!*args[cur_arg] ||
8964 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8965 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8966 file, linenum, args[0]);
8967 goto out_err;
8968 }
8969
8970 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8971 if (err && *err != '\0') {
8972 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8973 file, linenum, err, args[0]);
8974 goto out_err;
8975 }
8976 cur_arg++;
8977#else
8978 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8979 goto out_err;
8980#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008981 } else if (!strcmp(args[0], "set-mark")) {
8982#ifdef SO_MARK
8983 char *err;
8984 rule->action = HTTP_REQ_ACT_SET_MARK;
8985 cur_arg = 1;
8986
8987 if (!*args[cur_arg] ||
8988 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8989 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8990 file, linenum, args[0]);
8991 goto out_err;
8992 }
8993
8994 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8995 if (err && *err != '\0') {
8996 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8997 file, linenum, err, args[0]);
8998 goto out_err;
8999 }
9000 cur_arg++;
9001 global.last_checks |= LSTCHK_NETADM;
9002#else
9003 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9004 goto out_err;
9005#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009006 } else if (!strcmp(args[0], "set-log-level")) {
9007 rule->action = HTTP_REQ_ACT_SET_LOGL;
9008 cur_arg = 1;
9009
9010 if (!*args[cur_arg] ||
9011 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9012 bad_log_level:
9013 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9014 file, linenum, args[0]);
9015 goto out_err;
9016 }
9017 if (strcmp(args[cur_arg], "silent") == 0)
9018 rule->arg.loglevel = -1;
9019 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9020 goto bad_log_level;
9021 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009022 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9023 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9024 cur_arg = 1;
9025
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009026 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9027 (*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 +01009028 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9029 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009030 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009031 }
9032
9033 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9034 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9035 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009036
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009037 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009038 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009039 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9040 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009041 free(proxy->conf.lfs_file);
9042 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9043 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009044 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009045 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9046 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009047 cur_arg = 1;
9048
9049 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009050 (*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 -06009051 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9052 file, linenum, args[0]);
9053 goto out_err;
9054 }
9055
9056 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9057 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9058 LIST_INIT(&rule->arg.hdr_add.fmt);
9059
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009060 error = NULL;
9061 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9062 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9063 args[cur_arg + 1], error);
9064 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009065 goto out_err;
9066 }
9067
9068 proxy->conf.args.ctx = ARGC_HRQ;
9069 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9070 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9071 file, linenum);
9072
9073 free(proxy->conf.lfs_file);
9074 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9075 proxy->conf.lfs_line = proxy->conf.args.line;
9076 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009077 } else if (strcmp(args[0], "del-header") == 0) {
9078 rule->action = HTTP_REQ_ACT_DEL_HDR;
9079 cur_arg = 1;
9080
9081 if (!*args[cur_arg] ||
9082 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9083 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9084 file, linenum, args[0]);
9085 goto out_err;
9086 }
9087
9088 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9089 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9090
9091 proxy->conf.args.ctx = ARGC_HRQ;
9092 free(proxy->conf.lfs_file);
9093 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9094 proxy->conf.lfs_line = proxy->conf.args.line;
9095 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009096 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9097 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009098 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009099 struct sample_expr *expr;
9100 unsigned int where;
9101 char *err = NULL;
9102
9103 cur_arg = 1;
9104 proxy->conf.args.ctx = ARGC_TRK;
9105
9106 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9107 if (!expr) {
9108 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9109 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9110 free(err);
9111 goto out_err;
9112 }
9113
9114 where = 0;
9115 if (proxy->cap & PR_CAP_FE)
9116 where |= SMP_VAL_FE_HRQ_HDR;
9117 if (proxy->cap & PR_CAP_BE)
9118 where |= SMP_VAL_BE_HRQ_HDR;
9119
9120 if (!(expr->fetch->val & where)) {
9121 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9122 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9123 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9124 args[cur_arg-1], sample_src_names(expr->fetch->use));
9125 free(expr);
9126 goto out_err;
9127 }
9128
9129 if (strcmp(args[cur_arg], "table") == 0) {
9130 cur_arg++;
9131 if (!args[cur_arg]) {
9132 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9133 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9134 free(expr);
9135 goto out_err;
9136 }
9137 /* we copy the table name for now, it will be resolved later */
9138 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9139 cur_arg++;
9140 }
9141 rule->act_prm.trk_ctr.expr = expr;
9142 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009143 } else if (strcmp(args[0], "redirect") == 0) {
9144 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009145 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009146
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009147 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009148 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9149 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9150 goto out_err;
9151 }
9152
9153 /* this redirect rule might already contain a parsed condition which
9154 * we'll pass to the http-request rule.
9155 */
9156 rule->action = HTTP_REQ_ACT_REDIR;
9157 rule->arg.redir = redir;
9158 rule->cond = redir->cond;
9159 redir->cond = NULL;
9160 cur_arg = 2;
9161 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009162 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9163 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9164 rule->action = HTTP_REQ_ACT_ADD_ACL;
9165 /*
9166 * '+ 8' for 'add-acl('
9167 * '- 9' for 'add-acl(' + trailing ')'
9168 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009169 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009170
9171 cur_arg = 1;
9172
9173 if (!*args[cur_arg] ||
9174 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9175 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9176 file, linenum, args[0]);
9177 goto out_err;
9178 }
9179
9180 LIST_INIT(&rule->arg.map.key);
9181 proxy->conf.args.ctx = ARGC_HRQ;
9182 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9183 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9184 file, linenum);
9185 free(proxy->conf.lfs_file);
9186 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9187 proxy->conf.lfs_line = proxy->conf.args.line;
9188 cur_arg += 1;
9189 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9190 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9191 rule->action = HTTP_REQ_ACT_DEL_ACL;
9192 /*
9193 * '+ 8' for 'del-acl('
9194 * '- 9' for 'del-acl(' + trailing ')'
9195 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009196 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009197
9198 cur_arg = 1;
9199
9200 if (!*args[cur_arg] ||
9201 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9202 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9203 file, linenum, args[0]);
9204 goto out_err;
9205 }
9206
9207 LIST_INIT(&rule->arg.map.key);
9208 proxy->conf.args.ctx = ARGC_HRQ;
9209 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9210 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9211 file, linenum);
9212 free(proxy->conf.lfs_file);
9213 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9214 proxy->conf.lfs_line = proxy->conf.args.line;
9215 cur_arg += 1;
9216 } else if (strncmp(args[0], "del-map", 7) == 0) {
9217 /* http-request del-map(<reference (map name)>) <key pattern> */
9218 rule->action = HTTP_REQ_ACT_DEL_MAP;
9219 /*
9220 * '+ 8' for 'del-map('
9221 * '- 9' for 'del-map(' + trailing ')'
9222 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009223 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009224
9225 cur_arg = 1;
9226
9227 if (!*args[cur_arg] ||
9228 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9229 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9230 file, linenum, args[0]);
9231 goto out_err;
9232 }
9233
9234 LIST_INIT(&rule->arg.map.key);
9235 proxy->conf.args.ctx = ARGC_HRQ;
9236 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9237 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9238 file, linenum);
9239 free(proxy->conf.lfs_file);
9240 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9241 proxy->conf.lfs_line = proxy->conf.args.line;
9242 cur_arg += 1;
9243 } else if (strncmp(args[0], "set-map", 7) == 0) {
9244 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9245 rule->action = HTTP_REQ_ACT_SET_MAP;
9246 /*
9247 * '+ 8' for 'set-map('
9248 * '- 9' for 'set-map(' + trailing ')'
9249 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009250 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009251
9252 cur_arg = 1;
9253
9254 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9255 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9256 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9257 file, linenum, args[0]);
9258 goto out_err;
9259 }
9260
9261 LIST_INIT(&rule->arg.map.key);
9262 LIST_INIT(&rule->arg.map.value);
9263 proxy->conf.args.ctx = ARGC_HRQ;
9264
9265 /* key pattern */
9266 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9267 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9268 file, linenum);
9269
9270 /* value pattern */
9271 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9272 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9273 file, linenum);
9274 free(proxy->conf.lfs_file);
9275 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9276 proxy->conf.lfs_line = proxy->conf.args.line;
9277
9278 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009279 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9280 char *errmsg = NULL;
9281 cur_arg = 1;
9282 /* try in the module list */
9283 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9284 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9285 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9286 free(errmsg);
9287 goto out_err;
9288 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009289 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009290 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 +01009291 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009292 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009293 }
9294
9295 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9296 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009297 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009298
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009299 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9300 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9301 file, linenum, args[0], errmsg);
9302 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009303 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009304 }
9305 rule->cond = cond;
9306 }
9307 else if (*args[cur_arg]) {
9308 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9309 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9310 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009311 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009312 }
9313
9314 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009315 out_err:
9316 free(rule);
9317 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009318}
9319
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009320/* parse an "http-respose" rule */
9321struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9322{
9323 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009324 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009325 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009326 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009327
9328 rule = calloc(1, sizeof(*rule));
9329 if (!rule) {
9330 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9331 goto out_err;
9332 }
9333
9334 if (!strcmp(args[0], "allow")) {
9335 rule->action = HTTP_RES_ACT_ALLOW;
9336 cur_arg = 1;
9337 } else if (!strcmp(args[0], "deny")) {
9338 rule->action = HTTP_RES_ACT_DENY;
9339 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009340 } else if (!strcmp(args[0], "set-nice")) {
9341 rule->action = HTTP_RES_ACT_SET_NICE;
9342 cur_arg = 1;
9343
9344 if (!*args[cur_arg] ||
9345 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9346 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9347 file, linenum, args[0]);
9348 goto out_err;
9349 }
9350 rule->arg.nice = atoi(args[cur_arg]);
9351 if (rule->arg.nice < -1024)
9352 rule->arg.nice = -1024;
9353 else if (rule->arg.nice > 1024)
9354 rule->arg.nice = 1024;
9355 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009356 } else if (!strcmp(args[0], "set-tos")) {
9357#ifdef IP_TOS
9358 char *err;
9359 rule->action = HTTP_RES_ACT_SET_TOS;
9360 cur_arg = 1;
9361
9362 if (!*args[cur_arg] ||
9363 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9364 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9365 file, linenum, args[0]);
9366 goto out_err;
9367 }
9368
9369 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9370 if (err && *err != '\0') {
9371 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9372 file, linenum, err, args[0]);
9373 goto out_err;
9374 }
9375 cur_arg++;
9376#else
9377 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9378 goto out_err;
9379#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009380 } else if (!strcmp(args[0], "set-mark")) {
9381#ifdef SO_MARK
9382 char *err;
9383 rule->action = HTTP_RES_ACT_SET_MARK;
9384 cur_arg = 1;
9385
9386 if (!*args[cur_arg] ||
9387 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9388 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9389 file, linenum, args[0]);
9390 goto out_err;
9391 }
9392
9393 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9394 if (err && *err != '\0') {
9395 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9396 file, linenum, err, args[0]);
9397 goto out_err;
9398 }
9399 cur_arg++;
9400 global.last_checks |= LSTCHK_NETADM;
9401#else
9402 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9403 goto out_err;
9404#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009405 } else if (!strcmp(args[0], "set-log-level")) {
9406 rule->action = HTTP_RES_ACT_SET_LOGL;
9407 cur_arg = 1;
9408
9409 if (!*args[cur_arg] ||
9410 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9411 bad_log_level:
9412 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9413 file, linenum, args[0]);
9414 goto out_err;
9415 }
9416 if (strcmp(args[cur_arg], "silent") == 0)
9417 rule->arg.loglevel = -1;
9418 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9419 goto bad_log_level;
9420 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009421 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9422 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9423 cur_arg = 1;
9424
9425 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9426 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9427 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9428 file, linenum, args[0]);
9429 goto out_err;
9430 }
9431
9432 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9433 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9434 LIST_INIT(&rule->arg.hdr_add.fmt);
9435
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009436 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009437 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009438 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9439 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009440 free(proxy->conf.lfs_file);
9441 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9442 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009443 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009444 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009445 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009446 cur_arg = 1;
9447
9448 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009449 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9450 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009451 file, linenum, args[0]);
9452 goto out_err;
9453 }
9454
9455 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9456 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9457 LIST_INIT(&rule->arg.hdr_add.fmt);
9458
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009459 error = NULL;
9460 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9461 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9462 args[cur_arg + 1], error);
9463 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009464 goto out_err;
9465 }
9466
9467 proxy->conf.args.ctx = ARGC_HRQ;
9468 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9469 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9470 file, linenum);
9471
9472 free(proxy->conf.lfs_file);
9473 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9474 proxy->conf.lfs_line = proxy->conf.args.line;
9475 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009476 } else if (strcmp(args[0], "del-header") == 0) {
9477 rule->action = HTTP_RES_ACT_DEL_HDR;
9478 cur_arg = 1;
9479
9480 if (!*args[cur_arg] ||
9481 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9482 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9483 file, linenum, args[0]);
9484 goto out_err;
9485 }
9486
9487 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9488 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9489
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009490 proxy->conf.args.ctx = ARGC_HRS;
9491 free(proxy->conf.lfs_file);
9492 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9493 proxy->conf.lfs_line = proxy->conf.args.line;
9494 cur_arg += 1;
9495 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9496 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9497 rule->action = HTTP_RES_ACT_ADD_ACL;
9498 /*
9499 * '+ 8' for 'add-acl('
9500 * '- 9' for 'add-acl(' + trailing ')'
9501 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009502 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009503
9504 cur_arg = 1;
9505
9506 if (!*args[cur_arg] ||
9507 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9508 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9509 file, linenum, args[0]);
9510 goto out_err;
9511 }
9512
9513 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009514 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009515 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9516 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9517 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009518 free(proxy->conf.lfs_file);
9519 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9520 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009521
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009522 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009523 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9524 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9525 rule->action = HTTP_RES_ACT_DEL_ACL;
9526 /*
9527 * '+ 8' for 'del-acl('
9528 * '- 9' for 'del-acl(' + trailing ')'
9529 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009530 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009531
9532 cur_arg = 1;
9533
9534 if (!*args[cur_arg] ||
9535 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9536 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9537 file, linenum, args[0]);
9538 goto out_err;
9539 }
9540
9541 LIST_INIT(&rule->arg.map.key);
9542 proxy->conf.args.ctx = ARGC_HRS;
9543 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9544 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9545 file, linenum);
9546 free(proxy->conf.lfs_file);
9547 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9548 proxy->conf.lfs_line = proxy->conf.args.line;
9549 cur_arg += 1;
9550 } else if (strncmp(args[0], "del-map", 7) == 0) {
9551 /* http-response del-map(<reference (map name)>) <key pattern> */
9552 rule->action = HTTP_RES_ACT_DEL_MAP;
9553 /*
9554 * '+ 8' for 'del-map('
9555 * '- 9' for 'del-map(' + trailing ')'
9556 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009557 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009558
9559 cur_arg = 1;
9560
9561 if (!*args[cur_arg] ||
9562 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9563 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9564 file, linenum, args[0]);
9565 goto out_err;
9566 }
9567
9568 LIST_INIT(&rule->arg.map.key);
9569 proxy->conf.args.ctx = ARGC_HRS;
9570 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9571 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9572 file, linenum);
9573 free(proxy->conf.lfs_file);
9574 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9575 proxy->conf.lfs_line = proxy->conf.args.line;
9576 cur_arg += 1;
9577 } else if (strncmp(args[0], "set-map", 7) == 0) {
9578 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9579 rule->action = HTTP_RES_ACT_SET_MAP;
9580 /*
9581 * '+ 8' for 'set-map('
9582 * '- 9' for 'set-map(' + trailing ')'
9583 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009584 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009585
9586 cur_arg = 1;
9587
9588 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9589 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9590 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9591 file, linenum, args[0]);
9592 goto out_err;
9593 }
9594
9595 LIST_INIT(&rule->arg.map.key);
9596 LIST_INIT(&rule->arg.map.value);
9597
9598 proxy->conf.args.ctx = ARGC_HRS;
9599
9600 /* key pattern */
9601 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9602 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9603 file, linenum);
9604
9605 /* value pattern */
9606 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9607 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9608 file, linenum);
9609
9610 free(proxy->conf.lfs_file);
9611 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9612 proxy->conf.lfs_line = proxy->conf.args.line;
9613
9614 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009615 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9616 char *errmsg = NULL;
9617 cur_arg = 1;
9618 /* try in the module list */
9619 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9620 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9621 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9622 free(errmsg);
9623 goto out_err;
9624 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009625 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009626 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 +02009627 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9628 goto out_err;
9629 }
9630
9631 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9632 struct acl_cond *cond;
9633 char *errmsg = NULL;
9634
9635 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9636 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9637 file, linenum, args[0], errmsg);
9638 free(errmsg);
9639 goto out_err;
9640 }
9641 rule->cond = cond;
9642 }
9643 else if (*args[cur_arg]) {
9644 Alert("parsing [%s:%d]: 'http-response %s' expects"
9645 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9646 file, linenum, args[0], args[cur_arg]);
9647 goto out_err;
9648 }
9649
9650 return rule;
9651 out_err:
9652 free(rule);
9653 return NULL;
9654}
9655
Willy Tarreau4baae242012-12-27 12:00:31 +01009656/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009657 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9658 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009659 */
9660struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009661 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009662{
9663 struct redirect_rule *rule;
9664 int cur_arg;
9665 int type = REDIRECT_TYPE_NONE;
9666 int code = 302;
9667 const char *destination = NULL;
9668 const char *cookie = NULL;
9669 int cookie_set = 0;
9670 unsigned int flags = REDIRECT_FLAG_NONE;
9671 struct acl_cond *cond = NULL;
9672
9673 cur_arg = 0;
9674 while (*(args[cur_arg])) {
9675 if (strcmp(args[cur_arg], "location") == 0) {
9676 if (!*args[cur_arg + 1])
9677 goto missing_arg;
9678
9679 type = REDIRECT_TYPE_LOCATION;
9680 cur_arg++;
9681 destination = args[cur_arg];
9682 }
9683 else if (strcmp(args[cur_arg], "prefix") == 0) {
9684 if (!*args[cur_arg + 1])
9685 goto missing_arg;
9686
9687 type = REDIRECT_TYPE_PREFIX;
9688 cur_arg++;
9689 destination = args[cur_arg];
9690 }
9691 else if (strcmp(args[cur_arg], "scheme") == 0) {
9692 if (!*args[cur_arg + 1])
9693 goto missing_arg;
9694
9695 type = REDIRECT_TYPE_SCHEME;
9696 cur_arg++;
9697 destination = args[cur_arg];
9698 }
9699 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9700 if (!*args[cur_arg + 1])
9701 goto missing_arg;
9702
9703 cur_arg++;
9704 cookie = args[cur_arg];
9705 cookie_set = 1;
9706 }
9707 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9708 if (!*args[cur_arg + 1])
9709 goto missing_arg;
9710
9711 cur_arg++;
9712 cookie = args[cur_arg];
9713 cookie_set = 0;
9714 }
9715 else if (strcmp(args[cur_arg], "code") == 0) {
9716 if (!*args[cur_arg + 1])
9717 goto missing_arg;
9718
9719 cur_arg++;
9720 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009721 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009722 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009723 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009724 args[cur_arg - 1], args[cur_arg]);
9725 return NULL;
9726 }
9727 }
9728 else if (!strcmp(args[cur_arg],"drop-query")) {
9729 flags |= REDIRECT_FLAG_DROP_QS;
9730 }
9731 else if (!strcmp(args[cur_arg],"append-slash")) {
9732 flags |= REDIRECT_FLAG_APPEND_SLASH;
9733 }
9734 else if (strcmp(args[cur_arg], "if") == 0 ||
9735 strcmp(args[cur_arg], "unless") == 0) {
9736 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9737 if (!cond) {
9738 memprintf(errmsg, "error in condition: %s", *errmsg);
9739 return NULL;
9740 }
9741 break;
9742 }
9743 else {
9744 memprintf(errmsg,
9745 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9746 args[cur_arg]);
9747 return NULL;
9748 }
9749 cur_arg++;
9750 }
9751
9752 if (type == REDIRECT_TYPE_NONE) {
9753 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9754 return NULL;
9755 }
9756
9757 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9758 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009759 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009760
9761 if (!use_fmt) {
9762 /* old-style static redirect rule */
9763 rule->rdr_str = strdup(destination);
9764 rule->rdr_len = strlen(destination);
9765 }
9766 else {
9767 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009768
9769 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9770 * if prefix == "/", we don't want to add anything, otherwise it
9771 * makes it hard for the user to configure a self-redirection.
9772 */
Godbachd9722032014-12-18 15:44:58 +08009773 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009774 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009775 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009776 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9777 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009778 free(curproxy->conf.lfs_file);
9779 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9780 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009781 }
9782 }
9783
Willy Tarreau4baae242012-12-27 12:00:31 +01009784 if (cookie) {
9785 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9786 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9787 */
9788 rule->cookie_len = strlen(cookie);
9789 if (cookie_set) {
9790 rule->cookie_str = malloc(rule->cookie_len + 10);
9791 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9792 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9793 rule->cookie_len += 9;
9794 } else {
9795 rule->cookie_str = malloc(rule->cookie_len + 21);
9796 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9797 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9798 rule->cookie_len += 20;
9799 }
9800 }
9801 rule->type = type;
9802 rule->code = code;
9803 rule->flags = flags;
9804 LIST_INIT(&rule->list);
9805 return rule;
9806
9807 missing_arg:
9808 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9809 return NULL;
9810}
9811
Willy Tarreau8797c062007-05-07 00:55:35 +02009812/************************************************************************/
9813/* The code below is dedicated to ACL parsing and matching */
9814/************************************************************************/
9815
9816
Willy Tarreau14174bc2012-04-16 14:34:04 +02009817/* This function ensures that the prerequisites for an L7 fetch are ready,
9818 * which means that a request or response is ready. If some data is missing,
9819 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009820 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9821 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009822 *
9823 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009824 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9825 * decide whether or not an HTTP message is present ;
9826 * 0 if the requested data cannot be fetched or if it is certain that
9827 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009828 * 1 if an HTTP message is ready
9829 */
9830static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009831smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009832 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009833{
9834 struct http_txn *txn = l7;
9835 struct http_msg *msg = &txn->req;
9836
9837 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9838 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9839 */
9840
9841 if (unlikely(!s || !txn))
9842 return 0;
9843
9844 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009845 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009846
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009847 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009848 if (unlikely(!s->req))
9849 return 0;
9850
Willy Tarreauaae75e32013-03-29 12:31:49 +01009851 /* If the buffer does not leave enough free space at the end,
9852 * we must first realign it.
9853 */
9854 if (s->req->buf->p > s->req->buf->data &&
9855 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9856 buffer_slow_realign(s->req->buf);
9857
Willy Tarreau14174bc2012-04-16 14:34:04 +02009858 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009859 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009860 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009861
9862 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009863 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009864 http_msg_analyzer(msg, &txn->hdr_idx);
9865
9866 /* Still no valid request ? */
9867 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009868 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009869 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009870 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009871 }
9872 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009873 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009874 return 0;
9875 }
9876
9877 /* OK we just got a valid HTTP request. We have some minor
9878 * preparation to perform so that further checks can rely
9879 * on HTTP tests.
9880 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009881
9882 /* If the request was parsed but was too large, we must absolutely
9883 * return an error so that it is not processed. At the moment this
9884 * cannot happen, but if the parsers are to change in the future,
9885 * we want this check to be maintained.
9886 */
9887 if (unlikely(s->req->buf->i + s->req->buf->p >
9888 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9889 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009890 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009891 return 1;
9892 }
9893
Willy Tarreau9b28e032012-10-12 23:49:43 +02009894 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009895 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9896 s->flags |= SN_REDIRECTABLE;
9897
Willy Tarreau506d0502013-07-06 13:29:24 +02009898 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9899 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009900 }
9901
Willy Tarreau506d0502013-07-06 13:29:24 +02009902 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009903 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009904 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009905
9906 /* otherwise everything's ready for the request */
9907 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009908 else {
9909 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009910 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9911 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009912 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009913 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009914 }
9915
9916 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009917 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009918 return 1;
9919}
Willy Tarreau8797c062007-05-07 00:55:35 +02009920
Willy Tarreau6c616e02014-06-25 16:56:41 +02009921/* Note: these functinos *do* modify the sample. Even in case of success, at
9922 * least the type and uint value are modified.
9923 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009924#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009925 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 +02009926
Willy Tarreau24e32d82012-04-23 23:55:44 +02009927#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009928 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 +02009929
Willy Tarreau8797c062007-05-07 00:55:35 +02009930
9931/* 1. Check on METHOD
9932 * We use the pre-parsed method if it is known, and store its number as an
9933 * integer. If it is unknown, we use the pointer and the length.
9934 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009935static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009936{
9937 int len, meth;
9938
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009939 len = strlen(text);
9940 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009941
9942 pattern->val.i = meth;
9943 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009944 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009945 pattern->len = len;
9946 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009947 else {
9948 pattern->ptr.str = NULL;
9949 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009950 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009951 return 1;
9952}
9953
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009954/* This function fetches the method of current HTTP request and stores
9955 * it in the global pattern struct as a chunk. There are two possibilities :
9956 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9957 * in <len> and <ptr> is NULL ;
9958 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9959 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009960 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009961 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009962static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009963smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01009964 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009965{
9966 int meth;
9967 struct http_txn *txn = l7;
9968
Willy Tarreau24e32d82012-04-23 23:55:44 +02009969 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009970
Willy Tarreau8797c062007-05-07 00:55:35 +02009971 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009972 smp->type = SMP_T_METH;
9973 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009974 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009975 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9976 /* ensure the indexes are not affected */
9977 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009978 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009979 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9980 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009981 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009982 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009983 return 1;
9984}
9985
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009986/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009987static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009988{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009989 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009990 struct pattern_list *lst;
9991 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009992
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009993 list_for_each_entry(lst, &expr->patterns, list) {
9994 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009995
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009996 /* well-known method */
9997 if (pattern->val.i != HTTP_METH_OTHER) {
9998 if (smp->data.meth.meth == pattern->val.i)
9999 return pattern;
10000 else
10001 continue;
10002 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010003
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010004 /* Other method, we must compare the strings */
10005 if (pattern->len != smp->data.meth.str.len)
10006 continue;
10007
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010008 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010009 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10010 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010011 return pattern;
10012 }
10013 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010014}
10015
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010016static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010017smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010018 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010019{
10020 struct http_txn *txn = l7;
10021 char *ptr;
10022 int len;
10023
Willy Tarreauc0239e02012-04-16 14:42:55 +020010024 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010025
Willy Tarreau8797c062007-05-07 00:55:35 +020010026 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010027 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010028
10029 while ((len-- > 0) && (*ptr++ != '/'));
10030 if (len <= 0)
10031 return 0;
10032
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010033 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010034 smp->data.str.str = ptr;
10035 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010036
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010037 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038 return 1;
10039}
10040
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010041static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010042smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010043 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010044{
10045 struct http_txn *txn = l7;
10046 char *ptr;
10047 int len;
10048
Willy Tarreauc0239e02012-04-16 14:42:55 +020010049 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010050
Willy Tarreauf26b2522012-12-14 08:33:14 +010010051 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10052 return 0;
10053
Willy Tarreau8797c062007-05-07 00:55:35 +020010054 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010055 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010056
10057 while ((len-- > 0) && (*ptr++ != '/'));
10058 if (len <= 0)
10059 return 0;
10060
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010061 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010062 smp->data.str.str = ptr;
10063 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010064
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010065 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010066 return 1;
10067}
10068
10069/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010070static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010071smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010072 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010073{
10074 struct http_txn *txn = l7;
10075 char *ptr;
10076 int len;
10077
Willy Tarreauc0239e02012-04-16 14:42:55 +020010078 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010079
Willy Tarreauf26b2522012-12-14 08:33:14 +010010080 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10081 return 0;
10082
Willy Tarreau8797c062007-05-07 00:55:35 +020010083 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010084 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010085
Willy Tarreauf853c462012-04-23 18:53:56 +020010086 smp->type = SMP_T_UINT;
10087 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010088 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010089 return 1;
10090}
10091
10092/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010093static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010094smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010095 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010096{
10097 struct http_txn *txn = l7;
10098
Willy Tarreauc0239e02012-04-16 14:42:55 +020010099 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010100
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010101 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010102 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010103 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010104 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010105 return 1;
10106}
10107
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010108static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010109smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010110 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010111{
10112 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010113 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010114
Willy Tarreauc0239e02012-04-16 14:42:55 +020010115 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010116
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010117 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 +020010118 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010119 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010120
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010121 smp->type = SMP_T_IPV4;
10122 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010123 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010124 return 1;
10125}
10126
10127static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010128smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010129 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010130{
10131 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010132 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010133
Willy Tarreauc0239e02012-04-16 14:42:55 +020010134 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010135
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010136 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 +020010137 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10138 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010139
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010140 smp->type = SMP_T_UINT;
10141 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010142 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010143 return 1;
10144}
10145
Willy Tarreau185b5c42012-04-26 15:11:51 +020010146/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10147 * Accepts an optional argument of type string containing the header field name,
10148 * and an optional argument of type signed or unsigned integer to request an
10149 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010150 * headers are considered from the first one. It does not stop on commas and
10151 * returns full lines instead (useful for User-Agent or Date for example).
10152 */
10153static int
10154smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010155 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010156{
10157 struct http_txn *txn = l7;
10158 struct hdr_idx *idx = &txn->hdr_idx;
10159 struct hdr_ctx *ctx = smp->ctx.a[0];
10160 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10161 int occ = 0;
10162 const char *name_str = NULL;
10163 int name_len = 0;
10164
10165 if (!ctx) {
10166 /* first call */
10167 ctx = &static_hdr_ctx;
10168 ctx->idx = 0;
10169 smp->ctx.a[0] = ctx;
10170 }
10171
10172 if (args) {
10173 if (args[0].type != ARGT_STR)
10174 return 0;
10175 name_str = args[0].data.str.str;
10176 name_len = args[0].data.str.len;
10177
10178 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10179 occ = args[1].data.uint;
10180 }
10181
10182 CHECK_HTTP_MESSAGE_FIRST();
10183
10184 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10185 /* search for header from the beginning */
10186 ctx->idx = 0;
10187
10188 if (!occ && !(opt & SMP_OPT_ITERATE))
10189 /* no explicit occurrence and single fetch => last header by default */
10190 occ = -1;
10191
10192 if (!occ)
10193 /* prepare to report multiple occurrences for ACL fetches */
10194 smp->flags |= SMP_F_NOT_LAST;
10195
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010196 smp->type = SMP_T_STR;
10197 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010198 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10199 return 1;
10200
10201 smp->flags &= ~SMP_F_NOT_LAST;
10202 return 0;
10203}
10204
10205/* 6. Check on HTTP header count. The number of occurrences is returned.
10206 * Accepts exactly 1 argument of type string. It does not stop on commas and
10207 * returns full lines instead (useful for User-Agent or Date for example).
10208 */
10209static int
10210smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010211 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010212{
10213 struct http_txn *txn = l7;
10214 struct hdr_idx *idx = &txn->hdr_idx;
10215 struct hdr_ctx ctx;
10216 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10217 int cnt;
10218
10219 if (!args || args->type != ARGT_STR)
10220 return 0;
10221
10222 CHECK_HTTP_MESSAGE_FIRST();
10223
10224 ctx.idx = 0;
10225 cnt = 0;
10226 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10227 cnt++;
10228
10229 smp->type = SMP_T_UINT;
10230 smp->data.uint = cnt;
10231 smp->flags = SMP_F_VOL_HDR;
10232 return 1;
10233}
10234
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010235static int
10236smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010237 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010238{
10239 struct http_txn *txn = l7;
10240 struct hdr_idx *idx = &txn->hdr_idx;
10241 struct hdr_ctx ctx;
10242 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10243 struct chunk *temp;
10244 char del = ',';
10245
10246 if (args && args->type == ARGT_STR)
10247 del = *args[0].data.str.str;
10248
10249 CHECK_HTTP_MESSAGE_FIRST();
10250
10251 temp = get_trash_chunk();
10252
10253 ctx.idx = 0;
10254 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10255 if (temp->len)
10256 temp->str[temp->len++] = del;
10257 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10258 temp->len += ctx.del;
10259 }
10260
10261 smp->type = SMP_T_STR;
10262 smp->data.str.str = temp->str;
10263 smp->data.str.len = temp->len;
10264 smp->flags = SMP_F_VOL_HDR;
10265 return 1;
10266}
10267
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010268/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10269 * Accepts an optional argument of type string containing the header field name,
10270 * and an optional argument of type signed or unsigned integer to request an
10271 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010272 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010273 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010274static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010275smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010276 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010277{
10278 struct http_txn *txn = l7;
10279 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010280 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010281 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010282 int occ = 0;
10283 const char *name_str = NULL;
10284 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010285
Willy Tarreaua890d072013-04-02 12:01:06 +020010286 if (!ctx) {
10287 /* first call */
10288 ctx = &static_hdr_ctx;
10289 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010290 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010291 }
10292
Willy Tarreau185b5c42012-04-26 15:11:51 +020010293 if (args) {
10294 if (args[0].type != ARGT_STR)
10295 return 0;
10296 name_str = args[0].data.str.str;
10297 name_len = args[0].data.str.len;
10298
10299 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10300 occ = args[1].data.uint;
10301 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010302
Willy Tarreaue333ec92012-04-16 16:26:40 +020010303 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010304
Willy Tarreau185b5c42012-04-26 15:11:51 +020010305 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010306 /* search for header from the beginning */
10307 ctx->idx = 0;
10308
Willy Tarreau185b5c42012-04-26 15:11:51 +020010309 if (!occ && !(opt & SMP_OPT_ITERATE))
10310 /* no explicit occurrence and single fetch => last header by default */
10311 occ = -1;
10312
10313 if (!occ)
10314 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010315 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010316
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010317 smp->type = SMP_T_STR;
10318 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010319 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 +020010320 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010321
Willy Tarreau37406352012-04-23 16:16:37 +020010322 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010323 return 0;
10324}
10325
Willy Tarreauc11416f2007-06-17 16:58:38 +020010326/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010327 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010328 */
10329static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010330smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010331 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010332{
10333 struct http_txn *txn = l7;
10334 struct hdr_idx *idx = &txn->hdr_idx;
10335 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010336 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010337 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010338
Willy Tarreau24e32d82012-04-23 23:55:44 +020010339 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010340 return 0;
10341
Willy Tarreaue333ec92012-04-16 16:26:40 +020010342 CHECK_HTTP_MESSAGE_FIRST();
10343
Willy Tarreau33a7e692007-06-10 19:45:56 +020010344 ctx.idx = 0;
10345 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010346 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 +020010347 cnt++;
10348
Willy Tarreauf853c462012-04-23 18:53:56 +020010349 smp->type = SMP_T_UINT;
10350 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010351 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010352 return 1;
10353}
10354
Willy Tarreau185b5c42012-04-26 15:11:51 +020010355/* Fetch an HTTP header's integer value. The integer value is returned. It
10356 * takes a mandatory argument of type string and an optional one of type int
10357 * to designate a specific occurrence. It returns an unsigned integer, which
10358 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010359 */
10360static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010361smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010362 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010363{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010364 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010365
Willy Tarreauf853c462012-04-23 18:53:56 +020010366 if (ret > 0) {
10367 smp->type = SMP_T_UINT;
10368 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10369 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010370
Willy Tarreaud53e2422012-04-16 17:21:11 +020010371 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010372}
10373
Cyril Bonté69fa9922012-10-25 00:01:06 +020010374/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10375 * and an optional one of type int to designate a specific occurrence.
10376 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010377 */
10378static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010379smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010380 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010381{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010382 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010383
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010384 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010385 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10386 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010387 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010388 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010389 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010390 if (smp->data.str.len < temp->size - 1) {
10391 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10392 temp->str[smp->data.str.len] = '\0';
10393 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10394 smp->type = SMP_T_IPV6;
10395 break;
10396 }
10397 }
10398 }
10399
Willy Tarreaud53e2422012-04-16 17:21:11 +020010400 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010401 if (!(smp->flags & SMP_F_NOT_LAST))
10402 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010403 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010404 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010405}
10406
Willy Tarreau737b0c12007-06-10 21:28:46 +020010407/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10408 * the first '/' after the possible hostname, and ends before the possible '?'.
10409 */
10410static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010411smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010412 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010413{
10414 struct http_txn *txn = l7;
10415 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010416
Willy Tarreauc0239e02012-04-16 14:42:55 +020010417 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010418
Willy Tarreau9b28e032012-10-12 23:49:43 +020010419 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010420 ptr = http_get_path(txn);
10421 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010422 return 0;
10423
10424 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010425 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010426 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010427
10428 while (ptr < end && *ptr != '?')
10429 ptr++;
10430
Willy Tarreauf853c462012-04-23 18:53:56 +020010431 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010432 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010433 return 1;
10434}
10435
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010436/* This produces a concatenation of the first occurrence of the Host header
10437 * followed by the path component if it begins with a slash ('/'). This means
10438 * that '*' will not be added, resulting in exactly the first Host entry.
10439 * If no Host header is found, then the path is returned as-is. The returned
10440 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010441 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010442 */
10443static int
10444smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010445 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010446{
10447 struct http_txn *txn = l7;
10448 char *ptr, *end, *beg;
10449 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010450 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010451
10452 CHECK_HTTP_MESSAGE_FIRST();
10453
10454 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010455 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010456 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010457
10458 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010459 temp = get_trash_chunk();
10460 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010461 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010462 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010463 smp->data.str.len = ctx.vlen;
10464
10465 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010466 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010467 beg = http_get_path(txn);
10468 if (!beg)
10469 beg = end;
10470
10471 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10472
10473 if (beg < ptr && *beg == '/') {
10474 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10475 smp->data.str.len += ptr - beg;
10476 }
10477
10478 smp->flags = SMP_F_VOL_1ST;
10479 return 1;
10480}
10481
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010482/* This produces a 32-bit hash of the concatenation of the first occurrence of
10483 * the Host header followed by the path component if it begins with a slash ('/').
10484 * This means that '*' will not be added, resulting in exactly the first Host
10485 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010486 * is hashed using the path hash followed by a full avalanche hash and provides a
10487 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010488 * high-traffic sites without having to store whole paths.
10489 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010490int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010491smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010492 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010493{
10494 struct http_txn *txn = l7;
10495 struct hdr_ctx ctx;
10496 unsigned int hash = 0;
10497 char *ptr, *beg, *end;
10498 int len;
10499
10500 CHECK_HTTP_MESSAGE_FIRST();
10501
10502 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010503 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010504 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10505 ptr = ctx.line + ctx.val;
10506 len = ctx.vlen;
10507 while (len--)
10508 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10509 }
10510
10511 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010512 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010513 beg = http_get_path(txn);
10514 if (!beg)
10515 beg = end;
10516
10517 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10518
10519 if (beg < ptr && *beg == '/') {
10520 while (beg < ptr)
10521 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10522 }
10523 hash = full_hash(hash);
10524
10525 smp->type = SMP_T_UINT;
10526 smp->data.uint = hash;
10527 smp->flags = SMP_F_VOL_1ST;
10528 return 1;
10529}
10530
Willy Tarreau4a550602012-12-09 14:53:32 +010010531/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010532 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10533 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10534 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010535 * that in environments where IPv6 is insignificant, truncating the output to
10536 * 8 bytes would still work.
10537 */
10538static int
10539smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010540 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010541{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010542 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010543 struct connection *cli_conn = objt_conn(l4->si[0].end);
10544
10545 if (!cli_conn)
10546 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010547
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010548 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010549 return 0;
10550
Willy Tarreau47ca5452012-12-23 20:22:19 +010010551 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010552 *(unsigned int *)temp->str = htonl(smp->data.uint);
10553 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010554
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010555 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010556 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010557 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010558 temp->len += 4;
10559 break;
10560 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010561 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010562 temp->len += 16;
10563 break;
10564 default:
10565 return 0;
10566 }
10567
10568 smp->data.str = *temp;
10569 smp->type = SMP_T_BIN;
10570 return 1;
10571}
10572
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010573/* Extracts the query string, which comes after the question mark '?'. If no
10574 * question mark is found, nothing is returned. Otherwise it returns a sample
10575 * of type string carrying the whole query string.
10576 */
10577static int
10578smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010579 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010580{
10581 struct http_txn *txn = l7;
10582 char *ptr, *end;
10583
10584 CHECK_HTTP_MESSAGE_FIRST();
10585
10586 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10587 end = ptr + txn->req.sl.rq.u_l;
10588
10589 /* look up the '?' */
10590 do {
10591 if (ptr == end)
10592 return 0;
10593 } while (*ptr++ != '?');
10594
10595 smp->type = SMP_T_STR;
10596 smp->data.str.str = ptr;
10597 smp->data.str.len = end - ptr;
10598 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10599 return 1;
10600}
10601
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010602static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010603smp_fetch_proto_http(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 Tarreau2492d5b2009-07-11 00:06:00 +020010605{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010606 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10607 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10608 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010609
Willy Tarreau24e32d82012-04-23 23:55:44 +020010610 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010611
Willy Tarreauf853c462012-04-23 18:53:56 +020010612 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010613 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010614 return 1;
10615}
10616
Willy Tarreau7f18e522010-10-22 20:04:13 +020010617/* return a valid test if the current request is the first one on the connection */
10618static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010619smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010620 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010621{
10622 if (!s)
10623 return 0;
10624
Willy Tarreauf853c462012-04-23 18:53:56 +020010625 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010626 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010627 return 1;
10628}
10629
Willy Tarreau34db1082012-04-19 17:16:54 +020010630/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010631static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010632smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010633 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010634{
10635
Willy Tarreau24e32d82012-04-23 23:55:44 +020010636 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010637 return 0;
10638
Willy Tarreauc0239e02012-04-16 14:42:55 +020010639 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010640
Willy Tarreauc0239e02012-04-16 14:42:55 +020010641 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010642 return 0;
10643
Willy Tarreauf853c462012-04-23 18:53:56 +020010644 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010645 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010646 return 1;
10647}
Willy Tarreau8797c062007-05-07 00:55:35 +020010648
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010649/* Accepts exactly 1 argument of type userlist */
10650static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010651smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010652 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010653{
10654
10655 if (!args || args->type != ARGT_USR)
10656 return 0;
10657
10658 CHECK_HTTP_MESSAGE_FIRST();
10659
10660 if (!get_http_auth(l4))
10661 return 0;
10662
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010663 /* if the user does not belong to the userlist or has a wrong password,
10664 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010665 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010666 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010667 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10668 return 0;
10669
10670 /* pat_match_auth() will need the user list */
10671 smp->ctx.a[0] = args->data.usr;
10672
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010673 smp->type = SMP_T_STR;
10674 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010675 smp->data.str.str = l4->txn.auth.user;
10676 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010677
10678 return 1;
10679}
10680
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010681/* Try to find the next occurrence of a cookie name in a cookie header value.
10682 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10683 * the cookie value is returned into *value and *value_l, and the function
10684 * returns a pointer to the next pointer to search from if the value was found.
10685 * Otherwise if the cookie was not found, NULL is returned and neither value
10686 * nor value_l are touched. The input <hdr> string should first point to the
10687 * header's value, and the <hdr_end> pointer must point to the first character
10688 * not part of the value. <list> must be non-zero if value may represent a list
10689 * of values (cookie headers). This makes it faster to abort parsing when no
10690 * list is expected.
10691 */
10692static char *
10693extract_cookie_value(char *hdr, const char *hdr_end,
10694 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010695 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010696{
10697 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10698 char *next;
10699
10700 /* we search at least a cookie name followed by an equal, and more
10701 * generally something like this :
10702 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10703 */
10704 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10705 /* Iterate through all cookies on this line */
10706
10707 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10708 att_beg++;
10709
10710 /* find att_end : this is the first character after the last non
10711 * space before the equal. It may be equal to hdr_end.
10712 */
10713 equal = att_end = att_beg;
10714
10715 while (equal < hdr_end) {
10716 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10717 break;
10718 if (http_is_spht[(unsigned char)*equal++])
10719 continue;
10720 att_end = equal;
10721 }
10722
10723 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10724 * is between <att_beg> and <equal>, both may be identical.
10725 */
10726
10727 /* look for end of cookie if there is an equal sign */
10728 if (equal < hdr_end && *equal == '=') {
10729 /* look for the beginning of the value */
10730 val_beg = equal + 1;
10731 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10732 val_beg++;
10733
10734 /* find the end of the value, respecting quotes */
10735 next = find_cookie_value_end(val_beg, hdr_end);
10736
10737 /* make val_end point to the first white space or delimitor after the value */
10738 val_end = next;
10739 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10740 val_end--;
10741 } else {
10742 val_beg = val_end = next = equal;
10743 }
10744
10745 /* We have nothing to do with attributes beginning with '$'. However,
10746 * they will automatically be removed if a header before them is removed,
10747 * since they're supposed to be linked together.
10748 */
10749 if (*att_beg == '$')
10750 continue;
10751
10752 /* Ignore cookies with no equal sign */
10753 if (equal == next)
10754 continue;
10755
10756 /* Now we have the cookie name between att_beg and att_end, and
10757 * its value between val_beg and val_end.
10758 */
10759
10760 if (att_end - att_beg == cookie_name_l &&
10761 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10762 /* let's return this value and indicate where to go on from */
10763 *value = val_beg;
10764 *value_l = val_end - val_beg;
10765 return next + 1;
10766 }
10767
10768 /* Set-Cookie headers only have the name in the first attr=value part */
10769 if (!list)
10770 break;
10771 }
10772
10773 return NULL;
10774}
10775
William Lallemanda43ba4e2014-01-28 18:14:25 +010010776/* Fetch a captured HTTP request header. The index is the position of
10777 * the "capture" option in the configuration file
10778 */
10779static int
10780smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010781 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010782{
10783 struct proxy *fe = l4->fe;
10784 struct http_txn *txn = l7;
10785 int idx;
10786
10787 if (!args || args->type != ARGT_UINT)
10788 return 0;
10789
10790 idx = args->data.uint;
10791
10792 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10793 return 0;
10794
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010795 smp->type = SMP_T_STR;
10796 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010797 smp->data.str.str = txn->req.cap[idx];
10798 smp->data.str.len = strlen(txn->req.cap[idx]);
10799
10800 return 1;
10801}
10802
10803/* Fetch a captured HTTP response header. The index is the position of
10804 * the "capture" option in the configuration file
10805 */
10806static int
10807smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010808 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010809{
10810 struct proxy *fe = l4->fe;
10811 struct http_txn *txn = l7;
10812 int idx;
10813
10814 if (!args || args->type != ARGT_UINT)
10815 return 0;
10816
10817 idx = args->data.uint;
10818
10819 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10820 return 0;
10821
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010822 smp->type = SMP_T_STR;
10823 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010824 smp->data.str.str = txn->rsp.cap[idx];
10825 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10826
10827 return 1;
10828}
10829
William Lallemand65ad6e12014-01-31 15:08:02 +010010830/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10831static int
10832smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010833 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010834{
10835 struct chunk *temp;
10836 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010837 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010838
10839 if (!txn->uri)
10840 return 0;
10841
William Lallemand96a77852014-02-05 00:30:02 +010010842 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010843
William Lallemand96a77852014-02-05 00:30:02 +010010844 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10845 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010846
William Lallemand96a77852014-02-05 00:30:02 +010010847 temp = get_trash_chunk();
10848 temp->str = txn->uri;
10849 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010850 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010851 smp->type = SMP_T_STR;
10852 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010853
10854 return 1;
10855
10856}
10857
10858/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10859static int
10860smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010861 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010862{
10863 struct chunk *temp;
10864 struct http_txn *txn = l7;
10865 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010866
10867 if (!txn->uri)
10868 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010869
William Lallemand65ad6e12014-01-31 15:08:02 +010010870 ptr = txn->uri;
10871
10872 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10873 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010874
William Lallemand65ad6e12014-01-31 15:08:02 +010010875 if (!*ptr)
10876 return 0;
10877
10878 ptr++; /* skip the space */
10879
10880 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010881 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010882 if (!ptr)
10883 return 0;
10884 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10885 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010886
10887 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010888 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010889 smp->type = SMP_T_STR;
10890 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010891
10892 return 1;
10893}
10894
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010895/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10896 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10897 */
10898static int
10899smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010900 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010901{
10902 struct http_txn *txn = l7;
10903
10904 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10905 return 0;
10906
10907 if (txn->req.flags & HTTP_MSGF_VER_11)
10908 smp->data.str.str = "HTTP/1.1";
10909 else
10910 smp->data.str.str = "HTTP/1.0";
10911
10912 smp->data.str.len = 8;
10913 smp->type = SMP_T_STR;
10914 smp->flags = SMP_F_CONST;
10915 return 1;
10916
10917}
10918
10919/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10920 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10921 */
10922static int
10923smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010924 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010925{
10926 struct http_txn *txn = l7;
10927
10928 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10929 return 0;
10930
10931 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10932 smp->data.str.str = "HTTP/1.1";
10933 else
10934 smp->data.str.str = "HTTP/1.0";
10935
10936 smp->data.str.len = 8;
10937 smp->type = SMP_T_STR;
10938 smp->flags = SMP_F_CONST;
10939 return 1;
10940
10941}
10942
William Lallemand65ad6e12014-01-31 15:08:02 +010010943
Willy Tarreaue333ec92012-04-16 16:26:40 +020010944/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010945 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010946 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010947 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010948 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010949 * Accepts exactly 1 argument of type string. If the input options indicate
10950 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010951 * The returned sample is of type CSTR. Can be used to parse cookies in other
10952 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010953 */
William Lallemand07c8b242014-05-02 17:11:07 +020010954int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010955 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010956{
10957 struct http_txn *txn = l7;
10958 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010959 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010960 const struct http_msg *msg;
10961 const char *hdr_name;
10962 int hdr_name_len;
10963 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010964 int occ = 0;
10965 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010966
Willy Tarreau24e32d82012-04-23 23:55:44 +020010967 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010968 return 0;
10969
Willy Tarreaua890d072013-04-02 12:01:06 +020010970 if (!ctx) {
10971 /* first call */
10972 ctx = &static_hdr_ctx;
10973 ctx->idx = 0;
10974 smp->ctx.a[2] = ctx;
10975 }
10976
Willy Tarreaue333ec92012-04-16 16:26:40 +020010977 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010978
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010979 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010980 msg = &txn->req;
10981 hdr_name = "Cookie";
10982 hdr_name_len = 6;
10983 } else {
10984 msg = &txn->rsp;
10985 hdr_name = "Set-Cookie";
10986 hdr_name_len = 10;
10987 }
10988
Willy Tarreau28376d62012-04-26 21:26:10 +020010989 if (!occ && !(opt & SMP_OPT_ITERATE))
10990 /* no explicit occurrence and single fetch => last cookie by default */
10991 occ = -1;
10992
10993 /* OK so basically here, either we want only one value and it's the
10994 * last one, or we want to iterate over all of them and we fetch the
10995 * next one.
10996 */
10997
Willy Tarreau9b28e032012-10-12 23:49:43 +020010998 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010999 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011000 /* search for the header from the beginning, we must first initialize
11001 * the search parameters.
11002 */
Willy Tarreau37406352012-04-23 16:16:37 +020011003 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011004 ctx->idx = 0;
11005 }
11006
Willy Tarreau28376d62012-04-26 21:26:10 +020011007 smp->flags |= SMP_F_VOL_HDR;
11008
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011009 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011010 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11011 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011012 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11013 goto out;
11014
Willy Tarreau24e32d82012-04-23 23:55:44 +020011015 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011016 continue;
11017
Willy Tarreau37406352012-04-23 16:16:37 +020011018 smp->ctx.a[0] = ctx->line + ctx->val;
11019 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011020 }
11021
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011022 smp->type = SMP_T_STR;
11023 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011024 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011025 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011026 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011027 &smp->data.str.str,
11028 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011029 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011030 found = 1;
11031 if (occ >= 0) {
11032 /* one value was returned into smp->data.str.{str,len} */
11033 smp->flags |= SMP_F_NOT_LAST;
11034 return 1;
11035 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011036 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011037 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011038 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011039 /* all cookie headers and values were scanned. If we're looking for the
11040 * last occurrence, we may return it now.
11041 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011042 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011043 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011044 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011045}
11046
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011047/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011048 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011049 * multiple cookies may be parsed on the same line. The returned sample is of
11050 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011051 */
11052static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010011053smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011054 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011055{
11056 struct http_txn *txn = l7;
11057 struct hdr_idx *idx = &txn->hdr_idx;
11058 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011059 const struct http_msg *msg;
11060 const char *hdr_name;
11061 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011062 int cnt;
11063 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011064 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011065
Willy Tarreau24e32d82012-04-23 23:55:44 +020011066 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011067 return 0;
11068
Willy Tarreaue333ec92012-04-16 16:26:40 +020011069 CHECK_HTTP_MESSAGE_FIRST();
11070
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011071 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011072 msg = &txn->req;
11073 hdr_name = "Cookie";
11074 hdr_name_len = 6;
11075 } else {
11076 msg = &txn->rsp;
11077 hdr_name = "Set-Cookie";
11078 hdr_name_len = 10;
11079 }
11080
Willy Tarreau9b28e032012-10-12 23:49:43 +020011081 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011082 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011083 ctx.idx = 0;
11084 cnt = 0;
11085
11086 while (1) {
11087 /* Note: val_beg == NULL every time we need to fetch a new header */
11088 if (!val_beg) {
11089 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11090 break;
11091
Willy Tarreau24e32d82012-04-23 23:55:44 +020011092 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011093 continue;
11094
11095 val_beg = ctx.line + ctx.val;
11096 val_end = val_beg + ctx.vlen;
11097 }
11098
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011099 smp->type = SMP_T_STR;
11100 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011101 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011102 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011103 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011104 &smp->data.str.str,
11105 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011106 cnt++;
11107 }
11108 }
11109
Willy Tarreaub169eba2013-12-16 15:14:43 +010011110 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011111 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011112 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011113 return 1;
11114}
11115
Willy Tarreau51539362012-05-08 12:46:28 +020011116/* Fetch an cookie's integer value. The integer value is returned. It
11117 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11118 */
11119static int
11120smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011121 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011122{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011123 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011124
11125 if (ret > 0) {
11126 smp->type = SMP_T_UINT;
11127 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11128 }
11129
11130 return ret;
11131}
11132
Willy Tarreau8797c062007-05-07 00:55:35 +020011133/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011134/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011135/************************************************************************/
11136
David Cournapeau16023ee2010-12-23 20:55:41 +090011137/*
11138 * Given a path string and its length, find the position of beginning of the
11139 * query string. Returns NULL if no query string is found in the path.
11140 *
11141 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11142 *
11143 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11144 */
bedis4c75cca2012-10-05 08:38:24 +020011145static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011146{
11147 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011148
bedis4c75cca2012-10-05 08:38:24 +020011149 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011150 return p ? p + 1 : NULL;
11151}
11152
bedis4c75cca2012-10-05 08:38:24 +020011153static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011154{
bedis4c75cca2012-10-05 08:38:24 +020011155 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011156}
11157
11158/*
11159 * Given a url parameter, find the starting position of the first occurence,
11160 * or NULL if the parameter is not found.
11161 *
11162 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11163 * the function will return query_string+8.
11164 */
11165static char*
11166find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011167 char* url_param_name, size_t url_param_name_l,
11168 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011169{
11170 char *pos, *last;
11171
11172 pos = query_string;
11173 last = query_string + query_string_l - url_param_name_l - 1;
11174
11175 while (pos <= last) {
11176 if (pos[url_param_name_l] == '=') {
11177 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11178 return pos;
11179 pos += url_param_name_l + 1;
11180 }
bedis4c75cca2012-10-05 08:38:24 +020011181 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011182 pos++;
11183 pos++;
11184 }
11185 return NULL;
11186}
11187
11188/*
11189 * Given a url parameter name, returns its value and size into *value and
11190 * *value_l respectively, and returns non-zero. If the parameter is not found,
11191 * zero is returned and value/value_l are not touched.
11192 */
11193static int
11194find_url_param_value(char* path, size_t path_l,
11195 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011196 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011197{
11198 char *query_string, *qs_end;
11199 char *arg_start;
11200 char *value_start, *value_end;
11201
bedis4c75cca2012-10-05 08:38:24 +020011202 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011203 if (!query_string)
11204 return 0;
11205
11206 qs_end = path + path_l;
11207 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011208 url_param_name, url_param_name_l,
11209 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011210 if (!arg_start)
11211 return 0;
11212
11213 value_start = arg_start + url_param_name_l + 1;
11214 value_end = value_start;
11215
bedis4c75cca2012-10-05 08:38:24 +020011216 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011217 value_end++;
11218
11219 *value = value_start;
11220 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011221 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011222}
11223
11224static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011225smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011226 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011227{
bedis4c75cca2012-10-05 08:38:24 +020011228 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011229 struct http_txn *txn = l7;
11230 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011231
bedis4c75cca2012-10-05 08:38:24 +020011232 if (!args || args[0].type != ARGT_STR ||
11233 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011234 return 0;
11235
11236 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011237
bedis4c75cca2012-10-05 08:38:24 +020011238 if (args[1].type)
11239 delim = *args[1].data.str.str;
11240
Willy Tarreau9b28e032012-10-12 23:49:43 +020011241 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011242 args->data.str.str, args->data.str.len,
11243 &smp->data.str.str, &smp->data.str.len,
11244 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011245 return 0;
11246
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011247 smp->type = SMP_T_STR;
11248 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011249 return 1;
11250}
11251
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011252/* Return the signed integer value for the specified url parameter (see url_param
11253 * above).
11254 */
11255static int
11256smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011257 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011258{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011259 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011260
11261 if (ret > 0) {
11262 smp->type = SMP_T_UINT;
11263 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11264 }
11265
11266 return ret;
11267}
11268
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011269/* This produces a 32-bit hash of the concatenation of the first occurrence of
11270 * the Host header followed by the path component if it begins with a slash ('/').
11271 * This means that '*' will not be added, resulting in exactly the first Host
11272 * entry. If no Host header is found, then the path is used. The resulting value
11273 * is hashed using the url hash followed by a full avalanche hash and provides a
11274 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11275 * high-traffic sites without having to store whole paths.
11276 * this differs from the base32 functions in that it includes the url parameters
11277 * as well as the path
11278 */
11279static int
11280smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011281 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011282{
11283 struct http_txn *txn = l7;
11284 struct hdr_ctx ctx;
11285 unsigned int hash = 0;
11286 char *ptr, *beg, *end;
11287 int len;
11288
11289 CHECK_HTTP_MESSAGE_FIRST();
11290
11291 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011292 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011293 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11294 ptr = ctx.line + ctx.val;
11295 len = ctx.vlen;
11296 while (len--)
11297 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11298 }
11299
11300 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011301 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 +000011302 beg = http_get_path(txn);
11303 if (!beg)
11304 beg = end;
11305
11306 for (ptr = beg; ptr < end ; ptr++);
11307
11308 if (beg < ptr && *beg == '/') {
11309 while (beg < ptr)
11310 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11311 }
11312 hash = full_hash(hash);
11313
11314 smp->type = SMP_T_UINT;
11315 smp->data.uint = hash;
11316 smp->flags = SMP_F_VOL_1ST;
11317 return 1;
11318}
11319
11320/* This concatenates the source address with the 32-bit hash of the Host and
11321 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11322 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11323 * on the source address length. The URL hash is stored before the address so
11324 * that in environments where IPv6 is insignificant, truncating the output to
11325 * 8 bytes would still work.
11326 */
11327static int
11328smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011329 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011330{
11331 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011332 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011333
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011334 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011335 return 0;
11336
11337 temp = get_trash_chunk();
11338 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11339 temp->len += sizeof(smp->data.uint);
11340
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011341 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011342 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011343 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011344 temp->len += 4;
11345 break;
11346 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011347 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011348 temp->len += 16;
11349 break;
11350 default:
11351 return 0;
11352 }
11353
11354 smp->data.str = *temp;
11355 smp->type = SMP_T_BIN;
11356 return 1;
11357}
11358
Willy Tarreau185b5c42012-04-26 15:11:51 +020011359/* This function is used to validate the arguments passed to any "hdr" fetch
11360 * keyword. These keywords support an optional positive or negative occurrence
11361 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11362 * is assumed that the types are already the correct ones. Returns 0 on error,
11363 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11364 * error message in case of error, that the caller is responsible for freeing.
11365 * The initial location must either be freeable or NULL.
11366 */
11367static int val_hdr(struct arg *arg, char **err_msg)
11368{
11369 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011370 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011371 return 0;
11372 }
11373 return 1;
11374}
11375
Willy Tarreau276fae92013-07-25 14:36:01 +020011376/* takes an UINT value on input supposed to represent the time since EPOCH,
11377 * adds an optional offset found in args[0] and emits a string representing
11378 * the date in RFC-1123/5322 format.
11379 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011380static int sample_conv_http_date(struct session *session, const struct arg *args,
11381 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011382{
11383 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11384 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11385 struct chunk *temp;
11386 struct tm *tm;
11387 time_t curr_date = smp->data.uint;
11388
11389 /* add offset */
11390 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11391 curr_date += args[0].data.sint;
11392
11393 tm = gmtime(&curr_date);
11394
11395 temp = get_trash_chunk();
11396 temp->len = snprintf(temp->str, temp->size - temp->len,
11397 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11398 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11399 tm->tm_hour, tm->tm_min, tm->tm_sec);
11400
11401 smp->data.str = *temp;
11402 smp->type = SMP_T_STR;
11403 return 1;
11404}
11405
Thierry FOURNIERad903512014-04-11 17:51:01 +020011406/* Match language range with language tag. RFC2616 14.4:
11407 *
11408 * A language-range matches a language-tag if it exactly equals
11409 * the tag, or if it exactly equals a prefix of the tag such
11410 * that the first tag character following the prefix is "-".
11411 *
11412 * Return 1 if the strings match, else return 0.
11413 */
11414static inline int language_range_match(const char *range, int range_len,
11415 const char *tag, int tag_len)
11416{
11417 const char *end = range + range_len;
11418 const char *tend = tag + tag_len;
11419 while (range < end) {
11420 if (*range == '-' && tag == tend)
11421 return 1;
11422 if (*range != *tag || tag == tend)
11423 return 0;
11424 range++;
11425 tag++;
11426 }
11427 /* Return true only if the last char of the tag is matched. */
11428 return tag == tend;
11429}
11430
11431/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011432static int sample_conv_q_prefered(struct session *session, const struct arg *args,
11433 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011434{
11435 const char *al = smp->data.str.str;
11436 const char *end = al + smp->data.str.len;
11437 const char *token;
11438 int toklen;
11439 int qvalue;
11440 const char *str;
11441 const char *w;
11442 int best_q = 0;
11443
11444 /* Set the constant to the sample, because the output of the
11445 * function will be peek in the constant configuration string.
11446 */
11447 smp->flags |= SMP_F_CONST;
11448 smp->data.str.size = 0;
11449 smp->data.str.str = "";
11450 smp->data.str.len = 0;
11451
11452 /* Parse the accept language */
11453 while (1) {
11454
11455 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011456 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011457 al++;
11458 if (al >= end)
11459 break;
11460
11461 /* Start of the fisrt word. */
11462 token = al;
11463
11464 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011465 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011466 al++;
11467 if (al == token)
11468 goto expect_comma;
11469
11470 /* Length of the token. */
11471 toklen = al - token;
11472 qvalue = 1000;
11473
11474 /* Check if the token exists in the list. If the token not exists,
11475 * jump to the next token.
11476 */
11477 str = args[0].data.str.str;
11478 w = str;
11479 while (1) {
11480 if (*str == ';' || *str == '\0') {
11481 if (language_range_match(token, toklen, w, str-w))
11482 goto look_for_q;
11483 if (*str == '\0')
11484 goto expect_comma;
11485 w = str + 1;
11486 }
11487 str++;
11488 }
11489 goto expect_comma;
11490
11491look_for_q:
11492
11493 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011494 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011495 al++;
11496 if (al >= end)
11497 goto process_value;
11498
11499 /* If ',' is found, process the result */
11500 if (*al == ',')
11501 goto process_value;
11502
11503 /* If the character is different from ';', look
11504 * for the end of the header part in best effort.
11505 */
11506 if (*al != ';')
11507 goto expect_comma;
11508
11509 /* Assumes that the char is ';', now expect "q=". */
11510 al++;
11511
11512 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011513 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011514 al++;
11515 if (al >= end)
11516 goto process_value;
11517
11518 /* Expect 'q'. If no 'q', continue in best effort */
11519 if (*al != 'q')
11520 goto process_value;
11521 al++;
11522
11523 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011524 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011525 al++;
11526 if (al >= end)
11527 goto process_value;
11528
11529 /* Expect '='. If no '=', continue in best effort */
11530 if (*al != '=')
11531 goto process_value;
11532 al++;
11533
11534 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011535 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011536 al++;
11537 if (al >= end)
11538 goto process_value;
11539
11540 /* Parse the q value. */
11541 qvalue = parse_qvalue(al, &al);
11542
11543process_value:
11544
11545 /* If the new q value is the best q value, then store the associated
11546 * language in the response. If qvalue is the biggest value (1000),
11547 * break the process.
11548 */
11549 if (qvalue > best_q) {
11550 smp->data.str.str = (char *)w;
11551 smp->data.str.len = str - w;
11552 if (qvalue >= 1000)
11553 break;
11554 best_q = qvalue;
11555 }
11556
11557expect_comma:
11558
11559 /* Expect comma or end. If the end is detected, quit the loop. */
11560 while (al < end && *al != ',')
11561 al++;
11562 if (al >= end)
11563 break;
11564
11565 /* Comma is found, jump it and restart the analyzer. */
11566 al++;
11567 }
11568
11569 /* Set default value if required. */
11570 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11571 smp->data.str.str = args[1].data.str.str;
11572 smp->data.str.len = args[1].data.str.len;
11573 }
11574
11575 /* Return true only if a matching language was found. */
11576 return smp->data.str.len != 0;
11577}
11578
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011579/* This function executes one of the set-{method,path,query,uri} actions. It
11580 * builds a string in the trash from the specified format string, then modifies
11581 * the relevant part of the request line accordingly. Then it updates various
11582 * pointers to the next elements which were moved, and the total buffer length.
11583 * It finds the action to be performed in p[2], previously filled by function
11584 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11585 * error, though this can be revisited when this code is finally exploited.
11586 */
11587int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn)
11588{
11589 char *cur_ptr, *cur_end;
11590 int offset;
11591 int delta;
11592
11593 chunk_reset(&trash);
11594
11595 /* prepare a '?' just in case we have to create a query string */
11596 trash.str[trash.len++] = '?';
11597 offset = 1;
11598 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11599
11600 switch (*(int *)&rule->arg.act.p[2]) {
11601 case 0: // method
11602 cur_ptr = s->req->buf->p;
11603 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11604
11605 /* adjust req line offsets and lengths */
11606 delta = trash.len - offset - (cur_end - cur_ptr);
11607 txn->req.sl.rq.m_l += delta;
11608 txn->req.sl.rq.u += delta;
11609 txn->req.sl.rq.v += delta;
11610 break;
11611
11612 case 1: // path
11613 cur_ptr = http_get_path(txn);
11614 if (!cur_ptr)
11615 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11616
11617 cur_end = cur_ptr;
11618 while (cur_end < s->req->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
11619 cur_end++;
11620
11621 /* adjust req line offsets and lengths */
11622 delta = trash.len - offset - (cur_end - cur_ptr);
11623 txn->req.sl.rq.u_l += delta;
11624 txn->req.sl.rq.v += delta;
11625 break;
11626
11627 case 2: // query
11628 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11629 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11630 while (cur_ptr < cur_end && *cur_ptr != '?')
11631 cur_ptr++;
11632
11633 /* skip the question mark or indicate that we must insert it
11634 * (but only if the format string is not empty then).
11635 */
11636 if (cur_ptr < cur_end)
11637 cur_ptr++;
11638 else if (trash.len > 1)
11639 offset = 0;
11640
11641 /* adjust req line offsets and lengths */
11642 delta = trash.len - offset - (cur_end - cur_ptr);
11643 txn->req.sl.rq.u_l += delta;
11644 txn->req.sl.rq.v += delta;
11645 break;
11646
11647 case 3: // uri
11648 cur_ptr = s->req->buf->p + txn->req.sl.rq.u;
11649 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11650
11651 /* adjust req line offsets and lengths */
11652 delta = trash.len - offset - (cur_end - cur_ptr);
11653 txn->req.sl.rq.u_l += delta;
11654 txn->req.sl.rq.v += delta;
11655 break;
11656
11657 default:
11658 return -1;
11659 }
11660
11661 /* commit changes and adjust end of message */
11662 delta = buffer_replace2(s->req->buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset);
11663 http_msg_move_end(&txn->req, delta);
11664 return 0;
11665}
11666
11667/* parse an http-request action among :
11668 * set-method
11669 * set-path
11670 * set-query
11671 * set-uri
11672 *
11673 * All of them accept a single argument of type string representing a log-format.
11674 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11675 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11676 * It returns 0 on success, < 0 on error.
11677 */
11678int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11679{
11680 int cur_arg = *orig_arg;
11681
11682 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11683
11684 switch (args[0][4]) {
11685 case 'm' :
11686 *(int *)&rule->arg.act.p[2] = 0;
11687 rule->action_ptr = http_action_set_req_line;
11688 break;
11689 case 'p' :
11690 *(int *)&rule->arg.act.p[2] = 1;
11691 rule->action_ptr = http_action_set_req_line;
11692 break;
11693 case 'q' :
11694 *(int *)&rule->arg.act.p[2] = 2;
11695 rule->action_ptr = http_action_set_req_line;
11696 break;
11697 case 'u' :
11698 *(int *)&rule->arg.act.p[2] = 3;
11699 rule->action_ptr = http_action_set_req_line;
11700 break;
11701 default:
11702 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11703 return -1;
11704 }
11705
11706 if (!*args[cur_arg] ||
11707 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11708 memprintf(err, "expects exactly 1 argument <format>");
11709 return -1;
11710 }
11711
11712 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11713 proxy->conf.args.ctx = ARGC_HRQ;
11714 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11715 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11716 proxy->conf.args.file, proxy->conf.args.line);
11717
11718 (*orig_arg)++;
11719 return 0;
11720}
11721
William Lallemand73025dd2014-04-24 14:38:37 +020011722/*
11723 * Return the struct http_req_action_kw associated to a keyword.
11724 */
11725struct http_req_action_kw *action_http_req_custom(const char *kw)
11726{
11727 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11728 struct http_req_action_kw_list *kw_list;
11729 int i;
11730
11731 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11732 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11733 if (!strcmp(kw, kw_list->kw[i].kw))
11734 return &kw_list->kw[i];
11735 }
11736 }
11737 }
11738 return NULL;
11739}
11740
11741/*
11742 * Return the struct http_res_action_kw associated to a keyword.
11743 */
11744struct http_res_action_kw *action_http_res_custom(const char *kw)
11745{
11746 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11747 struct http_res_action_kw_list *kw_list;
11748 int i;
11749
11750 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11751 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11752 if (!strcmp(kw, kw_list->kw[i].kw))
11753 return &kw_list->kw[i];
11754 }
11755 }
11756 }
11757 return NULL;
11758}
11759
Willy Tarreau4a568972010-05-12 08:08:50 +020011760/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011761/* All supported ACL keywords must be declared here. */
11762/************************************************************************/
11763
11764/* Note: must not be declared <const> as its list will be overwritten.
11765 * Please take care of keeping this list alphabetically sorted.
11766 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011767static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011768 { "base", "base", PAT_MATCH_STR },
11769 { "base_beg", "base", PAT_MATCH_BEG },
11770 { "base_dir", "base", PAT_MATCH_DIR },
11771 { "base_dom", "base", PAT_MATCH_DOM },
11772 { "base_end", "base", PAT_MATCH_END },
11773 { "base_len", "base", PAT_MATCH_LEN },
11774 { "base_reg", "base", PAT_MATCH_REG },
11775 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011776
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011777 { "cook", "req.cook", PAT_MATCH_STR },
11778 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11779 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11780 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11781 { "cook_end", "req.cook", PAT_MATCH_END },
11782 { "cook_len", "req.cook", PAT_MATCH_LEN },
11783 { "cook_reg", "req.cook", PAT_MATCH_REG },
11784 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011785
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011786 { "hdr", "req.hdr", PAT_MATCH_STR },
11787 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11788 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11789 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11790 { "hdr_end", "req.hdr", PAT_MATCH_END },
11791 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11792 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11793 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011794
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011795 /* these two declarations uses strings with list storage (in place
11796 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11797 * and delete functions are relative to the list management. The parse
11798 * and match method are related to the corresponding fetch methods. This
11799 * is very particular ACL declaration mode.
11800 */
11801 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11802 { "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 +020011803
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011804 { "path", "path", PAT_MATCH_STR },
11805 { "path_beg", "path", PAT_MATCH_BEG },
11806 { "path_dir", "path", PAT_MATCH_DIR },
11807 { "path_dom", "path", PAT_MATCH_DOM },
11808 { "path_end", "path", PAT_MATCH_END },
11809 { "path_len", "path", PAT_MATCH_LEN },
11810 { "path_reg", "path", PAT_MATCH_REG },
11811 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011812
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011813 { "req_ver", "req.ver", PAT_MATCH_STR },
11814 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011815
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011816 { "scook", "res.cook", PAT_MATCH_STR },
11817 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11818 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11819 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11820 { "scook_end", "res.cook", PAT_MATCH_END },
11821 { "scook_len", "res.cook", PAT_MATCH_LEN },
11822 { "scook_reg", "res.cook", PAT_MATCH_REG },
11823 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011824
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011825 { "shdr", "res.hdr", PAT_MATCH_STR },
11826 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11827 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11828 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11829 { "shdr_end", "res.hdr", PAT_MATCH_END },
11830 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11831 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11832 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011833
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011834 { "url", "url", PAT_MATCH_STR },
11835 { "url_beg", "url", PAT_MATCH_BEG },
11836 { "url_dir", "url", PAT_MATCH_DIR },
11837 { "url_dom", "url", PAT_MATCH_DOM },
11838 { "url_end", "url", PAT_MATCH_END },
11839 { "url_len", "url", PAT_MATCH_LEN },
11840 { "url_reg", "url", PAT_MATCH_REG },
11841 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011842
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011843 { "urlp", "urlp", PAT_MATCH_STR },
11844 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11845 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11846 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11847 { "urlp_end", "urlp", PAT_MATCH_END },
11848 { "urlp_len", "urlp", PAT_MATCH_LEN },
11849 { "urlp_reg", "urlp", PAT_MATCH_REG },
11850 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011851
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011852 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011853}};
11854
11855/************************************************************************/
11856/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011857/************************************************************************/
11858/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011859static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011860 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011861 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11862 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11863
William Lallemanda43ba4e2014-01-28 18:14:25 +010011864 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011865 { "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 +020011866
11867 /* retrieve these captures from the HTTP logs */
11868 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11869 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11870 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11871
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011872 { "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 +020011873 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011874
Willy Tarreau409bcde2013-01-08 00:31:00 +010011875 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11876 * are only here to match the ACL's name, are request-only and are used
11877 * for ACL compatibility only.
11878 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011879 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11880 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011881 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11882 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11883
11884 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11885 * only here to match the ACL's name, are request-only and are used for
11886 * ACL compatibility only.
11887 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011888 { "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 +010011889 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11890 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11891 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11892
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011893 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011894 { "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 +010011895 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011896 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011897 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011898 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011899
11900 /* HTTP protocol on the request path */
11901 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011902 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011903
11904 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011905 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11906 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011907
11908 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011909 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11910 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011911
Willy Tarreau18ed2562013-01-14 15:56:36 +010011912 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011913 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011914 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11915 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11916
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011917 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011918 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011919 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011920 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11921 { "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 +010011922 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011923 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11924
11925 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011926 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011927 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11928 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11929
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011930 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011931 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011932 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011933 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11934 { "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 +010011935 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011936 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11937
Willy Tarreau409bcde2013-01-08 00:31:00 +010011938 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011939 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011940 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11941 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011942 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011943
11944 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011945 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011946 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11947 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11948 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11949
11950 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011951 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011952 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11953 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011954 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11955 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011956 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11957 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011958 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11959 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011960}};
11961
Willy Tarreau8797c062007-05-07 00:55:35 +020011962
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011963/************************************************************************/
11964/* All supported converter keywords must be declared here. */
11965/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020011966/* Note: must not be declared <const> as its list will be overwritten */
11967static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011968 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11969 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011970 { NULL, NULL, 0, 0, 0 },
11971}};
11972
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011973/************************************************************************/
11974/* All supported http-request action keywords must be declared here. */
11975/************************************************************************/
11976struct http_req_action_kw_list http_req_actions = {
11977 .scope = "http",
11978 .kw = {
11979 { "set-method", parse_set_req_line },
11980 { "set-path", parse_set_req_line },
11981 { "set-query", parse_set_req_line },
11982 { "set-uri", parse_set_req_line },
11983 }
11984};
11985
Willy Tarreau8797c062007-05-07 00:55:35 +020011986__attribute__((constructor))
11987static void __http_protocol_init(void)
11988{
11989 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011990 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011991 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011992 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020011993}
11994
11995
Willy Tarreau58f10d72006-12-04 02:26:12 +010011996/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011997 * Local variables:
11998 * c-indent-level: 8
11999 * c-basic-offset: 8
12000 * End:
12001 */