blob: 33893edf5a3905ae48508710d323cda298e6a5ae [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
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<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 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200276/* This function returns a reason associated with the HTTP status.
277 * This function never fails, a message is always returned.
278 */
279const char *get_reason(unsigned int status)
280{
281 switch (status) {
282 case 100: return "Continue";
283 case 101: return "Switching Protocols";
284 case 102: return "Processing";
285 case 200: return "OK";
286 case 201: return "Created";
287 case 202: return "Accepted";
288 case 203: return "Non-Authoritative Information";
289 case 204: return "No Content";
290 case 205: return "Reset Content";
291 case 206: return "Partial Content";
292 case 207: return "Multi-Status";
293 case 210: return "Content Different";
294 case 226: return "IM Used";
295 case 300: return "Multiple Choices";
296 case 301: return "Moved Permanently";
297 case 302: return "Moved Temporarily";
298 case 303: return "See Other";
299 case 304: return "Not Modified";
300 case 305: return "Use Proxy";
301 case 307: return "Temporary Redirect";
302 case 308: return "Permanent Redirect";
303 case 310: return "Too many Redirects";
304 case 400: return "Bad Request";
305 case 401: return "Unauthorized";
306 case 402: return "Payment Required";
307 case 403: return "Forbidden";
308 case 404: return "Not Found";
309 case 405: return "Method Not Allowed";
310 case 406: return "Not Acceptable";
311 case 407: return "Proxy Authentication Required";
312 case 408: return "Request Time-out";
313 case 409: return "Conflict";
314 case 410: return "Gone";
315 case 411: return "Length Required";
316 case 412: return "Precondition Failed";
317 case 413: return "Request Entity Too Large";
318 case 414: return "Request-URI Too Long";
319 case 415: return "Unsupported Media Type";
320 case 416: return "Requested range unsatisfiable";
321 case 417: return "Expectation failed";
322 case 418: return "I'm a teapot";
323 case 422: return "Unprocessable entity";
324 case 423: return "Locked";
325 case 424: return "Method failure";
326 case 425: return "Unordered Collection";
327 case 426: return "Upgrade Required";
328 case 428: return "Precondition Required";
329 case 429: return "Too Many Requests";
330 case 431: return "Request Header Fields Too Large";
331 case 449: return "Retry With";
332 case 450: return "Blocked by Windows Parental Controls";
333 case 451: return "Unavailable For Legal Reasons";
334 case 456: return "Unrecoverable Error";
335 case 499: return "client has closed connection";
336 case 500: return "Internal Server Error";
337 case 501: return "Not Implemented";
338 case 502: return "Bad Gateway ou Proxy Error";
339 case 503: return "Service Unavailable";
340 case 504: return "Gateway Time-out";
341 case 505: return "HTTP Version not supported";
342 case 506: return "Variant also negociate";
343 case 507: return "Insufficient storage";
344 case 508: return "Loop detected";
345 case 509: return "Bandwidth Limit Exceeded";
346 case 510: return "Not extended";
347 case 511: return "Network authentication required";
348 case 520: return "Web server is returning an unknown error";
349 default:
350 switch (status) {
351 case 100 ... 199: return "Informational";
352 case 200 ... 299: return "Success";
353 case 300 ... 399: return "Redirection";
354 case 400 ... 499: return "Client Error";
355 case 500 ... 599: return "Server Error";
356 default: return "Other";
357 }
358 }
359}
360
Willy Tarreau80587432006-12-24 17:47:20 +0100361void init_proto_http()
362{
Willy Tarreau42250582007-04-01 01:30:43 +0200363 int i;
364 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100365 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200366
Willy Tarreau80587432006-12-24 17:47:20 +0100367 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
368 if (!http_err_msgs[msg]) {
369 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
370 abort();
371 }
372
373 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
374 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
375 }
Willy Tarreau42250582007-04-01 01:30:43 +0200376
377 /* initialize the log header encoding map : '{|}"#' should be encoded with
378 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
379 * URL encoding only requires '"', '#' to be encoded as well as non-
380 * printable characters above.
381 */
382 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
383 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100384 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200385 for (i = 0; i < 32; i++) {
386 FD_SET(i, hdr_encode_map);
387 FD_SET(i, url_encode_map);
388 }
389 for (i = 127; i < 256; i++) {
390 FD_SET(i, hdr_encode_map);
391 FD_SET(i, url_encode_map);
392 }
393
394 tmp = "\"#{|}";
395 while (*tmp) {
396 FD_SET(*tmp, hdr_encode_map);
397 tmp++;
398 }
399
400 tmp = "\"#";
401 while (*tmp) {
402 FD_SET(*tmp, url_encode_map);
403 tmp++;
404 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200405
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100406 /* initialize the http header encoding map. The draft httpbis define the
407 * header content as:
408 *
409 * HTTP-message = start-line
410 * *( header-field CRLF )
411 * CRLF
412 * [ message-body ]
413 * header-field = field-name ":" OWS field-value OWS
414 * field-value = *( field-content / obs-fold )
415 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
416 * obs-fold = CRLF 1*( SP / HTAB )
417 * field-vchar = VCHAR / obs-text
418 * VCHAR = %x21-7E
419 * obs-text = %x80-FF
420 *
421 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
422 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
423 * "obs-fold" is volontary forgotten because haproxy remove this.
424 */
425 memset(http_encode_map, 0, sizeof(http_encode_map));
426 for (i = 0x00; i <= 0x08; i++)
427 FD_SET(i, http_encode_map);
428 for (i = 0x0a; i <= 0x1f; i++)
429 FD_SET(i, http_encode_map);
430 FD_SET(0x7f, http_encode_map);
431
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200432 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200433 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100435 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
Willy Tarreau53b6c742006-12-17 13:37:46 +0100438/*
439 * We have 26 list of methods (1 per first letter), each of which can have
440 * up to 3 entries (2 valid, 1 null).
441 */
442struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100443 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100444 int len;
445 const char text[8];
446};
447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100448const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 ['C' - 'A'] = {
450 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
451 },
452 ['D' - 'A'] = {
453 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
454 },
455 ['G' - 'A'] = {
456 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
457 },
458 ['H' - 'A'] = {
459 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
460 },
461 ['P' - 'A'] = {
462 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
463 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
464 },
465 ['T' - 'A'] = {
466 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
467 },
468 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200469 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100470 */
471};
472
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100473const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100474 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
475 [HTTP_METH_GET] = { "GET", 3 },
476 [HTTP_METH_HEAD] = { "HEAD", 4 },
477 [HTTP_METH_POST] = { "POST", 4 },
478 [HTTP_METH_PUT] = { "PUT", 3 },
479 [HTTP_METH_DELETE] = { "DELETE", 6 },
480 [HTTP_METH_TRACE] = { "TRACE", 5 },
481 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
482};
483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100484/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200485 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100486 * RFC2616 for those chars.
487 */
488
489const char http_is_spht[256] = {
490 [' '] = 1, ['\t'] = 1,
491};
492
493const char http_is_crlf[256] = {
494 ['\r'] = 1, ['\n'] = 1,
495};
496
497const char http_is_lws[256] = {
498 [' '] = 1, ['\t'] = 1,
499 ['\r'] = 1, ['\n'] = 1,
500};
501
502const char http_is_sep[256] = {
503 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
504 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
505 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
506 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
507 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
508};
509
510const char http_is_ctl[256] = {
511 [0 ... 31] = 1,
512 [127] = 1,
513};
514
515/*
516 * A token is any ASCII char that is neither a separator nor a CTL char.
517 * Do not overwrite values in assignment since gcc-2.95 will not handle
518 * them correctly. Instead, define every non-CTL char's status.
519 */
520const char http_is_token[256] = {
521 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
522 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
523 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
524 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
525 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
526 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
527 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
528 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
529 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
530 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
531 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
532 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
533 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
534 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
535 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
536 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
537 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
538 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
539 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
540 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
541 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
542 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
543 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
544 ['|'] = 1, ['}'] = 0, ['~'] = 1,
545};
546
547
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100548/*
549 * An http ver_token is any ASCII which can be found in an HTTP version,
550 * which includes 'H', 'T', 'P', '/', '.' and any digit.
551 */
552const char http_is_ver_token[256] = {
553 ['.'] = 1, ['/'] = 1,
554 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
555 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100556 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100557};
558
559
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100561 * Adds a header and its CRLF at the tail of the message's buffer, just before
562 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 * The header is also automatically added to the index <hdr_idx>, and the end
564 * of headers is automatically adjusted. The number of bytes added is returned
565 * on success, otherwise <0 is returned indicating an error.
566 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100567int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100568{
569 int bytes, len;
570
571 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200572 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100573 if (!bytes)
574 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100575 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
577}
578
579/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100580 * Adds a header and its CRLF at the tail of the message's buffer, just before
581 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100582 * the buffer is only opened and the space reserved, but nothing is copied.
583 * The header is also automatically added to the index <hdr_idx>, and the end
584 * of headers is automatically adjusted. The number of bytes added is returned
585 * on success, otherwise <0 is returned indicating an error.
586 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100587int http_header_add_tail2(struct http_msg *msg,
588 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100589{
590 int bytes;
591
Willy Tarreau9b28e032012-10-12 23:49:43 +0200592 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100593 if (!bytes)
594 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100595 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
597}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598
599/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100600 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
601 * If so, returns the position of the first non-space character relative to
602 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
603 * to return a pointer to the place after the first space. Returns 0 if the
604 * header name does not match. Checks are case-insensitive.
605 */
606int http_header_match2(const char *hdr, const char *end,
607 const char *name, int len)
608{
609 const char *val;
610
611 if (hdr + len >= end)
612 return 0;
613 if (hdr[len] != ':')
614 return 0;
615 if (strncasecmp(hdr, name, len) != 0)
616 return 0;
617 val = hdr + len + 1;
618 while (val < end && HTTP_IS_SPHT(*val))
619 val++;
620 if ((val >= end) && (len + 2 <= end - hdr))
621 return len + 2; /* we may replace starting from second space */
622 return val - hdr;
623}
624
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200625/* Find the first or next occurrence of header <name> in message buffer <sol>
626 * using headers index <idx>, and return it in the <ctx> structure. This
627 * structure holds everything necessary to use the header and find next
628 * occurrence. If its <idx> member is 0, the header is searched from the
629 * beginning. Otherwise, the next occurrence is returned. The function returns
630 * 1 when it finds a value, and 0 when there is no more. It is very similar to
631 * http_find_header2() except that it is designed to work with full-line headers
632 * whose comma is not a delimiter but is part of the syntax. As a special case,
633 * if ctx->val is NULL when searching for a new values of a header, the current
634 * header is rescanned. This allows rescanning after a header deletion.
635 */
636int http_find_full_header2(const char *name, int len,
637 char *sol, struct hdr_idx *idx,
638 struct hdr_ctx *ctx)
639{
640 char *eol, *sov;
641 int cur_idx, old_idx;
642
643 cur_idx = ctx->idx;
644 if (cur_idx) {
645 /* We have previously returned a header, let's search another one */
646 sol = ctx->line;
647 eol = sol + idx->v[cur_idx].len;
648 goto next_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
653 old_idx = 0;
654 cur_idx = hdr_idx_first_idx(idx);
655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
669 ctx->del = len;
670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
673
674 ctx->line = sol;
675 ctx->prev = old_idx;
676 ctx->idx = cur_idx;
677 ctx->val = sov - sol;
678 ctx->tws = 0;
679 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
680 eol--;
681 ctx->tws++;
682 }
683 ctx->vlen = eol - sov;
684 return 1;
685 }
686 next_hdr:
687 sol = eol + idx->v[cur_idx].cr + 1;
688 old_idx = cur_idx;
689 cur_idx = idx->v[cur_idx].next;
690 }
691 return 0;
692}
693
Willy Tarreauc90dc232015-02-20 13:51:36 +0100694/* Find the first or next header field in message buffer <sol> using headers
695 * index <idx>, and return it in the <ctx> structure. This structure holds
696 * everything necessary to use the header and find next occurrence. If its
697 * <idx> member is 0, the first header is retrieved. Otherwise, the next
698 * occurrence is returned. The function returns 1 when it finds a value, and
699 * 0 when there is no more. It is equivalent to http_find_full_header2() with
700 * no header name.
701 */
702int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
703{
704 char *eol, *sov;
705 int cur_idx, old_idx;
706 int len;
707
708 cur_idx = ctx->idx;
709 if (cur_idx) {
710 /* We have previously returned a header, let's search another one */
711 sol = ctx->line;
712 eol = sol + idx->v[cur_idx].len;
713 goto next_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
718 old_idx = 0;
719 cur_idx = hdr_idx_first_idx(idx);
720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
723 len = 0;
724 while (1) {
725 if (len >= eol - sol)
726 goto next_hdr;
727 if (sol[len] == ':')
728 break;
729 len++;
730 }
731
732 ctx->del = len;
733 sov = sol + len + 1;
734 while (sov < eol && http_is_lws[(unsigned char)*sov])
735 sov++;
736
737 ctx->line = sol;
738 ctx->prev = old_idx;
739 ctx->idx = cur_idx;
740 ctx->val = sov - sol;
741 ctx->tws = 0;
742
743 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
744 eol--;
745 ctx->tws++;
746 }
747 ctx->vlen = eol - sov;
748 return 1;
749
750 next_hdr:
751 sol = eol + idx->v[cur_idx].cr + 1;
752 old_idx = cur_idx;
753 cur_idx = idx->v[cur_idx].next;
754 }
755 return 0;
756}
757
Willy Tarreau68085d82010-01-18 14:54:04 +0100758/* Find the end of the header value contained between <s> and <e>. See RFC2616,
759 * par 2.2 for more information. Note that it requires a valid header to return
760 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100762char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200763{
764 int quoted, qdpair;
765
766 quoted = qdpair = 0;
767 for (; s < e; s++) {
768 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200769 else if (quoted) {
770 if (*s == '\\') qdpair = 1;
771 else if (*s == '"') quoted = 0;
772 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200773 else if (*s == '"') quoted = 1;
774 else if (*s == ',') return s;
775 }
776 return s;
777}
778
779/* Find the first or next occurrence of header <name> in message buffer <sol>
780 * using headers index <idx>, and return it in the <ctx> structure. This
781 * structure holds everything necessary to use the header and find next
782 * occurrence. If its <idx> member is 0, the header is searched from the
783 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100784 * 1 when it finds a value, and 0 when there is no more. It is designed to work
785 * with headers defined as comma-separated lists. As a special case, if ctx->val
786 * is NULL when searching for a new values of a header, the current header is
787 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 */
789int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100790 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 struct hdr_ctx *ctx)
792{
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *eol, *sov;
794 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200795
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 cur_idx = ctx->idx;
797 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 /* We have previously returned a value, let's search
799 * another one on the same line.
800 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200802 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100803 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 eol = sol + idx->v[cur_idx].len;
805
806 if (sov >= eol)
807 /* no more values in this header */
808 goto next_hdr;
809
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 /* values remaining for this header, skip the comma but save it
811 * for later use (eg: for header deletion).
812 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 sov++;
814 while (sov < eol && http_is_lws[(unsigned char)*sov])
815 sov++;
816
817 goto return_hdr;
818 }
819
820 /* first request for this header */
821 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100822 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200823 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 while (cur_idx) {
825 eol = sol + idx->v[cur_idx].len;
826
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200827 if (len == 0) {
828 /* No argument was passed, we want any header.
829 * To achieve this, we simply build a fake request. */
830 while (sol + len < eol && sol[len] != ':')
831 len++;
832 name = sol;
833 }
834
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835 if ((len < eol - sol) &&
836 (sol[len] == ':') &&
837 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100838 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200839 sov = sol + len + 1;
840 while (sov < eol && http_is_lws[(unsigned char)*sov])
841 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100842
Willy Tarreau33a7e692007-06-10 19:45:56 +0200843 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 ctx->prev = old_idx;
845 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->idx = cur_idx;
847 ctx->val = sov - sol;
848
849 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200850 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200851 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200852 eol--;
853 ctx->tws++;
854 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200855 ctx->vlen = eol - sov;
856 return 1;
857 }
858 next_hdr:
859 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100860 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200861 cur_idx = idx->v[cur_idx].next;
862 }
863 return 0;
864}
865
866int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100867 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200868 struct hdr_ctx *ctx)
869{
870 return http_find_header2(name, strlen(name), sol, idx, ctx);
871}
872
Willy Tarreau68085d82010-01-18 14:54:04 +0100873/* Remove one value of a header. This only works on a <ctx> returned by one of
874 * the http_find_header functions. The value is removed, as well as surrounding
875 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100876 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 * message <msg>. The new index is returned. If it is zero, it means there is
878 * no more header, so any processing may stop. The ctx is always left in a form
879 * that can be handled by http_find_header2() to find next occurrence.
880 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100881int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100882{
883 int cur_idx = ctx->idx;
884 char *sol = ctx->line;
885 struct hdr_idx_elem *hdr;
886 int delta, skip_comma;
887
888 if (!cur_idx)
889 return 0;
890
891 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200892 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100893 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200894 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 http_msg_move_end(msg, delta);
896 idx->used--;
897 hdr->len = 0; /* unused entry */
898 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100899 if (idx->tail == ctx->idx)
900 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100902 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200904 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100905 return ctx->idx;
906 }
907
908 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200909 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
910 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100911 */
912
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200913 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200914 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200915 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 NULL, 0);
917 hdr->len += delta;
918 http_msg_move_end(msg, delta);
919 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200920 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100921 return ctx->idx;
922}
923
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100924/* This function handles a server error at the stream interface level. The
925 * stream interface is assumed to be already in a closed state. An optional
926 * message is copied into the input buffer, and an HTTP status code stored.
927 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100928 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200930static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100933 channel_auto_read(si_oc(si));
934 channel_abort(si_oc(si));
935 channel_auto_close(si_oc(si));
936 channel_erase(si_oc(si));
937 channel_auto_close(si_ic(si));
938 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100939 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200940 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100941 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200944 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200946 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947}
948
Willy Tarreau87b09662015-04-03 00:22:06 +0200949/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100950 * and message.
951 */
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100954{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200955 if (s->be->errmsg[msgnum].str)
956 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200957 else if (strm_fe(s)->errmsg[msgnum].str)
958 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100959 else
960 return &http_err_chunks[msgnum];
961}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200964 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
965 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100967enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100968{
969 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971
972 m = ((unsigned)*str - 'A');
973
974 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 for (h = http_methods[m]; h->len > 0; h++) {
976 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100979 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200982 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983}
984
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985/* Parse the URI from the given transaction (which is assumed to be in request
986 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
987 * It is returned otherwise.
988 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200989char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100990{
991 char *ptr, *end;
992
Willy Tarreau9b28e032012-10-12 23:49:43 +0200993 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100994 end = ptr + txn->req.sl.rq.u_l;
995
996 if (ptr >= end)
997 return NULL;
998
999 /* RFC2616, par. 5.1.2 :
1000 * Request-URI = "*" | absuri | abspath | authority
1001 */
1002
1003 if (*ptr == '*')
1004 return NULL;
1005
1006 if (isalpha((unsigned char)*ptr)) {
1007 /* this is a scheme as described by RFC3986, par. 3.1 */
1008 ptr++;
1009 while (ptr < end &&
1010 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1011 ptr++;
1012 /* skip '://' */
1013 if (ptr == end || *ptr++ != ':')
1014 return NULL;
1015 if (ptr == end || *ptr++ != '/')
1016 return NULL;
1017 if (ptr == end || *ptr++ != '/')
1018 return NULL;
1019 }
1020 /* skip [user[:passwd]@]host[:[port]] */
1021
1022 while (ptr < end && *ptr != '/')
1023 ptr++;
1024
1025 if (ptr == end)
1026 return NULL;
1027
1028 /* OK, we got the '/' ! */
1029 return ptr;
1030}
1031
William Lallemand65ad6e12014-01-31 15:08:02 +01001032/* Parse the URI from the given string and look for the "/" beginning the PATH.
1033 * If not found, return NULL. It is returned otherwise.
1034 */
1035static char *
1036http_get_path_from_string(char *str)
1037{
1038 char *ptr = str;
1039
1040 /* RFC2616, par. 5.1.2 :
1041 * Request-URI = "*" | absuri | abspath | authority
1042 */
1043
1044 if (*ptr == '*')
1045 return NULL;
1046
1047 if (isalpha((unsigned char)*ptr)) {
1048 /* this is a scheme as described by RFC3986, par. 3.1 */
1049 ptr++;
1050 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1051 ptr++;
1052 /* skip '://' */
1053 if (*ptr == '\0' || *ptr++ != ':')
1054 return NULL;
1055 if (*ptr == '\0' || *ptr++ != '/')
1056 return NULL;
1057 if (*ptr == '\0' || *ptr++ != '/')
1058 return NULL;
1059 }
1060 /* skip [user[:passwd]@]host[:[port]] */
1061
1062 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1063 ptr++;
1064
1065 if (*ptr == '\0' || *ptr == ' ')
1066 return NULL;
1067
1068 /* OK, we got the '/' ! */
1069 return ptr;
1070}
1071
Willy Tarreau71241ab2012-12-27 11:30:54 +01001072/* Returns a 302 for a redirectable request that reaches a server working in
1073 * in redirect mode. This may only be called just after the stream interface
1074 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1075 * follow normal proxy processing. NOTE: this function is designed to support
1076 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001078void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079{
1080 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001081 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001083 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084
1085 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001086 trash.len = strlen(HTTP_302);
1087 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001089 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001090
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001092 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 return;
1094
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001095 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001096 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001097 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1098 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001099 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001101 /* 3: add the request URI. Since it was already forwarded, we need
1102 * to temporarily rewind the buffer.
1103 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001104 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001105 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001106
Willy Tarreauefb453c2008-10-26 20:49:47 +01001107 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001108 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 +02001109
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001110 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001111
Willy Tarreauefb453c2008-10-26 20:49:47 +01001112 if (!path)
1113 return;
1114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001115 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 return;
1117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 memcpy(trash.str + trash.len, path, len);
1119 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001120
1121 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001122 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1123 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001124 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001125 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1126 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001127 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128
1129 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001130 si_shutr(si);
1131 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 si->state = SI_ST_CLO;
1134
1135 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001136 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137
1138 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001139 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001140 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141}
1142
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001143/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001144 * that the server side is closed. Note that err_type is actually a
1145 * bitmask, where almost only aborts may be cumulated with other
1146 * values. We consider that aborted operations are more important
1147 * than timeouts or errors due to the fact that nobody else in the
1148 * logs might explain incomplete retries. All others should avoid
1149 * being cumulated. It should normally not be possible to have multiple
1150 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001151 * Note that connection errors appearing on the second request of a keep-alive
1152 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001154void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001156 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157
1158 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001160 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001161 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001163 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001164 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001167 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001170 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001171 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001173 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001174 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1177 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001178 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001179 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001181 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001182 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001183 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001185 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001186}
1187
Willy Tarreau42250582007-04-01 01:30:43 +02001188extern const char sess_term_cond[8];
1189extern const char sess_fin_state[8];
1190extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001191struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001192struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001193struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001194struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001195
Willy Tarreau117f59e2007-03-04 18:17:17 +01001196/*
1197 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001198 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001199 */
1200void capture_headers(char *som, struct hdr_idx *idx,
1201 char **cap, struct cap_hdr *cap_hdr)
1202{
1203 char *eol, *sol, *col, *sov;
1204 int cur_idx;
1205 struct cap_hdr *h;
1206 int len;
1207
1208 sol = som + hdr_idx_first_pos(idx);
1209 cur_idx = hdr_idx_first_idx(idx);
1210
1211 while (cur_idx) {
1212 eol = sol + idx->v[cur_idx].len;
1213
1214 col = sol;
1215 while (col < eol && *col != ':')
1216 col++;
1217
1218 sov = col + 1;
1219 while (sov < eol && http_is_lws[(unsigned char)*sov])
1220 sov++;
1221
1222 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001223 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001224 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1225 if (cap[h->index] == NULL)
1226 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001227 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228
1229 if (cap[h->index] == NULL) {
1230 Alert("HTTP capture : out of memory.\n");
1231 continue;
1232 }
1233
1234 len = eol - sov;
1235 if (len > h->len)
1236 len = h->len;
1237
1238 memcpy(cap[h->index], sov, len);
1239 cap[h->index][len]=0;
1240 }
1241 }
1242 sol = eol + idx->v[cur_idx].cr + 1;
1243 cur_idx = idx->v[cur_idx].next;
1244 }
1245}
1246
1247
Willy Tarreau42250582007-04-01 01:30:43 +02001248/* either we find an LF at <ptr> or we jump to <bad>.
1249 */
1250#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1251
1252/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1253 * otherwise to <http_msg_ood> with <state> set to <st>.
1254 */
1255#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1256 ptr++; \
1257 if (likely(ptr < end)) \
1258 goto good; \
1259 else { \
1260 state = (st); \
1261 goto http_msg_ood; \
1262 } \
1263 } while (0)
1264
1265
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001267 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1269 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1270 * will give undefined results.
1271 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1272 * and that msg->sol points to the beginning of the response.
1273 * If a complete line is found (which implies that at least one CR or LF is
1274 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1275 * returned indicating an incomplete line (which does not mean that parts have
1276 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1277 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1278 * upon next call.
1279 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1282 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001285const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001286 enum ht_state state, const char *ptr, const char *end,
1287 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001288{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001289 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001290
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001293 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001294 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1296
1297 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001298 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1300 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001301 state = HTTP_MSG_ERROR;
1302 break;
1303
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 goto http_msg_rpcode;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1312 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001313 state = HTTP_MSG_ERROR;
1314 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 if (likely(!HTTP_IS_LWS(*ptr)))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1320
1321 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001322 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1324 }
1325
1326 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 http_msg_rsp_reason:
1329 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001330 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 msg->sl.st.r_l = 0;
1332 goto http_msg_rpline_eol;
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 goto http_msg_rpreason;
1339 }
1340 if (likely(HTTP_IS_SPHT(*ptr)))
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1342 /* so it's a CR/LF, so there is no reason phrase */
1343 goto http_msg_rsp_reason;
1344
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001346 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (likely(!HTTP_IS_CRLF(*ptr)))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 http_msg_rpline_eol:
1351 /* We have seen the end of line. Note that we do not
1352 * necessarily have the \n yet, but at least we know that we
1353 * have EITHER \r OR \n, otherwise the response would not be
1354 * complete. We can then record the response length and return
1355 * to the caller which will be able to register it.
1356 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001357 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 return ptr;
1359
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001361#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1363 exit(1);
1364#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001365 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 }
1367
1368 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001373 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375}
1376
Willy Tarreau8973c702007-01-21 23:58:29 +01001377/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 * This function parses a request line between <ptr> and <end>, starting with
1379 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1380 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1381 * will give undefined results.
1382 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1383 * and that msg->sol points to the beginning of the request.
1384 * If a complete line is found (which implies that at least one CR or LF is
1385 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1386 * returned indicating an incomplete line (which does not mean that parts have
1387 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1388 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1389 * upon next call.
1390 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001391 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1393 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001394 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001396const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001397 enum ht_state state, const char *ptr, const char *end,
1398 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001400 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 if (likely(HTTP_IS_TOKEN(*ptr)))
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001409 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (likely(HTTP_IS_CRLF(*ptr))) {
1414 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001415 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001417 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001419 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001421 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 msg->sl.rq.v_l = 0;
1423 goto http_msg_rqline_eol;
1424 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001425 state = HTTP_MSG_ERROR;
1426 break;
1427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001429 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001431 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 goto http_msg_rquri;
1433 }
1434 if (likely(HTTP_IS_SPHT(*ptr)))
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1436 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1437 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001441 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001445 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1447 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001448
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001449 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001450 /* non-ASCII chars are forbidden unless option
1451 * accept-invalid-http-request is enabled in the frontend.
1452 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001453 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001454 if (msg->err_pos < -1)
1455 goto invalid_char;
1456 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001457 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1459 }
1460
1461 if (likely(HTTP_IS_CRLF(*ptr))) {
1462 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1463 goto http_msg_req09_uri_e;
1464 }
1465
1466 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001467 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001468 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001469 state = HTTP_MSG_ERROR;
1470 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001475 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 goto http_msg_rqver;
1477 }
1478 if (likely(HTTP_IS_SPHT(*ptr)))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1480 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1481 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001484 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001485 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001487
1488 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001489 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001490 http_msg_rqline_eol:
1491 /* We have seen the end of line. Note that we do not
1492 * necessarily have the \n yet, but at least we know that we
1493 * have EITHER \r OR \n, otherwise the request would not be
1494 * complete. We can then record the request length and return
1495 * to the caller which will be able to register it.
1496 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001497 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001498 return ptr;
1499 }
1500
1501 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001502 state = HTTP_MSG_ERROR;
1503 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001506#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1508 exit(1);
1509#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001510 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001514 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 if (ret_state)
1516 *ret_state = state;
1517 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001518 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001522/*
1523 * Returns the data from Authorization header. Function may be called more
1524 * than once so data is stored in txn->auth_data. When no header is found
1525 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001526 * searching again for something we are unable to find anyway. However, if
1527 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001528 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001529 */
1530
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001531/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1532 * set according to global.tune.bufsize.
1533 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001534char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001535
1536int
Willy Tarreau87b09662015-04-03 00:22:06 +02001537get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538{
1539
Willy Tarreaueee5b512015-04-03 23:46:31 +02001540 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001541 struct chunk auth_method;
1542 struct hdr_ctx ctx;
1543 char *h, *p;
1544 int len;
1545
1546#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001547 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001548#endif
1549
1550 if (txn->auth.method == HTTP_AUTH_WRONG)
1551 return 0;
1552
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001553 txn->auth.method = HTTP_AUTH_WRONG;
1554
1555 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001556
1557 if (txn->flags & TX_USE_PX_CONN) {
1558 h = "Proxy-Authorization";
1559 len = strlen(h);
1560 } else {
1561 h = "Authorization";
1562 len = strlen(h);
1563 }
1564
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001565 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001566 return 0;
1567
1568 h = ctx.line + ctx.val;
1569
1570 p = memchr(h, ' ', ctx.vlen);
1571 if (!p || p == h)
1572 return 0;
1573
1574 chunk_initlen(&auth_method, h, 0, p-h);
1575 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1576
1577 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1578
1579 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001580 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001581
1582 if (len < 0)
1583 return 0;
1584
1585
1586 get_http_auth_buff[len] = '\0';
1587
1588 p = strchr(get_http_auth_buff, ':');
1589
1590 if (!p)
1591 return 0;
1592
1593 txn->auth.user = get_http_auth_buff;
1594 *p = '\0';
1595 txn->auth.pass = p+1;
1596
1597 txn->auth.method = HTTP_AUTH_BASIC;
1598 return 1;
1599 }
1600
1601 return 0;
1602}
1603
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau8973c702007-01-21 23:58:29 +01001605/*
1606 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001607 * depending on the initial msg->msg_state. The caller is responsible for
1608 * ensuring that the message does not wrap. The function can be preempted
1609 * everywhere when data are missing and recalled at the exact same location
1610 * with no information loss. The message may even be realigned between two
1611 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001612 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001613 * fields. Note that msg->sol will be initialized after completing the first
1614 * state, so that none of the msg pointers has to be initialized prior to the
1615 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001617void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618{
Willy Tarreau3770f232013-12-07 00:01:53 +01001619 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001621 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001623 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001624 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 ptr = buf->p + msg->next;
1626 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (unlikely(ptr >= end))
1629 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /*
1633 * First, states that are specific to the response only.
1634 * We check them first so that request and headers are
1635 * closer to each other (accessed more often).
1636 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001639 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001640 /* we have a start of message, but we have to check
1641 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001642 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001643 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 if (unlikely(ptr != buf->p)) {
1645 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001646 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001647 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 }
Willy Tarreau26927362012-05-18 23:22:52 +02001650 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001651 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 hdr_idx_init(idx);
1653 state = HTTP_MSG_RPVER;
1654 goto http_msg_rpver;
1655 }
1656
1657 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1658 goto http_msg_invalid;
1659
1660 if (unlikely(*ptr == '\n'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1662 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1663 /* stop here */
1664
Willy Tarreau8973c702007-01-21 23:58:29 +01001665 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001666 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001667 EXPECT_LF_HERE(ptr, http_msg_invalid);
1668 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1669 /* stop here */
1670
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 case HTTP_MSG_RPVER_SP:
1674 case HTTP_MSG_RPCODE:
1675 case HTTP_MSG_RPCODE_SP:
1676 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001677 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001678 state, ptr, end,
1679 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 if (unlikely(!ptr))
1681 return;
1682
1683 /* we have a full response and we know that we have either a CR
1684 * or an LF at <ptr>.
1685 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1687
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001688 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 if (likely(*ptr == '\r'))
1690 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1691 goto http_msg_rpline_end;
1692
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001694 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 /* msg->sol must point to the first of CR or LF. */
1696 EXPECT_LF_HERE(ptr, http_msg_invalid);
1697 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1698 /* stop here */
1699
1700 /*
1701 * Second, states that are specific to the request only
1702 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001706 /* we have a start of message, but we have to check
1707 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001708 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001710 if (likely(ptr != buf->p)) {
1711 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001712 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001713 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001714 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 }
Willy Tarreau26927362012-05-18 23:22:52 +02001716 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001717 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 state = HTTP_MSG_RQMETH;
1719 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1723 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (unlikely(*ptr == '\n'))
1726 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1727 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001728 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001731 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 EXPECT_LF_HERE(ptr, http_msg_invalid);
1733 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001734 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001737 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 case HTTP_MSG_RQMETH_SP:
1739 case HTTP_MSG_RQURI:
1740 case HTTP_MSG_RQURI_SP:
1741 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001742 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001743 state, ptr, end,
1744 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 if (unlikely(!ptr))
1746 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 /* we have a full request and we know that we have either a CR
1749 * or an LF at <ptr>.
1750 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001752
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 if (likely(*ptr == '\r'))
1755 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001759 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 /* check for HTTP/0.9 request : no version information available.
1761 * msg->sol must point to the first of CR or LF.
1762 */
1763 if (unlikely(msg->sl.rq.v_l == 0))
1764 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 EXPECT_LF_HERE(ptr, http_msg_invalid);
1767 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001768 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8973c702007-01-21 23:58:29 +01001770 /*
1771 * Common states below
1772 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001774 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001775 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 if (likely(!HTTP_IS_CRLF(*ptr))) {
1777 goto http_msg_hdr_name;
1778 }
1779
1780 if (likely(*ptr == '\r'))
1781 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1782 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001785 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 /* assumes msg->sol points to the first char */
1787 if (likely(HTTP_IS_TOKEN(*ptr)))
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001790 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001793 if (likely(msg->err_pos < -1) || *ptr == '\n')
1794 goto http_msg_invalid;
1795
1796 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001797 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001798
1799 /* and we still accept this non-token character */
1800 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001802 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001803 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001804 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 if (likely(HTTP_IS_SPHT(*ptr)))
1806 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001809 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 if (likely(!HTTP_IS_CRLF(*ptr))) {
1812 goto http_msg_hdr_val;
1813 }
1814
1815 if (likely(*ptr == '\r'))
1816 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1817 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001820 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 EXPECT_LF_HERE(ptr, http_msg_invalid);
1822 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001825 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 if (likely(HTTP_IS_SPHT(*ptr))) {
1827 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001828 for (; buf->p + msg->sov < ptr; msg->sov++)
1829 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 goto http_msg_hdr_l1_sp;
1831 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001832 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001833 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 goto http_msg_complete_header;
1835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001837 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001838 /* assumes msg->sol points to the first char, and msg->sov
1839 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 */
1841 if (likely(!HTTP_IS_CRLF(*ptr)))
1842 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001843
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001844 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 /* Note: we could also copy eol into ->eoh so that we have the
1846 * real header end in case it ends with lots of LWS, but is this
1847 * really needed ?
1848 */
1849 if (likely(*ptr == '\r'))
1850 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1851 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001853 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001855 EXPECT_LF_HERE(ptr, http_msg_invalid);
1856 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001859 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1861 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001862 for (; buf->p + msg->eol < ptr; msg->eol++)
1863 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 goto http_msg_hdr_val;
1865 }
1866 http_msg_complete_header:
1867 /*
1868 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001869 * Assumes msg->sol points to the first char, msg->sov points
1870 * to the first character of the value and msg->eol to the
1871 * first CR or LF so we know how the line ends. We insert last
1872 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001874 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 idx, idx->tail) < 0))
1876 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001877
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 if (likely(!HTTP_IS_CRLF(*ptr))) {
1880 goto http_msg_hdr_name;
1881 }
1882
1883 if (likely(*ptr == '\r'))
1884 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1885 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001888 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001889 /* Assumes msg->sol points to the first of either CR or LF.
1890 * Sets ->sov and ->next to the total header length, ->eoh to
1891 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1892 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 EXPECT_LF_HERE(ptr, http_msg_invalid);
1894 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001896 msg->eoh = msg->sol;
1897 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001898 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001901
1902 case HTTP_MSG_ERROR:
1903 /* this may only happen if we call http_msg_analyser() twice with an error */
1904 break;
1905
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001906 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001907#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1909 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001910#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001911 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 }
1913 http_msg_ood:
1914 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001915 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 http_msg_invalid:
1920 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001921 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 return;
1924}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001925
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001926/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1927 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1928 * nothing is done and 1 is returned.
1929 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001930static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001931{
1932 int delta;
1933 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001934 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935
1936 if (msg->sl.rq.v_l != 0)
1937 return 1;
1938
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001939 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1940 if (txn->meth != HTTP_METH_GET)
1941 return 0;
1942
Willy Tarreau9b28e032012-10-12 23:49:43 +02001943 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001944 delta = 0;
1945
1946 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001947 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1948 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001949 }
1950 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001951 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001952 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001954 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001955 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001956 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001957 NULL, NULL);
1958 if (unlikely(!cur_end))
1959 return 0;
1960
1961 /* we have a full HTTP/1.0 request now and we know that
1962 * we have either a CR or an LF at <ptr>.
1963 */
1964 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1965 return 1;
1966}
1967
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001968/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001969 * and "keep-alive" values. If we already know that some headers may safely
1970 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1972 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001973 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1975 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1976 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001977 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001978 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001979void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001980{
Willy Tarreau5b154472009-12-21 20:11:07 +01001981 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001982 const char *hdr_val = "Connection";
1983 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001984
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001986 return;
1987
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1989 hdr_val = "Proxy-Connection";
1990 hdr_len = 16;
1991 }
1992
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001995 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1997 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001998 if (to_del & 2)
1999 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000 else
2001 txn->flags |= TX_CON_KAL_SET;
2002 }
2003 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2004 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002005 if (to_del & 1)
2006 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002007 else
2008 txn->flags |= TX_CON_CLO_SET;
2009 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002010 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2011 txn->flags |= TX_HDR_CONN_UPG;
2012 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002013 }
2014
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002015 txn->flags |= TX_HDR_CONN_PRS;
2016 return;
2017}
Willy Tarreau5b154472009-12-21 20:11:07 +01002018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019/* Apply desired changes on the Connection: header. Values may be removed and/or
2020 * added depending on the <wanted> flags, which are exclusively composed of
2021 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2022 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2023 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002024void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025{
2026 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002027 const char *hdr_val = "Connection";
2028 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002029
2030 ctx.idx = 0;
2031
Willy Tarreau88d349d2010-01-25 12:15:43 +01002032
2033 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2034 hdr_val = "Proxy-Connection";
2035 hdr_len = 16;
2036 }
2037
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002039 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002040 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2041 if (wanted & TX_CON_KAL_SET)
2042 txn->flags |= TX_CON_KAL_SET;
2043 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002044 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002045 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002046 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2047 if (wanted & TX_CON_CLO_SET)
2048 txn->flags |= TX_CON_CLO_SET;
2049 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002050 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002051 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002052 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002053
2054 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2055 return;
2056
2057 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2058 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002059 hdr_val = "Connection: close";
2060 hdr_len = 17;
2061 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2062 hdr_val = "Proxy-Connection: close";
2063 hdr_len = 23;
2064 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002065 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002066 }
2067
2068 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2069 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002070 hdr_val = "Connection: keep-alive";
2071 hdr_len = 22;
2072 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2073 hdr_val = "Proxy-Connection: keep-alive";
2074 hdr_len = 28;
2075 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002076 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002077 }
2078 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002079}
2080
Willy Tarreauc24715e2014-04-17 15:21:20 +02002081/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2082 * the first byte of data after the chunk size, so that we know we can forward
2083 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2084 * the chunk size. That way it is always possible to differentiate between the
2085 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002086 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002089static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002090{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002091 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002093 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002094 const char *end = buf->data + buf->size;
2095 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 unsigned int chunk = 0;
2097
2098 /* The chunk size is in the following form, though we are only
2099 * interested in the size and CRLF :
2100 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2101 */
2102 while (1) {
2103 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002104 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002107 if (c < 0) /* not a hex digit anymore */
2108 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002109 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002111 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002112 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002113 chunk = (chunk << 4) + c;
2114 }
2115
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002117 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002118 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119
2120 while (http_is_spht[(unsigned char)*ptr]) {
2121 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002122 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002123 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002124 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 }
2126
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127 /* Up to there, we know that at least one byte is present at *ptr. Check
2128 * for the end of chunk size.
2129 */
2130 while (1) {
2131 if (likely(HTTP_IS_CRLF(*ptr))) {
2132 /* we now have a CR or an LF at ptr */
2133 if (likely(*ptr == '\r')) {
2134 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002135 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002136 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002137 return 0;
2138 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002139
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002141 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002143 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 /* done */
2145 break;
2146 }
2147 else if (*ptr == ';') {
2148 /* chunk extension, ends at next CRLF */
2149 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002150 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002151 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002152 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002153
2154 while (!HTTP_IS_CRLF(*ptr)) {
2155 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002156 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002157 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002158 return 0;
2159 }
2160 /* we have a CRLF now, loop above */
2161 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002162 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002163 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002164 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002165 }
2166
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002168 * which may or may not be present. We save that into ->next,
2169 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002170 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002171 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002172 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002173 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002174 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002175 msg->chunk_len = chunk;
2176 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002178 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002179 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002181 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182}
2183
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002184/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002185 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186 * the trailers is found, it is automatically scheduled to be forwarded,
2187 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2188 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002189 * except maybe msg->next if it could parse some lines, and returns zero.
2190 * If a parse error is encountered, the function returns < 0 and does not
2191 * change anything except maybe msg->next. Note that the message must
2192 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002193 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002194 * forwarding, and that msg->next exactly matches the length of trailers
2195 * already parsed and not forwarded. It is also important to note that this
2196 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002198static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002199{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002200 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002201
Willy Tarreaua458b672012-03-05 11:17:50 +01002202 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002204 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002205 const char *ptr = b_ptr(buf, msg->next);
2206 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002207 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002208
2209 /* scan current line and stop at LF or CRLF */
2210 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002211 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 return 0;
2213
2214 if (*ptr == '\n') {
2215 if (!p1)
2216 p1 = ptr;
2217 p2 = ptr;
2218 break;
2219 }
2220
2221 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002222 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002223 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002224 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002225 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002226 p1 = ptr;
2227 }
2228
2229 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002230 if (ptr >= buf->data + buf->size)
2231 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002232 }
2233
2234 /* after LF; point to beginning of next line */
2235 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002236 if (p2 >= buf->data + buf->size)
2237 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002238
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002240 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002241 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002242
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002243 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002244 /* LF/CRLF at beginning of line => end of trailers at p2.
2245 * Everything was scheduled for forwarding, there's nothing
2246 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002247 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002248 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002249 msg->msg_state = HTTP_MSG_DONE;
2250 return 1;
2251 }
2252 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002253 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002254 }
2255}
2256
Willy Tarreauc24715e2014-04-17 15:21:20 +02002257/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2258 * or a possible LF alone at the end of a chunk. It automatically adjusts
2259 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002260 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2262 * not enough data are available, the function does not change anything and
2263 * returns zero. If a parse error is encountered, the function returns < 0 and
2264 * does not change anything. Note: this function is designed to parse wrapped
2265 * CRLF at the end of the buffer.
2266 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002267static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002268{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002269 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002270 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271 int bytes;
2272
2273 /* NB: we'll check data availabilty at the end. It's not a
2274 * problem because whatever we match first will be checked
2275 * against the correct length.
2276 */
2277 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002278 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002279 if (*ptr == '\r') {
2280 bytes++;
2281 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002282 if (ptr >= buf->data + buf->size)
2283 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284 }
2285
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002286 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 return 0;
2288
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002289 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002290 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293
2294 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002295 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002296 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002297 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002298 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002299 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2300 return 1;
2301}
Willy Tarreau5b154472009-12-21 20:11:07 +01002302
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002303/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2304 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2305 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2306 * Unparsable qvalues return 1000 as "q=1.000".
2307 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002308int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002309{
2310 int q = 1000;
2311
Willy Tarreau506c69a2014-07-08 00:59:48 +02002312 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 goto out;
2314 q = (*qvalue++ - '0') * 1000;
2315
2316 if (*qvalue++ != '.')
2317 goto out;
2318
Willy Tarreau506c69a2014-07-08 00:59:48 +02002319 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002320 goto out;
2321 q += (*qvalue++ - '0') * 100;
2322
Willy Tarreau506c69a2014-07-08 00:59:48 +02002323 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002324 goto out;
2325 q += (*qvalue++ - '0') * 10;
2326
Willy Tarreau506c69a2014-07-08 00:59:48 +02002327 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002328 goto out;
2329 q += (*qvalue++ - '0') * 1;
2330 out:
2331 if (q > 1000)
2332 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002333 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002334 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002335 return q;
2336}
William Lallemand82fe75c2012-10-23 10:25:10 +02002337
2338/*
2339 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002340 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002341int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002342{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002343 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002344 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 struct hdr_ctx ctx;
2346 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002347 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002348
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002349 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2350 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002351 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2352 */
2353 ctx.idx = 0;
2354 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2355 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002356 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2357 (ctx.vlen < 31 ||
2358 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2359 ctx.line[ctx.val + 30] < '6' ||
2360 (ctx.line[ctx.val + 30] == '6' &&
2361 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2362 s->comp_algo = NULL;
2363 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002364 }
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002367 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002368 int best_q = 0;
2369
William Lallemand82fe75c2012-10-23 10:25:10 +02002370 ctx.idx = 0;
2371 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002372 const char *qval;
2373 int q;
2374 int toklen;
2375
2376 /* try to isolate the token from the optional q-value */
2377 toklen = 0;
2378 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2379 toklen++;
2380
2381 qval = ctx.line + ctx.val + toklen;
2382 while (1) {
2383 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2384 qval++;
2385
2386 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2387 qval = NULL;
2388 break;
2389 }
2390 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002391
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002392 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2393 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002394
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002395 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2396 qval = NULL;
2397 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002399 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2400 break;
2401
2402 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2403 qval++;
2404 }
2405
2406 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002407 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002408
2409 if (q <= best_q)
2410 continue;
2411
2412 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2413 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002414 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002415 s->comp_algo = comp_algo;
2416 best_q = q;
2417 break;
2418 }
2419 }
2420 }
2421 }
2422
2423 /* remove all occurrences of the header when "compression offload" is set */
2424 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002425 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002426 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2427 ctx.idx = 0;
2428 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2429 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002430 }
2431 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002432 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 }
2434
2435 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002436 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002437 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002438 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002439 s->comp_algo = comp_algo;
2440 return 1;
2441 }
2442 }
2443 }
2444
2445 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002446 return 0;
2447}
2448
2449/*
2450 * Selects a comression algorithm depending of the server response.
2451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002452int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002453{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002454 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 struct http_msg *msg = &txn->rsp;
2456 struct hdr_ctx ctx;
2457 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002458
2459 /* no common compression algorithm was found in request header */
2460 if (s->comp_algo == NULL)
2461 goto fail;
2462
2463 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002464 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002465 goto fail;
2466
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002467 /* compress 200,201,202,203 responses only */
2468 if ((txn->status != 200) &&
2469 (txn->status != 201) &&
2470 (txn->status != 202) &&
2471 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002472 goto fail;
2473
William Lallemand82fe75c2012-10-23 10:25:10 +02002474 /* Content-Length is null */
2475 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2476 goto fail;
2477
2478 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002479 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2481 goto fail;
2482
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002483 /* no compression when Cache-Control: no-transform is present in the message */
2484 ctx.idx = 0;
2485 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2486 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2487 goto fail;
2488 }
2489
William Lallemand82fe75c2012-10-23 10:25:10 +02002490 comp_type = NULL;
2491
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002492 /* we don't want to compress multipart content-types, nor content-types that are
2493 * not listed in the "compression type" directive if any. If no content-type was
2494 * found but configuration requires one, we don't compress either. Backend has
2495 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002496 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002497 ctx.idx = 0;
2498 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2499 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2500 goto fail;
2501
2502 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002503 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002504 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002505 if (ctx.vlen >= comp_type->name_len &&
2506 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002507 /* this Content-Type should be compressed */
2508 break;
2509 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002510 /* this Content-Type should not be compressed */
2511 if (comp_type == NULL)
2512 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002513 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002514 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002515 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002516 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002517 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002518 }
2519
William Lallemandd85f9172012-11-09 17:05:39 +01002520 /* limit compression rate */
2521 if (global.comp_rate_lim > 0)
2522 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2523 goto fail;
2524
William Lallemand072a2bf2012-11-20 17:01:01 +01002525 /* limit cpu usage */
2526 if (idle_pct < compress_min_idle)
2527 goto fail;
2528
William Lallemand4c49fae2012-11-07 15:00:23 +01002529 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002530 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002531 goto fail;
2532
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002534
William Lallemand82fe75c2012-10-23 10:25:10 +02002535 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002536 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002537 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2538 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2539
2540 /* add Transfer-Encoding header */
2541 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2542 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2543
2544 /*
2545 * Add Content-Encoding header when it's not identity encoding.
2546 * RFC 2616 : Identity encoding: This content-coding is used only in the
2547 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2548 * header.
2549 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002550 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002551 trash.len = 18;
2552 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002553 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2554 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 trash.str[trash.len] = '\0';
2556 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002557 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002558 return 1;
2559
2560fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002561 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002562 return 0;
2563}
2564
Willy Tarreau87b09662015-04-03 00:22:06 +02002565void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002566{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002567 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002568 int tmp = TX_CON_WANT_KAL;
2569
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002570 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2571 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002572 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2573 tmp = TX_CON_WANT_TUN;
2574
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002575 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002576 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2577 tmp = TX_CON_WANT_TUN;
2578 }
2579
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002580 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002581 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2582 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002583 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002584 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2585 tmp = TX_CON_WANT_CLO;
2586 else
2587 tmp = TX_CON_WANT_SCL;
2588 }
2589
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002590 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002591 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2592 tmp = TX_CON_WANT_CLO;
2593
2594 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2595 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2596
2597 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2598 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2599 /* parse the Connection header and possibly clean it */
2600 int to_del = 0;
2601 if ((msg->flags & HTTP_MSGF_VER_11) ||
2602 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002603 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002604 to_del |= 2; /* remove "keep-alive" */
2605 if (!(msg->flags & HTTP_MSGF_VER_11))
2606 to_del |= 1; /* remove "close" */
2607 http_parse_connection_header(txn, msg, to_del);
2608 }
2609
2610 /* check if client or config asks for explicit close in KAL/SCL */
2611 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2612 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2613 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2614 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2615 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002616 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002617 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2618}
William Lallemand82fe75c2012-10-23 10:25:10 +02002619
Willy Tarreaud787e662009-07-07 10:14:51 +02002620/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2621 * processing can continue on next analysers, or zero if it either needs more
2622 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002623 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002624 * when it has nothing left to do, and may remove any analyser when it wants to
2625 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002626 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002627int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002628{
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 /*
2630 * We will parse the partial (or complete) lines.
2631 * We will check the request syntax, and also join multi-line
2632 * headers. An index of all the lines will be elaborated while
2633 * parsing.
2634 *
2635 * For the parsing, we use a 28 states FSM.
2636 *
2637 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002638 * req->buf->p = beginning of request
2639 * req->buf->p + msg->eoh = end of processed headers / start of current one
2640 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002641 * msg->eol = end of current header or line (LF or CRLF)
2642 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002643 *
2644 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002645 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2647 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002651 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002652 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002654 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002655
Willy Tarreau87b09662015-04-03 00:22:06 +02002656 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002657 now_ms, __FUNCTION__,
2658 s,
2659 req,
2660 req->rex, req->wex,
2661 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002663 req->analysers);
2664
Willy Tarreau52a0c602009-08-16 22:45:38 +02002665 /* we're speaking HTTP here, so let's speak HTTP to the client */
2666 s->srv_error = http_return_srv_error;
2667
Willy Tarreau83e3af02009-12-28 17:39:57 +01002668 /* There's a protected area at the end of the buffer for rewriting
2669 * purposes. We don't want to start to parse the request if the
2670 * protected area is affected, because we may have to move processed
2671 * data later, which is much more complicated.
2672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002673 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002674 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002675 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002676 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002677 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002678 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002679 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002680 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002681 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002682 return 0;
2683 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002684 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2685 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2686 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002687 }
2688
Willy Tarreau065e8332010-01-08 00:30:20 +01002689 /* Note that we have the same problem with the response ; we
2690 * may want to send a redirect, error or anything which requires
2691 * some spare space. So we'll ensure that we have at least
2692 * maxrewrite bytes available in the response buffer before
2693 * processing that one. This will only affect pipelined
2694 * keep-alive requests.
2695 */
2696 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002697 unlikely(!channel_is_rewritable(&s->res) ||
2698 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2699 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2700 if (s->res.buf->o) {
2701 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002702 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002703 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002704 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002705 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2706 s->res.flags |= CF_WAKE_WRITE;
2707 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002708 return 0;
2709 }
2710 }
2711
Willy Tarreau9b28e032012-10-12 23:49:43 +02002712 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002713 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002714 }
2715
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 /* 1: we might have to print this header in debug mode */
2717 if (unlikely((global.mode & MODE_DEBUG) &&
2718 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002719 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002721
Willy Tarreau9b28e032012-10-12 23:49:43 +02002722 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002723 /* this is a bit complex : in case of error on the request line,
2724 * we know that rq.l is still zero, so we display only the part
2725 * up to the end of the line (truncated by debug_hdr).
2726 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002727 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002729
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 sol += hdr_idx_first_pos(&txn->hdr_idx);
2731 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 while (cur_idx) {
2734 eol = sol + txn->hdr_idx.v[cur_idx].len;
2735 debug_hdr("clihdr", s, sol, eol);
2736 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2737 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002738 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 }
2740
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /*
2743 * Now we quickly check if we have found a full valid request.
2744 * If not so, we check the FD and buffer states before leaving.
2745 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002746 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002747 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002748 * on a keep-alive stream, if we encounter and error, close, t/o,
2749 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002750 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002752 * Last, we may increase some tracked counters' http request errors on
2753 * the cases that are deliberately the client's fault. For instance,
2754 * a timeout or connection reset is not counted as an error. However
2755 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002757
Willy Tarreau655dce92009-11-08 13:10:58 +01002758 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002762 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002763 stream_inc_http_req_ctr(s);
2764 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002765 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 /* 1: Since we are in header mode, if there's no space
2770 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002771 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 * must terminate it now.
2773 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002774 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 /* FIXME: check if URI is set and return Status
2776 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002777 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002778 stream_inc_http_req_ctr(s);
2779 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002780 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002781 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002782 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 goto return_bad_req;
2784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002787 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002788 if (!(s->flags & SF_ERR_MASK))
2789 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002790
Willy Tarreaufcffa692010-01-10 14:21:19 +01002791 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002792 goto failed_keep_alive;
2793
Willy Tarreau0f228a02015-05-01 15:37:53 +02002794 if (sess->fe->options & PR_O_IGNORE_PRB)
2795 goto failed_keep_alive;
2796
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002799 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002800 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002801 }
2802
Willy Tarreaudc979f22012-12-04 10:39:01 +01002803 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002804 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 msg->msg_state = HTTP_MSG_ERROR;
2806 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002807
Willy Tarreau87b09662015-04-03 00:22:06 +02002808 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002809 proxy_inc_fe_req_ctr(sess->fe);
2810 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002811 if (sess->listener->counters)
2812 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002813
Willy Tarreaue7dff022015-04-03 01:14:29 +02002814 if (!(s->flags & SF_FINST_MASK))
2815 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 return 0;
2817 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002818
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002820 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002821 if (!(s->flags & SF_ERR_MASK))
2822 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002823
Willy Tarreaufcffa692010-01-10 14:21:19 +01002824 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002825 goto failed_keep_alive;
2826
Willy Tarreau0f228a02015-05-01 15:37:53 +02002827 if (sess->fe->options & PR_O_IGNORE_PRB)
2828 goto failed_keep_alive;
2829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002831 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002832 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002833 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002835 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002836 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002837 msg->msg_state = HTTP_MSG_ERROR;
2838 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002839
Willy Tarreau87b09662015-04-03 00:22:06 +02002840 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002841 proxy_inc_fe_req_ctr(sess->fe);
2842 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002843 if (sess->listener->counters)
2844 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002845
Willy Tarreaue7dff022015-04-03 01:14:29 +02002846 if (!(s->flags & SF_FINST_MASK))
2847 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 return 0;
2849 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002852 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002853 if (!(s->flags & SF_ERR_MASK))
2854 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002855
Willy Tarreaufcffa692010-01-10 14:21:19 +01002856 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002857 goto failed_keep_alive;
2858
Willy Tarreau0f228a02015-05-01 15:37:53 +02002859 if (sess->fe->options & PR_O_IGNORE_PRB)
2860 goto failed_keep_alive;
2861
Willy Tarreau4076a152009-04-02 15:18:36 +02002862 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002863 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002865 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 msg->msg_state = HTTP_MSG_ERROR;
2867 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002868
Willy Tarreau87b09662015-04-03 00:22:06 +02002869 stream_inc_http_err_ctr(s);
2870 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002871 proxy_inc_fe_req_ctr(sess->fe);
2872 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002873 if (sess->listener->counters)
2874 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002875
Willy Tarreaue7dff022015-04-03 01:14:29 +02002876 if (!(s->flags & SF_FINST_MASK))
2877 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002878 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002879 }
2880
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002881 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002882 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002883 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002884#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002885 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2886 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002887 /* We need more data, we have to re-enable quick-ack in case we
2888 * previously disabled it, otherwise we might cause the client
2889 * to delay next data.
2890 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002891 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002892 }
2893#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002894
Willy Tarreaufcffa692010-01-10 14:21:19 +01002895 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2896 /* If the client starts to talk, let's fall back to
2897 * request timeout processing.
2898 */
2899 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002900 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002901 }
2902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002904 if (!tick_isset(req->analyse_exp)) {
2905 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2906 (txn->flags & TX_WAIT_NEXT_RQ) &&
2907 tick_isset(s->be->timeout.httpka))
2908 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2909 else
2910 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002912
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 /* we're not ready yet */
2914 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002915
2916 failed_keep_alive:
2917 /* Here we process low-level errors for keep-alive requests. In
2918 * short, if the request is not the first one and it experiences
2919 * a timeout, read error or shutdown, we just silently close so
2920 * that the client can try again.
2921 */
2922 txn->status = 0;
2923 msg->msg_state = HTTP_MSG_RQBEFORE;
2924 req->analysers = 0;
2925 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002926 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002927 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002928 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002929 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002931
Willy Tarreaud787e662009-07-07 10:14:51 +02002932 /* OK now we have a complete HTTP request with indexed headers. Let's
2933 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002934 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002935 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002936 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002937 * byte after the last LF. msg->sov points to the first byte of data.
2938 * msg->eol cannot be trusted because it may have been left uninitialized
2939 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002940 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002941
Willy Tarreau87b09662015-04-03 00:22:06 +02002942 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002943 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002944
Willy Tarreaub16a5742010-01-10 14:46:16 +01002945 if (txn->flags & TX_WAIT_NEXT_RQ) {
2946 /* kill the pending keep-alive timeout */
2947 txn->flags &= ~TX_WAIT_NEXT_RQ;
2948 req->analyse_exp = TICK_ETERNITY;
2949 }
2950
2951
Willy Tarreaud787e662009-07-07 10:14:51 +02002952 /* Maybe we found in invalid header name while we were configured not
2953 * to block on that, so we have to capture it now.
2954 */
2955 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002956 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002957
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 /*
2959 * 1: identify the method
2960 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002962
2963 /* we can make use of server redirect on GET and HEAD */
2964 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002965 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /*
2968 * 2: check if the URI matches the monitor_uri.
2969 * We have to do this for every request which gets in, because
2970 * the monitor-uri is defined by the frontend.
2971 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002972 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2973 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002974 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002975 sess->fe->monitor_uri,
2976 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002977 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002978 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002979 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002981
Willy Tarreaue7dff022015-04-03 01:14:29 +02002982 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002983 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002984
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002986 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002987 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002988
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 ret = acl_pass(ret);
2990 if (cond->pol == ACL_COND_UNLESS)
2991 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002992
Willy Tarreau59234e92008-11-30 23:51:27 +01002993 if (ret) {
2994 /* we fail this request, let's return 503 service unavail */
2995 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002996 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002997 if (!(s->flags & SF_ERR_MASK))
2998 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002999 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003000 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003001 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003002
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 /* nothing to fail, let's reply normaly */
3004 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003005 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003006 if (!(s->flags & SF_ERR_MASK))
3007 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 goto return_prx_cond;
3009 }
3010
3011 /*
3012 * 3: Maybe we have to copy the original REQURI for the logs ?
3013 * Note: we cannot log anymore if the request has been
3014 * classified as invalid.
3015 */
3016 if (unlikely(s->logs.logwait & LW_REQ)) {
3017 /* we have a complete HTTP request that we must log */
3018 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3019 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003020
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 if (urilen >= REQURI_LEN)
3022 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003023 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003025
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003026 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 s->do_log(s);
3028 } else {
3029 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003030 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003031 }
Willy Tarreau06619262006-12-17 08:37:22 +01003032
Willy Tarreau91852eb2015-05-01 13:26:00 +02003033 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3034 * exactly one digit "." one digit. This check may be disabled using
3035 * option accept-invalid-http-request.
3036 */
3037 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3038 if (msg->sl.rq.v_l != 8) {
3039 msg->err_pos = msg->sl.rq.v;
3040 goto return_bad_req;
3041 }
3042
3043 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3044 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3045 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3046 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3047 msg->err_pos = msg->sl.rq.v + 4;
3048 goto return_bad_req;
3049 }
3050 }
Willy Tarreau13317662015-05-01 13:47:08 +02003051 else {
3052 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3053 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3054 goto return_bad_req;
3055 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003056
Willy Tarreau5b154472009-12-21 20:11:07 +01003057 /* ... and check if the request is HTTP/1.1 or above */
3058 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003059 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3060 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3061 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003062 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003063
3064 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003065 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 +01003066
Willy Tarreau88d349d2010-01-25 12:15:43 +01003067 /* if the frontend has "option http-use-proxy-header", we'll check if
3068 * we have what looks like a proxied connection instead of a connection,
3069 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3070 * Note that this is *not* RFC-compliant, however browsers and proxies
3071 * happen to do that despite being non-standard :-(
3072 * We consider that a request not beginning with either '/' or '*' is
3073 * a proxied connection, which covers both "scheme://location" and
3074 * CONNECT ip:port.
3075 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003076 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003077 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003078 txn->flags |= TX_USE_PX_CONN;
3079
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003080 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003081 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003082
Willy Tarreau59234e92008-11-30 23:51:27 +01003083 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003084 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003085 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003086 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003087
Willy Tarreau557f1992015-05-01 10:05:17 +02003088 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3089 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003090 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003091 * The length of a message body is determined by one of the following
3092 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003093 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003094 * 1. Any response to a HEAD request and any response with a 1xx
3095 * (Informational), 204 (No Content), or 304 (Not Modified) status
3096 * code is always terminated by the first empty line after the
3097 * header fields, regardless of the header fields present in the
3098 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003100 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3101 * the connection will become a tunnel immediately after the empty
3102 * line that concludes the header fields. A client MUST ignore any
3103 * Content-Length or Transfer-Encoding header fields received in
3104 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003105 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003106 * 3. If a Transfer-Encoding header field is present and the chunked
3107 * transfer coding (Section 4.1) is the final encoding, the message
3108 * body length is determined by reading and decoding the chunked
3109 * data until the transfer coding indicates the data is complete.
3110 *
3111 * If a Transfer-Encoding header field is present in a response and
3112 * the chunked transfer coding is not the final encoding, the
3113 * message body length is determined by reading the connection until
3114 * it is closed by the server. If a Transfer-Encoding header field
3115 * is present in a request and the chunked transfer coding is not
3116 * the final encoding, the message body length cannot be determined
3117 * reliably; the server MUST respond with the 400 (Bad Request)
3118 * status code and then close the connection.
3119 *
3120 * If a message is received with both a Transfer-Encoding and a
3121 * Content-Length header field, the Transfer-Encoding overrides the
3122 * Content-Length. Such a message might indicate an attempt to
3123 * perform request smuggling (Section 9.5) or response splitting
3124 * (Section 9.4) and ought to be handled as an error. A sender MUST
3125 * remove the received Content-Length field prior to forwarding such
3126 * a message downstream.
3127 *
3128 * 4. If a message is received without Transfer-Encoding and with
3129 * either multiple Content-Length header fields having differing
3130 * field-values or a single Content-Length header field having an
3131 * invalid value, then the message framing is invalid and the
3132 * recipient MUST treat it as an unrecoverable error. If this is a
3133 * request message, the server MUST respond with a 400 (Bad Request)
3134 * status code and then close the connection. If this is a response
3135 * message received by a proxy, the proxy MUST close the connection
3136 * to the server, discard the received response, and send a 502 (Bad
3137 * Gateway) response to the client. If this is a response message
3138 * received by a user agent, the user agent MUST close the
3139 * connection to the server and discard the received response.
3140 *
3141 * 5. If a valid Content-Length header field is present without
3142 * Transfer-Encoding, its decimal value defines the expected message
3143 * body length in octets. If the sender closes the connection or
3144 * the recipient times out before the indicated number of octets are
3145 * received, the recipient MUST consider the message to be
3146 * incomplete and close the connection.
3147 *
3148 * 6. If this is a request message and none of the above are true, then
3149 * the message body length is zero (no message body is present).
3150 *
3151 * 7. Otherwise, this is a response message without a declared message
3152 * body length, so the message body length is determined by the
3153 * number of octets received prior to the server closing the
3154 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003155 */
3156
Willy Tarreau32b47f42009-10-18 20:55:02 +02003157 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003158 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003159 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003160 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003161 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3162 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003163 /* chunked not last, return badreq */
3164 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003165 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003166 }
3167
Willy Tarreau1c913912015-04-30 10:57:51 +02003168 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003169 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003170 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3171 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3172 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3173 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003174 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003175 signed long long cl;
3176
Willy Tarreauad14f752011-09-02 20:33:27 +02003177 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003178 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003179 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003180 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003181
Willy Tarreauad14f752011-09-02 20:33:27 +02003182 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003183 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003185 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003186
Willy Tarreauad14f752011-09-02 20:33:27 +02003187 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003188 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003189 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003190 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003191
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003192 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003193 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003194 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003195 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003197 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003198 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003199 }
3200
Willy Tarreau34dfc602015-05-01 10:09:49 +02003201 /* even bodyless requests have a known length */
3202 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003203
Willy Tarreau179085c2014-04-28 16:48:56 +02003204 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3205 * only change if both the request and the config reference something else.
3206 * Option httpclose by itself sets tunnel mode where headers are mangled.
3207 * However, if another mode is set, it will affect it (eg: server-close/
3208 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3209 * if FE and BE have the same settings (common). The method consists in
3210 * checking if options changed between the two calls (implying that either
3211 * one is non-null, or one of them is non-null and we are there for the first
3212 * time.
3213 */
3214 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003215 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003216 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003217
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003218 /* we may have to wait for the request's body */
3219 if ((s->be->options & PR_O_WREQ_BODY) &&
3220 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3221 req->analysers |= AN_REQ_HTTP_BODY;
3222
Willy Tarreaud787e662009-07-07 10:14:51 +02003223 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003224 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003225 req->analyse_exp = TICK_ETERNITY;
3226 return 1;
3227
3228 return_bad_req:
3229 /* We centralize bad requests processing here */
3230 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3231 /* we detected a parsing error. We want to archive this request
3232 * in the dedicated proxy area for later troubleshooting.
3233 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003234 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003235 }
3236
3237 txn->req.msg_state = HTTP_MSG_ERROR;
3238 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003239 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003240
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003241 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003242 if (sess->listener->counters)
3243 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003244
3245 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003246 if (!(s->flags & SF_ERR_MASK))
3247 s->flags |= SF_ERR_PRXCOND;
3248 if (!(s->flags & SF_FINST_MASK))
3249 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003250
3251 req->analysers = 0;
3252 req->analyse_exp = TICK_ETERNITY;
3253 return 0;
3254}
3255
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003256
Willy Tarreau347a35d2013-11-22 17:51:09 +01003257/* This function prepares an applet to handle the stats. It can deal with the
3258 * "100-continue" expectation, check that admin rules are met for POST requests,
3259 * and program a response message if something was unexpected. It cannot fail
3260 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003261 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003262 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003263 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003264 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003265int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003266{
3267 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003268 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003269 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003270 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003271 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003272 struct uri_auth *uri_auth = s->be->uri_auth;
3273 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003274 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003276 appctx = si_appctx(si);
3277 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3278 appctx->st1 = appctx->st2 = 0;
3279 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3280 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003281 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003282 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003283
3284 uri = msg->chn->buf->p + msg->sl.rq.u;
3285 lookup = uri + uri_auth->uri_len;
3286
3287 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3288 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003289 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003290 break;
3291 }
3292 }
3293
3294 if (uri_auth->refresh) {
3295 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3296 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003297 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003298 break;
3299 }
3300 }
3301 }
3302
3303 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3304 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003305 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003306 break;
3307 }
3308 }
3309
3310 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3311 if (memcmp(h, ";st=", 4) == 0) {
3312 int i;
3313 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003314 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003315 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3316 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003317 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003318 break;
3319 }
3320 }
3321 break;
3322 }
3323 }
3324
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 appctx->ctx.stats.scope_str = 0;
3326 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003327 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3328 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3329 int itx = 0;
3330 const char *h2;
3331 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3332 const char *err;
3333
3334 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3335 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003336 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003337 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3338 itx++;
3339 h++;
3340 }
3341
3342 if (itx > STAT_SCOPE_TXT_MAXLEN)
3343 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003344 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003345
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003346 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003347 memcpy(scope_txt, h2, itx);
3348 scope_txt[itx] = '\0';
3349 err = invalid_char(scope_txt);
3350 if (err) {
3351 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003352 appctx->ctx.stats.scope_str = 0;
3353 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003354 }
3355 break;
3356 }
3357 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003358
3359 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003360 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003361 int ret = 1;
3362
3363 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003364 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003365 ret = acl_pass(ret);
3366 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3367 ret = !ret;
3368 }
3369
3370 if (ret) {
3371 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003373 break;
3374 }
3375 }
3376
3377 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003378 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003379 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003380 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003381 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3382 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003383 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003384 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003385 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003386 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3387 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003388 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003389 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003390 else {
3391 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
3394
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003395 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003396 return 1;
3397}
3398
Lukas Tribus67db8df2013-06-23 17:37:13 +02003399/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3400 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3401 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003402void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003403{
3404#ifdef IP_TOS
3405 if (from.ss_family == AF_INET)
3406 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3407#endif
3408#ifdef IPV6_TCLASS
3409 if (from.ss_family == AF_INET6) {
3410 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3411 /* v4-mapped addresses need IP_TOS */
3412 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3413 else
3414 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3415 }
3416#endif
3417}
3418
Willy Tarreau87b09662015-04-03 00:22:06 +02003419int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003420 const char* name, unsigned int name_len,
3421 const char *str, struct my_regex *re,
3422 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003423{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003424 struct hdr_ctx ctx;
3425 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003426 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003427 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3428 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003429 struct chunk *output = get_trash_chunk();
3430
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003431 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003432
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003433 /* Choose the header browsing function. */
3434 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003435 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436 http_find_hdr_func = http_find_header2;
3437 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003438 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003439 http_find_hdr_func = http_find_full_header2;
3440 break;
3441 default: /* impossible */
3442 return -1;
3443 }
3444
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003445 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3446 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003447 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003448 char *val = ctx.line + ctx.val;
3449 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003450
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003451 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3452 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003453
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003454 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003455 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003456 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003457
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003458 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003459
3460 hdr->len += delta;
3461 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003462
3463 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003464 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003465 }
3466
3467 return 0;
3468}
3469
Willy Tarreau87b09662015-04-03 00:22:06 +02003470static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003471 const char* name, unsigned int name_len,
3472 struct list *fmt, struct my_regex *re,
3473 int action)
3474{
3475 struct chunk *replace = get_trash_chunk();
3476
3477 replace->len = build_logline(s, replace->str, replace->size, fmt);
3478 if (replace->len >= replace->size - 1)
3479 return -1;
3480
3481 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3482}
3483
Willy Tarreau87b09662015-04-03 00:22:06 +02003484/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 * transaction <txn>. Returns the verdict of the first rule that prevents
3486 * further processing of the request (auth, deny, ...), and defaults to
3487 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3488 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3489 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003490 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003491enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003492http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003493{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003494 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003495 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003496 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003497 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003498 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003499 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003500
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003501 /* If "the current_rule_list" match the executed rule list, we are in
3502 * resume condition. If a resume is needed it is always in the action
3503 * and never in the ACL or converters. In this case, we initialise the
3504 * current rule, and go to the action execution point.
3505 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003506 if (s->current_rule) {
3507 rule = s->current_rule;
3508 s->current_rule = NULL;
3509 if (s->current_rule_list == rules)
3510 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003511 }
3512 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003513
Willy Tarreauff011f22011-01-06 17:51:27 +01003514 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003515
Willy Tarreau96257ec2012-12-27 10:46:37 +01003516 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003517 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003518 int ret;
3519
Willy Tarreau192252e2015-04-04 01:47:55 +02003520 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003521 ret = acl_pass(ret);
3522
Willy Tarreauff011f22011-01-06 17:51:27 +01003523 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003524 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003525
3526 if (!ret) /* condition not matched */
3527 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 }
3529
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003530resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003531 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003532 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003533 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003535 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003536 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003537 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003540 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003541 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003542 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003543
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003544 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003545 /* Auth might be performed on regular http-req rules as well as on stats */
3546 auth_realm = rule->arg.auth.realm;
3547 if (!auth_realm) {
3548 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3549 auth_realm = STATS_DEFAULT_REALM;
3550 else
3551 auth_realm = px->id;
3552 }
3553 /* send 401/407 depending on whether we use a proxy or not. We still
3554 * count one error, because normal browsing won't significantly
3555 * increase the counter but brute force attempts will.
3556 */
3557 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3558 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3559 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003560 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003561 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003563 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003564 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3565 return HTTP_RULE_RES_BADREQ;
3566 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003567
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003568 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003569 s->task->nice = rule->arg.nice;
3570 break;
3571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003572 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003573 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003574 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003575 break;
3576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003577 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003578#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003579 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003580 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003581#endif
3582 break;
3583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003584 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003585 s->logs.level = rule->arg.loglevel;
3586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_REPLACE_HDR:
3589 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003590 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3591 rule->arg.hdr_add.name_len,
3592 &rule->arg.hdr_add.fmt,
3593 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003594 return HTTP_RULE_RES_BADREQ;
3595 break;
3596
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003597 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003598 ctx.idx = 0;
3599 /* remove all occurrences of the header */
3600 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3601 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3602 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003603 }
Willy Tarreau85603282015-01-21 20:39:27 +01003604 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003605
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003606 case ACT_HTTP_SET_HDR:
3607 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003608 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3609 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3610 trash.len = rule->arg.hdr_add.name_len;
3611 trash.str[trash.len++] = ':';
3612 trash.str[trash.len++] = ' ';
3613 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 +01003614
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003615 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003616 /* remove all occurrences of the header */
3617 ctx.idx = 0;
3618 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3619 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3620 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3621 }
3622 }
3623
Willy Tarreau96257ec2012-12-27 10:46:37 +01003624 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3625 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003626
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003627 case ACT_HTTP_DEL_ACL:
3628 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003629 struct pat_ref *ref;
3630 char *key;
3631 int len;
3632
3633 /* collect reference */
3634 ref = pat_ref_lookup(rule->arg.map.ref);
3635 if (!ref)
3636 continue;
3637
3638 /* collect key */
3639 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3640 key = trash.str;
3641 key[len] = '\0';
3642
3643 /* perform update */
3644 /* returned code: 1=ok, 0=ko */
3645 pat_ref_delete(ref, key);
3646
3647 break;
3648 }
3649
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003650 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003651 struct pat_ref *ref;
3652 char *key;
3653 struct chunk *trash_key;
3654 int len;
3655
3656 trash_key = get_trash_chunk();
3657
3658 /* collect reference */
3659 ref = pat_ref_lookup(rule->arg.map.ref);
3660 if (!ref)
3661 continue;
3662
3663 /* collect key */
3664 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3665 key = trash_key->str;
3666 key[len] = '\0';
3667
3668 /* perform update */
3669 /* add entry only if it does not already exist */
3670 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003671 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003672
3673 break;
3674 }
3675
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003676 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003677 struct pat_ref *ref;
3678 char *key, *value;
3679 struct chunk *trash_key, *trash_value;
3680 int len;
3681
3682 trash_key = get_trash_chunk();
3683 trash_value = get_trash_chunk();
3684
3685 /* collect reference */
3686 ref = pat_ref_lookup(rule->arg.map.ref);
3687 if (!ref)
3688 continue;
3689
3690 /* collect key */
3691 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3692 key = trash_key->str;
3693 key[len] = '\0';
3694
3695 /* collect value */
3696 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3697 value = trash_value->str;
3698 value[len] = '\0';
3699
3700 /* perform update */
3701 if (pat_ref_find_elt(ref, key) != NULL)
3702 /* update entry if it exists */
3703 pat_ref_set(ref, key, value, NULL);
3704 else
3705 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003706 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003707
3708 break;
3709 }
William Lallemand73025dd2014-04-24 14:38:37 +02003710
Thierry FOURNIER42148732015-09-02 17:17:33 +02003711 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003712 switch (rule->action_ptr(rule, px, s->sess, s)) {
3713 case ACT_RET_ERR:
3714 case ACT_RET_CONT:
3715 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003716 case ACT_RET_STOP:
3717 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003718 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003719 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003720 return HTTP_RULE_RES_YIELD;
3721 }
William Lallemand73025dd2014-04-24 14:38:37 +02003722 break;
3723
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003724 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003725 /* Note: only the first valid tracking parameter of each
3726 * applies.
3727 */
3728
3729 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3730 struct stktable *t;
3731 struct stksess *ts;
3732 struct stktable_key *key;
3733 void *ptr;
3734
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003735 t = rule->arg.trk_ctr.table.t;
3736 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003737
3738 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003739 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003740
3741 /* let's count a new HTTP request as it's the first time we do it */
3742 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3743 if (ptr)
3744 stktable_data_cast(ptr, http_req_cnt)++;
3745
3746 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3747 if (ptr)
3748 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3749 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3750
3751 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003752 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003753 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3754 }
3755 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003756 break;
3757
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003758 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003759 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3760 struct sample *smp;
3761
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003762 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003763
3764 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003765 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003766 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003767 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003768 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003769 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003771 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003772 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3773 }
3774 }
3775 }
3776 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003777
3778 /* other flags exists, but normaly, they never be matched. */
3779 default:
3780 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003781 }
3782 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003783
3784 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003785 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003786}
3787
Willy Tarreau71241ab2012-12-27 11:30:54 +01003788
Willy Tarreau51d861a2015-05-22 17:30:48 +02003789/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3790 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3791 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3792 * is returned, the process can continue the evaluation of next rule list. If
3793 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3794 * is returned, it means the operation could not be processed and a server error
3795 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3796 * deny rule. If *YIELD is returned, the caller must call again the function
3797 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003798 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003799static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003800http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003802 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003803 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003804 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003805 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003806 struct hdr_ctx ctx;
3807
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003808 /* If "the current_rule_list" match the executed rule list, we are in
3809 * resume condition. If a resume is needed it is always in the action
3810 * and never in the ACL or converters. In this case, we initialise the
3811 * current rule, and go to the action execution point.
3812 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003813 if (s->current_rule) {
3814 rule = s->current_rule;
3815 s->current_rule = NULL;
3816 if (s->current_rule_list == rules)
3817 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003818 }
3819 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003820
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003821 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
3823 /* check optional condition */
3824 if (rule->cond) {
3825 int ret;
3826
Willy Tarreau192252e2015-04-04 01:47:55 +02003827 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003828 ret = acl_pass(ret);
3829
3830 if (rule->cond->pol == ACL_COND_UNLESS)
3831 ret = !ret;
3832
3833 if (!ret) /* condition not matched */
3834 continue;
3835 }
3836
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003837resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003838 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003839 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003840 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003842 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003844 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003847 s->task->nice = rule->arg.nice;
3848 break;
3849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003851 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003852 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003853 break;
3854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003855 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003856#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003857 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003858 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003859#endif
3860 break;
3861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003862 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003863 s->logs.level = rule->arg.loglevel;
3864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_REPLACE_HDR:
3867 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003868 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3869 rule->arg.hdr_add.name_len,
3870 &rule->arg.hdr_add.fmt,
3871 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003872 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003873 break;
3874
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003875 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003876 ctx.idx = 0;
3877 /* remove all occurrences of the header */
3878 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3879 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3880 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3881 }
Willy Tarreau85603282015-01-21 20:39:27 +01003882 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003883
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003884 case ACT_HTTP_SET_HDR:
3885 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003886 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3887 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3888 trash.len = rule->arg.hdr_add.name_len;
3889 trash.str[trash.len++] = ':';
3890 trash.str[trash.len++] = ' ';
3891 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 +01003892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003893 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003894 /* remove all occurrences of the header */
3895 ctx.idx = 0;
3896 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3897 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3898 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3899 }
3900 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003901 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3902 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003904 case ACT_HTTP_DEL_ACL:
3905 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003906 struct pat_ref *ref;
3907 char *key;
3908 int len;
3909
3910 /* collect reference */
3911 ref = pat_ref_lookup(rule->arg.map.ref);
3912 if (!ref)
3913 continue;
3914
3915 /* collect key */
3916 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3917 key = trash.str;
3918 key[len] = '\0';
3919
3920 /* perform update */
3921 /* returned code: 1=ok, 0=ko */
3922 pat_ref_delete(ref, key);
3923
3924 break;
3925 }
3926
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003927 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003928 struct pat_ref *ref;
3929 char *key;
3930 struct chunk *trash_key;
3931 int len;
3932
3933 trash_key = get_trash_chunk();
3934
3935 /* collect reference */
3936 ref = pat_ref_lookup(rule->arg.map.ref);
3937 if (!ref)
3938 continue;
3939
3940 /* collect key */
3941 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3942 key = trash_key->str;
3943 key[len] = '\0';
3944
3945 /* perform update */
3946 /* check if the entry already exists */
3947 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003948 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003949
3950 break;
3951 }
3952
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003953 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003954 struct pat_ref *ref;
3955 char *key, *value;
3956 struct chunk *trash_key, *trash_value;
3957 int len;
3958
3959 trash_key = get_trash_chunk();
3960 trash_value = get_trash_chunk();
3961
3962 /* collect reference */
3963 ref = pat_ref_lookup(rule->arg.map.ref);
3964 if (!ref)
3965 continue;
3966
3967 /* collect key */
3968 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3969 key = trash_key->str;
3970 key[len] = '\0';
3971
3972 /* collect value */
3973 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3974 value = trash_value->str;
3975 value[len] = '\0';
3976
3977 /* perform update */
3978 if (pat_ref_find_elt(ref, key) != NULL)
3979 /* update entry if it exists */
3980 pat_ref_set(ref, key, value, NULL);
3981 else
3982 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003983 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003984
3985 break;
3986 }
William Lallemand73025dd2014-04-24 14:38:37 +02003987
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003988 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003989 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3990 return HTTP_RULE_RES_BADREQ;
3991 return HTTP_RULE_RES_DONE;
3992
Thierry FOURNIER42148732015-09-02 17:17:33 +02003993 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003994 switch (rule->action_ptr(rule, px, s->sess, s)) {
3995 case ACT_RET_ERR:
3996 case ACT_RET_CONT:
3997 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003998 case ACT_RET_STOP:
3999 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004000 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004001 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004002 return HTTP_RULE_RES_YIELD;
4003 }
William Lallemand73025dd2014-04-24 14:38:37 +02004004 break;
4005
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004006 /* other flags exists, but normaly, they never be matched. */
4007 default:
4008 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004009 }
4010 }
4011
4012 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004013 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004014}
4015
4016
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017/* Perform an HTTP redirect based on the information in <rule>. The function
4018 * returns non-zero on success, or zero in case of a, irrecoverable error such
4019 * as too large a request to build a valid response.
4020 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004021static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004022{
Willy Tarreaub329a312015-05-22 16:27:37 +02004023 struct http_msg *req = &txn->req;
4024 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004026 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027
4028 /* build redirect message */
4029 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004030 case 308:
4031 msg_fmt = HTTP_308;
4032 break;
4033 case 307:
4034 msg_fmt = HTTP_307;
4035 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036 case 303:
4037 msg_fmt = HTTP_303;
4038 break;
4039 case 301:
4040 msg_fmt = HTTP_301;
4041 break;
4042 case 302:
4043 default:
4044 msg_fmt = HTTP_302;
4045 break;
4046 }
4047
4048 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4049 return 0;
4050
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004051 location = trash.str + trash.len;
4052
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 switch(rule->type) {
4054 case REDIRECT_TYPE_SCHEME: {
4055 const char *path;
4056 const char *host;
4057 struct hdr_ctx ctx;
4058 int pathlen;
4059 int hostlen;
4060
4061 host = "";
4062 hostlen = 0;
4063 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004064 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065 host = ctx.line + ctx.val;
4066 hostlen = ctx.vlen;
4067 }
4068
4069 path = http_get_path(txn);
4070 /* build message using path */
4071 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004072 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004073 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4074 int qs = 0;
4075 while (qs < pathlen) {
4076 if (path[qs] == '?') {
4077 pathlen = qs;
4078 break;
4079 }
4080 qs++;
4081 }
4082 }
4083 } else {
4084 path = "/";
4085 pathlen = 1;
4086 }
4087
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004088 if (rule->rdr_str) { /* this is an old "redirect" rule */
4089 /* check if we can add scheme + "://" + host + path */
4090 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4091 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004093 /* add scheme */
4094 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4095 trash.len += rule->rdr_len;
4096 }
4097 else {
4098 /* add scheme with executing log format */
4099 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004101 /* check if we can add scheme + "://" + host + path */
4102 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4103 return 0;
4104 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105 /* add "://" */
4106 memcpy(trash.str + trash.len, "://", 3);
4107 trash.len += 3;
4108
4109 /* add host */
4110 memcpy(trash.str + trash.len, host, hostlen);
4111 trash.len += hostlen;
4112
4113 /* add path */
4114 memcpy(trash.str + trash.len, path, pathlen);
4115 trash.len += pathlen;
4116
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004117 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 if (trash.len && trash.str[trash.len - 1] != '/' &&
4119 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4120 if (trash.len > trash.size - 5)
4121 return 0;
4122 trash.str[trash.len] = '/';
4123 trash.len++;
4124 }
4125
4126 break;
4127 }
4128 case REDIRECT_TYPE_PREFIX: {
4129 const char *path;
4130 int pathlen;
4131
4132 path = http_get_path(txn);
4133 /* build message using path */
4134 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004135 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004136 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4137 int qs = 0;
4138 while (qs < pathlen) {
4139 if (path[qs] == '?') {
4140 pathlen = qs;
4141 break;
4142 }
4143 qs++;
4144 }
4145 }
4146 } else {
4147 path = "/";
4148 pathlen = 1;
4149 }
4150
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004151 if (rule->rdr_str) { /* this is an old "redirect" rule */
4152 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4153 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004155 /* add prefix. Note that if prefix == "/", we don't want to
4156 * add anything, otherwise it makes it hard for the user to
4157 * configure a self-redirection.
4158 */
4159 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4160 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4161 trash.len += rule->rdr_len;
4162 }
4163 }
4164 else {
4165 /* add prefix with executing log format */
4166 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4167
4168 /* Check length */
4169 if (trash.len + pathlen > trash.size - 4)
4170 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004171 }
4172
4173 /* add path */
4174 memcpy(trash.str + trash.len, path, pathlen);
4175 trash.len += pathlen;
4176
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004177 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004178 if (trash.len && trash.str[trash.len - 1] != '/' &&
4179 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4180 if (trash.len > trash.size - 5)
4181 return 0;
4182 trash.str[trash.len] = '/';
4183 trash.len++;
4184 }
4185
4186 break;
4187 }
4188 case REDIRECT_TYPE_LOCATION:
4189 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 if (rule->rdr_str) { /* this is an old "redirect" rule */
4191 if (trash.len + rule->rdr_len > trash.size - 4)
4192 return 0;
4193
4194 /* add location */
4195 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4196 trash.len += rule->rdr_len;
4197 }
4198 else {
4199 /* add location with executing log format */
4200 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004201
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004202 /* Check left length */
4203 if (trash.len > trash.size - 4)
4204 return 0;
4205 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 break;
4207 }
4208
4209 if (rule->cookie_len) {
4210 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4211 trash.len += 14;
4212 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4213 trash.len += rule->cookie_len;
4214 memcpy(trash.str + trash.len, "\r\n", 2);
4215 trash.len += 2;
4216 }
4217
4218 /* add end of headers and the keep-alive/close status.
4219 * We may choose to set keep-alive if the Location begins
4220 * with a slash, because the client will come back to the
4221 * same server.
4222 */
4223 txn->status = rule->code;
4224 /* let's log the request time */
4225 s->logs.tv_request = now;
4226
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004227 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004228 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004229 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004230 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4231 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4232 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004233 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004234 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4235 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4236 trash.len += 30;
4237 } else {
4238 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4239 trash.len += 24;
4240 }
4241 }
4242 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4243 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004244 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004245 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 bi_fast_delete(req->chn->buf, req->sov);
4247 req->next -= req->sov;
4248 req->sov = 0;
4249 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004250 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004251 req->msg_state = HTTP_MSG_CLOSED;
4252 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004253 /* Trim any possible response */
4254 res->chn->buf->i = 0;
4255 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004256 } else {
4257 /* keep-alive not possible */
4258 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4259 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4260 trash.len += 29;
4261 } else {
4262 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4263 trash.len += 23;
4264 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004265 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004266 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004267 }
4268
Willy Tarreaue7dff022015-04-03 01:14:29 +02004269 if (!(s->flags & SF_ERR_MASK))
4270 s->flags |= SF_ERR_LOCAL;
4271 if (!(s->flags & SF_FINST_MASK))
4272 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004273
4274 return 1;
4275}
4276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004277/* This stream analyser runs all HTTP request processing which is common to
4278 * frontends and backends, which means blocking ACLs, filters, connection-close,
4279 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004280 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004281 * either needs more data or wants to immediately abort the request (eg: deny,
4282 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004283 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004284int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004285{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004286 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004287 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004288 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004289 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004290 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004291 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004292
Willy Tarreau655dce92009-11-08 13:10:58 +01004293 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004294 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004295 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004296 }
4297
Willy Tarreau87b09662015-04-03 00:22:06 +02004298 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004299 now_ms, __FUNCTION__,
4300 s,
4301 req,
4302 req->rex, req->wex,
4303 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004304 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004305 req->analysers);
4306
Willy Tarreau65410832014-04-28 21:25:43 +02004307 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004308 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004309
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004310 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004311 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004312 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004313
Willy Tarreau0b748332014-04-29 00:13:29 +02004314 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004315 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4316 goto return_prx_yield;
4317
Willy Tarreau0b748332014-04-29 00:13:29 +02004318 case HTTP_RULE_RES_CONT:
4319 case HTTP_RULE_RES_STOP: /* nothing to do */
4320 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004321
Willy Tarreau0b748332014-04-29 00:13:29 +02004322 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4323 if (txn->flags & TX_CLTARPIT)
4324 goto tarpit;
4325 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4328 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004329
Willy Tarreau0b748332014-04-29 00:13:29 +02004330 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004331 goto done;
4332
Willy Tarreau0b748332014-04-29 00:13:29 +02004333 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4334 goto return_bad_req;
4335 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004336 }
4337
Willy Tarreau52542592014-04-28 18:33:26 +02004338 /* OK at this stage, we know that the request was accepted according to
4339 * the http-request rules, we can check for the stats. Note that the
4340 * URI is detected *before* the req* rules in order not to be affected
4341 * by a possible reqrep, while they are processed *after* so that a
4342 * reqdeny can still block them. This clearly needs to change in 1.6!
4343 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004344 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004345 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004346 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004347 txn->status = 500;
4348 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004349 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004350
Willy Tarreaue7dff022015-04-03 01:14:29 +02004351 if (!(s->flags & SF_ERR_MASK))
4352 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004353 goto return_prx_cond;
4354 }
4355
4356 /* parse the whole stats request and extract the relevant information */
4357 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004358 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004359 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004360
Willy Tarreau0b748332014-04-29 00:13:29 +02004361 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4362 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004363
Willy Tarreau0b748332014-04-29 00:13:29 +02004364 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4365 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004366 }
4367
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004368 /* evaluate the req* rules except reqadd */
4369 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004370 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004371 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004372
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373 if (txn->flags & TX_CLDENY)
4374 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004375
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004376 if (txn->flags & TX_CLTARPIT)
4377 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004378 }
Willy Tarreau06619262006-12-17 08:37:22 +01004379
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004380 /* add request headers from the rule sets in the same order */
4381 list_for_each_entry(wl, &px->req_add, list) {
4382 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004383 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004384 ret = acl_pass(ret);
4385 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4386 ret = !ret;
4387 if (!ret)
4388 continue;
4389 }
4390
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004391 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004392 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004393 }
4394
Willy Tarreau52542592014-04-28 18:33:26 +02004395
4396 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004397 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004398 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004399 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4400 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004401
Willy Tarreaue7dff022015-04-03 01:14:29 +02004402 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4403 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4404 if (!(s->flags & SF_FINST_MASK))
4405 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004406
Willy Tarreau70730dd2014-04-24 18:06:27 +02004407 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004409 select_compression_request_header(s, req->buf);
4410
4411 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004412 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004413 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004414 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004416 /* check whether we have some ACLs set to redirect this request */
4417 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004418 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004419 int ret;
4420
Willy Tarreau192252e2015-04-04 01:47:55 +02004421 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004422 ret = acl_pass(ret);
4423 if (rule->cond->pol == ACL_COND_UNLESS)
4424 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004425 if (!ret)
4426 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004427 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004428 if (!http_apply_redirect_rule(rule, s, txn))
4429 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004430 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004431 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004432
Willy Tarreau2be39392010-01-03 17:24:51 +01004433 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4434 * If this happens, then the data will not come immediately, so we must
4435 * send all what we have without waiting. Note that due to the small gain
4436 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004437 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004438 * itself once used.
4439 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004440 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004441
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004442 done: /* done with this analyser, continue with next ones that the calling
4443 * points will have set, if any.
4444 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004445 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004446 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4447 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004448 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004449
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004450 tarpit:
4451 /* When a connection is tarpitted, we use the tarpit timeout,
4452 * which may be the same as the connect timeout if unspecified.
4453 * If unset, then set it to zero because we really want it to
4454 * eventually expire. We build the tarpit as an analyser.
4455 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004456 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004457
4458 /* wipe the request out so that we can drop the connection early
4459 * if the client closes first.
4460 */
4461 channel_dont_connect(req);
4462 req->analysers = 0; /* remove switching rules etc... */
4463 req->analysers |= AN_REQ_HTTP_TARPIT;
4464 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4465 if (!req->analyse_exp)
4466 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004467 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004468 sess->fe->fe_counters.denied_req++;
4469 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004471 if (sess->listener->counters)
4472 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004473 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004474
4475 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004476 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004477 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004478 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004479 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004480 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 sess->fe->fe_counters.denied_req++;
4482 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004484 if (sess->listener->counters)
4485 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004486 goto return_prx_cond;
4487
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004488 return_bad_req:
4489 /* We centralize bad requests processing here */
4490 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4491 /* we detected a parsing error. We want to archive this request
4492 * in the dedicated proxy area for later troubleshooting.
4493 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004494 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004495 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004496
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004497 txn->req.msg_state = HTTP_MSG_ERROR;
4498 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004499 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004500
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004502 if (sess->listener->counters)
4503 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004504
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004505 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004506 if (!(s->flags & SF_ERR_MASK))
4507 s->flags |= SF_ERR_PRXCOND;
4508 if (!(s->flags & SF_FINST_MASK))
4509 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004510
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004511 req->analysers = 0;
4512 req->analyse_exp = TICK_ETERNITY;
4513 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004514
4515 return_prx_yield:
4516 channel_dont_connect(req);
4517 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004518}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004520/* This function performs all the processing enabled for the current request.
4521 * It returns 1 if the processing can continue on next analysers, or zero if it
4522 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004523 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004525int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004526{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004527 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004528 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004529 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004530 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004531
Willy Tarreau655dce92009-11-08 13:10:58 +01004532 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004533 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004534 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004535 return 0;
4536 }
4537
Willy Tarreau87b09662015-04-03 00:22:06 +02004538 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539 now_ms, __FUNCTION__,
4540 s,
4541 req,
4542 req->rex, req->wex,
4543 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004544 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004547 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004548 select_compression_request_header(s, req->buf);
4549
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 /*
4551 * Right now, we know that we have processed the entire headers
4552 * and that unwanted requests have been filtered out. We can do
4553 * whatever we want with the remaining request. Also, now we
4554 * may have separate values for ->fe, ->be.
4555 */
Willy Tarreau06619262006-12-17 08:37:22 +01004556
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004558 * If HTTP PROXY is set we simply get remote server address parsing
4559 * incoming request. Note that this requires that a connection is
4560 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004561 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004562 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004563 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004564 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004565
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004566 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004567 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004568 txn->req.msg_state = HTTP_MSG_ERROR;
4569 txn->status = 500;
4570 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004571 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004572
Willy Tarreaue7dff022015-04-03 01:14:29 +02004573 if (!(s->flags & SF_ERR_MASK))
4574 s->flags |= SF_ERR_RESOURCE;
4575 if (!(s->flags & SF_FINST_MASK))
4576 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004577
4578 return 0;
4579 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004580
4581 path = http_get_path(txn);
4582 url2sa(req->buf->p + msg->sl.rq.u,
4583 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004584 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004585 /* if the path was found, we have to remove everything between
4586 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4587 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4588 * u_l characters by a single "/".
4589 */
4590 if (path) {
4591 char *cur_ptr = req->buf->p;
4592 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4593 int delta;
4594
4595 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4596 http_msg_move_end(&txn->req, delta);
4597 cur_end += delta;
4598 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4599 goto return_bad_req;
4600 }
4601 else {
4602 char *cur_ptr = req->buf->p;
4603 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4604 int delta;
4605
4606 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4607 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4608 http_msg_move_end(&txn->req, delta);
4609 cur_end += delta;
4610 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4611 goto return_bad_req;
4612 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614
Willy Tarreau59234e92008-11-30 23:51:27 +01004615 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004616 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 * Note that doing so might move headers in the request, but
4618 * the fields will stay coherent and the URI will not move.
4619 * This should only be performed in the backend.
4620 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004621 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004622 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4623 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004624
William Lallemanda73203e2012-03-12 12:48:57 +01004625 /* add unique-id if "header-unique-id" is specified */
4626
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004627 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004628 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4629 goto return_bad_req;
4630 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004632 }
William Lallemanda73203e2012-03-12 12:48:57 +01004633
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004634 if (sess->fe->header_unique_id && s->unique_id) {
4635 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004636 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004637 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004638 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004639 goto return_bad_req;
4640 }
4641
Cyril Bontéb21570a2009-11-29 20:04:48 +01004642 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 * 9: add X-Forwarded-For if either the frontend or the backend
4644 * asks for it.
4645 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004646 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004647 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4649 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4650 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004651 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004652 /* The header is set to be added only if none is present
4653 * and we found it, so don't do anything.
4654 */
4655 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004656 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004657 /* Add an X-Forwarded-For header unless the source IP is
4658 * in the 'except' network range.
4659 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004660 if ((!sess->fe->except_mask.s_addr ||
4661 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4662 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004664 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004666 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004669
4670 /* Note: we rely on the backend to get the header name to be used for
4671 * x-forwarded-for, because the header is really meant for the backends.
4672 * However, if the backend did not specify any option, we have to rely
4673 * on the frontend's header name.
4674 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004675 if (s->be->fwdfor_hdr_len) {
4676 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004677 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004678 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 len = sess->fe->fwdfor_hdr_len;
4680 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004681 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004682 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 +01004683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004685 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004686 }
4687 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004689 /* FIXME: for the sake of completeness, we should also support
4690 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004691 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 int len;
4693 char pn[INET6_ADDRSTRLEN];
4694 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004695 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004696 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004697
Willy Tarreau59234e92008-11-30 23:51:27 +01004698 /* Note: we rely on the backend to get the header name to be used for
4699 * x-forwarded-for, because the header is really meant for the backends.
4700 * However, if the backend did not specify any option, we have to rely
4701 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004702 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004703 if (s->be->fwdfor_hdr_len) {
4704 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004705 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004707 len = sess->fe->fwdfor_hdr_len;
4708 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004709 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004710 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004712 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 goto return_bad_req;
4714 }
4715 }
4716
4717 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004718 * 10: add X-Original-To if either the frontend or the backend
4719 * asks for it.
4720 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004721 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004722
4723 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004724 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004725 /* Add an X-Original-To header unless the destination IP is
4726 * in the 'except' network range.
4727 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004728 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004730 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004731 ((!sess->fe->except_mask_to.s_addr ||
4732 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4733 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004734 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004735 (((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 +02004736 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004737 int len;
4738 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004740
4741 /* Note: we rely on the backend to get the header name to be used for
4742 * x-original-to, because the header is really meant for the backends.
4743 * However, if the backend did not specify any option, we have to rely
4744 * on the frontend's header name.
4745 */
4746 if (s->be->orgto_hdr_len) {
4747 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004748 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004749 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 len = sess->fe->orgto_hdr_len;
4751 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004752 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004753 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 +02004754
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004755 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004756 goto return_bad_req;
4757 }
4758 }
4759 }
4760
Willy Tarreau50fc7772012-11-11 22:19:57 +01004761 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4762 * If an "Upgrade" token is found, the header is left untouched in order not to have
4763 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4764 * "Upgrade" is present in the Connection header.
4765 */
4766 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4767 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004768 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004769 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004770 unsigned int want_flags = 0;
4771
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004772 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004773 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004774 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004775 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004777 want_flags |= TX_CON_CLO_SET;
4778 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004779 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004780 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004781 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004782 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 want_flags |= TX_CON_KAL_SET;
4784 }
4785
4786 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004787 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004788 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004789
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 /* If we have no server assigned yet and we're balancing on url_param
4792 * with a POST request, we may be interested in checking the body for
4793 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004794 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004795 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004796 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004798 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004799 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004801
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004802 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004803 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004804#ifdef TCP_QUICKACK
4805 /* We expect some data from the client. Unless we know for sure
4806 * we already have a full request, we have to re-enable quick-ack
4807 * in case we previously disabled it, otherwise we might cause
4808 * the client to delay further data.
4809 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004810 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004811 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004812 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004813 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004814 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004815#endif
4816 }
Willy Tarreau03945942009-12-22 16:50:27 +01004817
Willy Tarreau59234e92008-11-30 23:51:27 +01004818 /*************************************************************
4819 * OK, that's finished for the headers. We have done what we *
4820 * could. Let's switch to the DATA state. *
4821 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004822 req->analyse_exp = TICK_ETERNITY;
4823 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004824
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004825 /* if the server closes the connection, we want to immediately react
4826 * and close the socket to save packets and syscalls.
4827 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004828 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004829 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004830
Willy Tarreau59234e92008-11-30 23:51:27 +01004831 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004832 /* OK let's go on with the BODY now */
4833 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004834
Willy Tarreau59234e92008-11-30 23:51:27 +01004835 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004836 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004837 /* we detected a parsing error. We want to archive this request
4838 * in the dedicated proxy area for later troubleshooting.
4839 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004840 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004841 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004842
Willy Tarreau59234e92008-11-30 23:51:27 +01004843 txn->req.msg_state = HTTP_MSG_ERROR;
4844 txn->status = 400;
4845 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004846 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004847
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004848 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004849 if (sess->listener->counters)
4850 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004851
Willy Tarreaue7dff022015-04-03 01:14:29 +02004852 if (!(s->flags & SF_ERR_MASK))
4853 s->flags |= SF_ERR_PRXCOND;
4854 if (!(s->flags & SF_FINST_MASK))
4855 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004856 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004857}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004858
Willy Tarreau60b85b02008-11-30 23:28:40 +01004859/* This function is an analyser which processes the HTTP tarpit. It always
4860 * returns zero, at the beginning because it prevents any other processing
4861 * from occurring, and at the end because it terminates the request.
4862 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004863int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004864{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004865 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004866
4867 /* This connection is being tarpitted. The CLIENT side has
4868 * already set the connect expiration date to the right
4869 * timeout. We just have to check that the client is still
4870 * there and that the timeout has not expired.
4871 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004872 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004873 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004874 !tick_is_expired(req->analyse_exp, now_ms))
4875 return 0;
4876
4877 /* We will set the queue timer to the time spent, just for
4878 * logging purposes. We fake a 500 server error, so that the
4879 * attacker will not suspect his connection has been tarpitted.
4880 * It will not cause trouble to the logs because we can exclude
4881 * the tarpitted connections by filtering on the 'PT' status flags.
4882 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4884
4885 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004887 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004888
4889 req->analysers = 0;
4890 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004891
Willy Tarreaue7dff022015-04-03 01:14:29 +02004892 if (!(s->flags & SF_ERR_MASK))
4893 s->flags |= SF_ERR_PRXCOND;
4894 if (!(s->flags & SF_FINST_MASK))
4895 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004896 return 0;
4897}
4898
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004899/* This function is an analyser which waits for the HTTP request body. It waits
4900 * for either the buffer to be full, or the full advertised contents to have
4901 * reached the buffer. It must only be called after the standard HTTP request
4902 * processing has occurred, because it expects the request to be parsed and will
4903 * look for the Expect header. It may send a 100-Continue interim response. It
4904 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4905 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4906 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004907 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004908int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004909{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004910 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004911 struct http_txn *txn = s->txn;
4912 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004913
4914 /* We have to parse the HTTP request body to find any required data.
4915 * "balance url_param check_post" should have been the only way to get
4916 * into this. We were brought here after HTTP header analysis, so all
4917 * related structures are ready.
4918 */
4919
Willy Tarreau890988f2014-04-10 11:59:33 +02004920 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4921 /* This is the first call */
4922 if (msg->msg_state < HTTP_MSG_BODY)
4923 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004924
Willy Tarreau890988f2014-04-10 11:59:33 +02004925 if (msg->msg_state < HTTP_MSG_100_SENT) {
4926 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4927 * send an HTTP/1.1 100 Continue intermediate response.
4928 */
4929 if (msg->flags & HTTP_MSGF_VER_11) {
4930 struct hdr_ctx ctx;
4931 ctx.idx = 0;
4932 /* Expect is allowed in 1.1, look for it */
4933 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4934 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004935 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004936 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004939 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004940
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004941 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004942 * req->buf->p still points to the beginning of the message. We
4943 * must save the body in msg->next because it survives buffer
4944 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004946 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004947
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004948 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004949 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4950 else
4951 msg->msg_state = HTTP_MSG_DATA;
4952 }
4953
Willy Tarreau890988f2014-04-10 11:59:33 +02004954 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4955 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004956 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 goto missing_data;
4958
4959 /* OK we have everything we need now */
4960 goto http_end;
4961 }
4962
4963 /* OK here we're parsing a chunked-encoded message */
4964
Willy Tarreau522d6c02009-12-06 18:49:18 +01004965 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004966 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004967 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004968 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004969 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004970 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004971
Willy Tarreau115acb92009-12-26 13:56:06 +01004972 if (!ret)
4973 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004974 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004975 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004976 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004977 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004978 }
4979
Willy Tarreaud98cf932009-12-27 22:54:55 +01004980 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004981 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4982 * for at least a whole chunk or the whole content length bytes after
4983 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004984 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004985 if (msg->msg_state == HTTP_MSG_TRAILERS)
4986 goto http_end;
4987
Willy Tarreaue115b492015-05-01 23:05:14 +02004988 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto http_end;
4990
4991 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004992 /* we get here if we need to wait for more data. If the buffer is full,
4993 * we have the maximum we can expect.
4994 */
4995 if (buffer_full(req->buf, global.tune.maxrewrite))
4996 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004997
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004998 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004999 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005000 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005001
Willy Tarreaue7dff022015-04-03 01:14:29 +02005002 if (!(s->flags & SF_ERR_MASK))
5003 s->flags |= SF_ERR_CLITO;
5004 if (!(s->flags & SF_FINST_MASK))
5005 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005006 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005007 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005008
5009 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005010 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005011 /* Not enough data. We'll re-use the http-request
5012 * timeout here. Ideally, we should set the timeout
5013 * relative to the accept() date. We just set the
5014 * request timeout once at the beginning of the
5015 * request.
5016 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005017 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005018 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005019 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005020 return 0;
5021 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005022
5023 http_end:
5024 /* The situation will not evolve, so let's give up on the analysis. */
5025 s->logs.tv_request = now; /* update the request timer to reflect full request */
5026 req->analysers &= ~an_bit;
5027 req->analyse_exp = TICK_ETERNITY;
5028 return 1;
5029
5030 return_bad_req: /* let's centralize all bad requests */
5031 txn->req.msg_state = HTTP_MSG_ERROR;
5032 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005033 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005034
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 if (!(s->flags & SF_ERR_MASK))
5036 s->flags |= SF_ERR_PRXCOND;
5037 if (!(s->flags & SF_FINST_MASK))
5038 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005039
Willy Tarreau522d6c02009-12-06 18:49:18 +01005040 return_err_msg:
5041 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005042 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005043 if (sess->listener->counters)
5044 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005045 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005046}
5047
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005048/* send a server's name with an outgoing request over an established connection.
5049 * Note: this function is designed to be called once the request has been scheduled
5050 * for being forwarded. This is the reason why it rewinds the buffer before
5051 * proceeding.
5052 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005053int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005054
5055 struct hdr_ctx ctx;
5056
Mark Lamourinec2247f02012-01-04 13:02:01 -05005057 char *hdr_name = be->server_id_hdr_name;
5058 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005059 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005060 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005062
William Lallemandd9e90662012-01-30 17:27:17 +01005063 ctx.idx = 0;
5064
Willy Tarreau211cdec2014-04-17 20:18:08 +02005065 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005066 if (old_o) {
5067 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005068 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005069 txn->req.next += old_o;
5070 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005071 }
5072
Willy Tarreau9b28e032012-10-12 23:49:43 +02005073 old_i = chn->buf->i;
5074 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 -05005075 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005076 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005077 }
5078
5079 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081 memcpy(hdr_val, hdr_name, hdr_name_len);
5082 hdr_val += hdr_name_len;
5083 *hdr_val++ = ':';
5084 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005085 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5086 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005087
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005088 if (old_o) {
5089 /* If this was a forwarded request, we must readjust the amount of
5090 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005091 * variations. Note that the current state is >= HTTP_MSG_BODY,
5092 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005093 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005094 old_o += chn->buf->i - old_i;
5095 b_adv(chn->buf, old_o);
5096 txn->req.next -= old_o;
5097 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005098 }
5099
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100 return 0;
5101}
5102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103/* Terminate current transaction and prepare a new one. This is very tricky
5104 * right now but it works.
5105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005106void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005108 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005109 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005110 struct connection *srv_conn;
5111 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005112 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005113
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 /* FIXME: We need a more portable way of releasing a backend's and a
5115 * server's connections. We need a safer way to reinitialize buffer
5116 * flags. We also need a more accurate method for computing per-request
5117 * data.
5118 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005119 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120
Willy Tarreau4213a112013-12-15 10:25:42 +01005121 /* unless we're doing keep-alive, we want to quickly close the connection
5122 * to the server.
5123 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005124 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005125 !si_conn_ready(&s->si[1])) {
5126 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5127 si_shutr(&s->si[1]);
5128 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130
Willy Tarreaue7dff022015-04-03 01:14:29 +02005131 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005132 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005133 if (unlikely(s->srv_conn))
5134 sess_change_server(s, NULL);
5135 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136
5137 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005138 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139
Willy Tarreaueee5b512015-04-03 23:46:31 +02005140 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 int n;
5142
Willy Tarreaueee5b512015-04-03 23:46:31 +02005143 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 if (n < 1 || n > 5)
5145 n = 0;
5146
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005147 if (fe->mode == PR_MODE_HTTP) {
5148 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005149 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005150 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005151 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005152 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005153 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005154 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005155 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005156 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005157 s->be->be_counters.p.http.comp_rsp++;
5158 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 }
5160
5161 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005162 s->logs.bytes_in -= s->req.buf->i;
5163 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164
5165 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005166 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005167 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005168 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169 s->do_log(s);
5170 }
5171
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005172 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005173 stream_stop_content_counters(s);
5174 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005175
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 s->logs.accept_date = date; /* user-visible date for logging */
5177 s->logs.tv_accept = now; /* corrected date for internal use */
5178 tv_zero(&s->logs.tv_request);
5179 s->logs.t_queue = -1;
5180 s->logs.t_connect = -1;
5181 s->logs.t_data = -1;
5182 s->logs.t_close = 0;
5183 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5184 s->logs.srv_queue_size = 0; /* we will get this number soon */
5185
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005186 s->logs.bytes_in = s->req.total = s->req.buf->i;
5187 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188
5189 if (s->pend_pos)
5190 pendconn_free(s->pend_pos);
5191
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005192 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005193 if (s->flags & SF_CURR_SESS) {
5194 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (may_dequeue_tasks(objt_server(s->target), s->be))
5198 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 }
5200
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005201 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202
Willy Tarreau4213a112013-12-15 10:25:42 +01005203 /* only release our endpoint if we don't intend to reuse the
5204 * connection.
5205 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005206 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005207 !si_conn_ready(&s->si[1])) {
5208 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005209 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005210 }
5211
Willy Tarreau350f4872014-11-28 14:42:25 +01005212 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5213 s->si[1].err_type = SI_ET_NONE;
5214 s->si[1].conn_retries = 0; /* used for logging too */
5215 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005216 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005217 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);
5218 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005219 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5220 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5221 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005222
Willy Tarreaueee5b512015-04-03 23:46:31 +02005223 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005225 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005226
5227 if (prev_status == 401 || prev_status == 407) {
5228 /* In HTTP keep-alive mode, if we receive a 401, we still have
5229 * a chance of being able to send the visitor again to the same
5230 * server over the same connection. This is required by some
5231 * broken protocols such as NTLM, and anyway whenever there is
5232 * an opportunity for sending the challenge to the proper place,
5233 * it's better to do it (at least it helps with debugging).
5234 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005235 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005236 if (srv_conn)
5237 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005238 }
5239
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005240 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005241 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005243 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005244 s->req.flags |= CF_NEVER_WAIT;
5245 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005246 }
5247
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 /* if the request buffer is not empty, it means we're
5249 * about to process another request, so send pending
5250 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005251 * Just don't do this if the buffer is close to be full,
5252 * because the request will wait for it to flush a little
5253 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005255 if (s->req.buf->i) {
5256 if (s->res.buf->o &&
5257 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5258 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5259 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005260 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005261
5262 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005263 channel_auto_read(&s->req);
5264 channel_auto_close(&s->req);
5265 channel_auto_read(&s->res);
5266 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267
Willy Tarreau27375622013-12-17 00:00:28 +01005268 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005269 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005270 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005271 if (!srv)
5272 si_idle_conn(&s->si[1], NULL);
5273 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5274 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5275 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005276 else if (prev_flags & TX_NOT_FIRST)
5277 /* note: we check the request, not the connection, but
5278 * this is valid for strategies SAFE and AGGR, and in
5279 * case of ALWS, we don't care anyway.
5280 */
5281 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005282 else
5283 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005284 }
Willy Tarreau27375622013-12-17 00:00:28 +01005285
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005286 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005287 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288}
5289
5290
5291/* This function updates the request state machine according to the response
5292 * state machine and buffer flags. It returns 1 if it changes anything (flag
5293 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5294 * it is only used to find when a request/response couple is complete. Both
5295 * this function and its equivalent should loop until both return zero. It
5296 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005298int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005301 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005302 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303 unsigned int old_state = txn->req.msg_state;
5304
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5306 return 0;
5307
5308 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005309 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005310 * We can shut the read side unless we want to abort_on_close,
5311 * or we have a POST request. The issue with POST requests is
5312 * that some browsers still send a CRLF after the request, and
5313 * this CRLF must be read so that it does not remain in the kernel
5314 * buffers, otherwise a close could cause an RST on some systems
5315 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005316 * Note that if we're using keep-alive on the client side, we'd
5317 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005318 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005319 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005320 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005321 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5322 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5323 !(s->be->options & PR_O_ABRT_CLOSE) &&
5324 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005325 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326
Willy Tarreau40f151a2012-12-20 12:10:09 +01005327 /* if the server closes the connection, we want to immediately react
5328 * and close the socket to save packets and syscalls.
5329 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005330 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005331
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5333 goto wait_other_side;
5334
5335 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5336 /* The server has not finished to respond, so we
5337 * don't want to move in order not to upset it.
5338 */
5339 goto wait_other_side;
5340 }
5341
5342 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5343 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005344 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005346 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 goto wait_other_side;
5348 }
5349
5350 /* When we get here, it means that both the request and the
5351 * response have finished receiving. Depending on the connection
5352 * mode, we'll have to wait for the last bytes to leave in either
5353 * direction, and sometimes for a close to be effective.
5354 */
5355
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005356 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5357 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005358 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5359 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005360 }
5361 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5362 /* Option forceclose is set, or either side wants to close,
5363 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005364 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005365 * once both states are CLOSED.
5366 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5368 channel_shutr_now(chn);
5369 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005371 }
5372 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005373 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5374 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005375 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005376 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5377 channel_auto_read(chn);
5378 txn->req.msg_state = HTTP_MSG_TUNNEL;
5379 chn->flags |= CF_NEVER_WAIT;
5380 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 }
5382
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005383 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005384 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005385 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005386
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005387 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 txn->req.msg_state = HTTP_MSG_CLOSING;
5389 goto http_msg_closing;
5390 }
5391 else {
5392 txn->req.msg_state = HTTP_MSG_CLOSED;
5393 goto http_msg_closed;
5394 }
5395 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 }
5398
5399 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5400 http_msg_closing:
5401 /* nothing else to forward, just waiting for the output buffer
5402 * to be empty and for the shutw_now to take effect.
5403 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005404 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 txn->req.msg_state = HTTP_MSG_CLOSED;
5406 goto http_msg_closed;
5407 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_ERROR;
5410 goto wait_other_side;
5411 }
5412 }
5413
5414 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5415 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005416 /* see above in MSG_DONE why we only do this in these states */
5417 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5418 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5419 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005420 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 goto wait_other_side;
5422 }
5423
5424 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426}
5427
5428
5429/* This function updates the response state machine according to the request
5430 * state machine and buffer flags. It returns 1 if it changes anything (flag
5431 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5432 * it is only used to find when a request/response couple is complete. Both
5433 * this function and its equivalent should loop until both return zero. It
5434 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005436int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005438 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005439 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005440 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 unsigned int old_state = txn->rsp.msg_state;
5442
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5444 return 0;
5445
5446 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5447 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005448 * still monitor the server connection for a possible close
5449 * while the request is being uploaded, so we don't disable
5450 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453
5454 if (txn->req.msg_state == HTTP_MSG_ERROR)
5455 goto wait_other_side;
5456
5457 if (txn->req.msg_state < HTTP_MSG_DONE) {
5458 /* The client seems to still be sending data, probably
5459 * because we got an error response during an upload.
5460 * We have the choice of either breaking the connection
5461 * or letting it pass through. Let's do the later.
5462 */
5463 goto wait_other_side;
5464 }
5465
5466 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5467 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005470 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 goto wait_other_side;
5472 }
5473
5474 /* When we get here, it means that both the request and the
5475 * response have finished receiving. Depending on the connection
5476 * mode, we'll have to wait for the last bytes to leave in either
5477 * direction, and sometimes for a close to be effective.
5478 */
5479
5480 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5481 /* Server-close mode : shut read and wait for the request
5482 * side to close its output buffer. The caller will detect
5483 * when we're in DONE and the other is in CLOSED and will
5484 * catch that for the final cleanup.
5485 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005486 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5487 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005489 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5490 /* Option forceclose is set, or either side wants to close,
5491 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005492 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005493 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005494 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005495 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5496 channel_shutr_now(chn);
5497 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005498 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 }
5500 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005501 /* The last possible modes are keep-alive and tunnel. Tunnel will
5502 * need to forward remaining data. Keep-alive will need to monitor
5503 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005505 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005506 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005507 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 }
5510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005511 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005513 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5515 goto http_msg_closing;
5516 }
5517 else {
5518 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5519 goto http_msg_closed;
5520 }
5521 }
5522 goto wait_other_side;
5523 }
5524
5525 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5526 http_msg_closing:
5527 /* nothing else to forward, just waiting for the output buffer
5528 * to be empty and for the shutw_now to take effect.
5529 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5532 goto http_msg_closed;
5533 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005536 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005537 if (objt_server(s->target))
5538 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 goto wait_other_side;
5540 }
5541 }
5542
5543 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5544 http_msg_closed:
5545 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005546 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005547 channel_auto_close(chn);
5548 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549 goto wait_other_side;
5550 }
5551
5552 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005553 /* We force the response to leave immediately if we're waiting for the
5554 * other side, since there is no pending shutdown to push it out.
5555 */
5556 if (!channel_is_empty(chn))
5557 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005558 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559}
5560
5561
5562/* Resync the request and response state machines. Return 1 if either state
5563 * changes.
5564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005565int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005567 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005568 int old_req_state = txn->req.msg_state;
5569 int old_res_state = txn->rsp.msg_state;
5570
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 http_sync_req_state(s);
5572 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 if (!http_sync_res_state(s))
5574 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 if (!http_sync_req_state(s))
5576 break;
5577 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005578
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 /* OK, both state machines agree on a compatible state.
5580 * There are a few cases we're interested in :
5581 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5582 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5583 * directions, so let's simply disable both analysers.
5584 * - HTTP_MSG_CLOSED on the response only means we must abort the
5585 * request.
5586 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5587 * with server-close mode means we've completed one request and we
5588 * must re-initialize the server connection.
5589 */
5590
5591 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5592 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5593 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5594 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005595 s->req.analysers = 0;
5596 channel_auto_close(&s->req);
5597 channel_auto_read(&s->req);
5598 s->res.analysers = 0;
5599 channel_auto_close(&s->res);
5600 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005602 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005603 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005604 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005605 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005606 s->res.analysers = 0;
5607 channel_auto_close(&s->res);
5608 channel_auto_read(&s->res);
5609 s->req.analysers = 0;
5610 channel_abort(&s->req);
5611 channel_auto_close(&s->req);
5612 channel_auto_read(&s->req);
5613 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005614 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005615 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5616 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005617 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005618 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5619 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5620 /* server-close/keep-alive: terminate this transaction,
5621 * possibly killing the server connection and reinitialize
5622 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005623 */
5624 http_end_txn_clean_session(s);
5625 }
5626
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 return txn->req.msg_state != old_req_state ||
5628 txn->rsp.msg_state != old_res_state;
5629}
5630
Willy Tarreaud98cf932009-12-27 22:54:55 +01005631/* This function is an analyser which forwards request body (including chunk
5632 * sizes if any). It is called as soon as we must forward, even if we forward
5633 * zero byte. The only situation where it must not be called is when we're in
5634 * tunnel mode and we want to forward till the close. It's used both to forward
5635 * remaining data and to resync after end of body. It expects the msg_state to
5636 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005637 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005638 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005639 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005640 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005641int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005642{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005643 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005644 struct http_txn *txn = s->txn;
5645 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005647 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5648 return 0;
5649
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005650 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005651 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005652 /* Output closed while we were sending data. We must abort and
5653 * wake the other side up.
5654 */
5655 msg->msg_state = HTTP_MSG_ERROR;
5656 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005657 return 1;
5658 }
5659
Willy Tarreaud98cf932009-12-27 22:54:55 +01005660 /* Note that we don't have to send 100-continue back because we don't
5661 * need the data to complete our job, and it's up to the server to
5662 * decide whether to return 100, 417 or anything else in return of
5663 * an "Expect: 100-continue" header.
5664 */
5665
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005666 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005667 /* we have msg->sov which points to the first byte of message
5668 * body, and req->buf.p still points to the beginning of the
5669 * message. We forward the headers now, as we don't need them
5670 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005672 b_adv(req->buf, msg->sov);
5673 msg->next -= msg->sov;
5674 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005675
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005676 /* The previous analysers guarantee that the state is somewhere
5677 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5678 * msg->next are always correct.
5679 */
5680 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5681 if (msg->flags & HTTP_MSGF_TE_CHNK)
5682 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5683 else
5684 msg->msg_state = HTTP_MSG_DATA;
5685 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005686 }
5687
Willy Tarreau7ba23542014-04-17 21:50:00 +02005688 /* Some post-connect processing might want us to refrain from starting to
5689 * forward data. Currently, the only reason for this is "balance url_param"
5690 * whichs need to parse/process the request after we've enabled forwarding.
5691 */
5692 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005693 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005694 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005695 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005696 goto missing_data;
5697 }
5698 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5699 }
5700
Willy Tarreau80a92c02014-03-12 10:41:13 +01005701 /* in most states, we should abort in case of early close */
5702 channel_auto_close(req);
5703
Willy Tarreauefdf0942014-04-24 20:08:57 +02005704 if (req->to_forward) {
5705 /* We can't process the buffer's contents yet */
5706 req->flags |= CF_WAKE_WRITE;
5707 goto missing_data;
5708 }
5709
Willy Tarreaud98cf932009-12-27 22:54:55 +01005710 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005711 if (msg->msg_state == HTTP_MSG_DATA) {
5712 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005713 /* we may have some pending data starting at req->buf->p */
5714 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005715 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005716 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005717 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005718 msg->next += msg->chunk_len;
5719 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005720
5721 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005722 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005723 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005724 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005725 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005726 }
5727 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005728 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005729 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 * TRAILERS state.
5731 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005732 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005733
Willy Tarreau54d23df2012-10-25 19:04:45 +02005734 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005735 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005736 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005737 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005738 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005739 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005741 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005744 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005745 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005746 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005747
5748 if (ret == 0)
5749 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005750 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005751 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005752 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005753 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005755 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 /* we're in MSG_CHUNK_SIZE now */
5757 }
5758 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005759 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760
5761 if (ret == 0)
5762 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005763 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005764 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005765 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005766 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005767 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005768 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005769 /* we're in HTTP_MSG_DONE now */
5770 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005771 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005772 int old_state = msg->msg_state;
5773
Willy Tarreau610ecce2010-01-04 21:15:02 +01005774 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005775
5776 /* we may have some pending data starting at req->buf->p
5777 * such as last chunk of data or trailers.
5778 */
5779 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005780 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005781 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005782 msg->next = 0;
5783
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005784 /* we don't want to forward closes on DONE except in
5785 * tunnel mode.
5786 */
5787 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005788 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005789 if (http_resync_states(s)) {
5790 /* some state changes occurred, maybe the analyser
5791 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005792 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005793 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005794 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005795 /* request errors are most likely due to
5796 * the server aborting the transfer.
5797 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005798 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005799 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005800 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005801 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005802 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005803 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005804 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005805 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005806
5807 /* If "option abortonclose" is set on the backend, we
5808 * want to monitor the client's connection and forward
5809 * any shutdown notification to the server, which will
5810 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005811 * request. We only do that in tunnel mode, and not in
5812 * other modes since it can be abused to exhaust source
5813 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005814 */
5815 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005816 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005817 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5818 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5819 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005820 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005821 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005822 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005823 /* POST requests may require to read extra CRLF
5824 * sent by broken browsers and which could cause
5825 * an RST to be sent upon close on some systems
5826 * (eg: Linux).
5827 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005828 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005829 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005830
Willy Tarreau610ecce2010-01-04 21:15:02 +01005831 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005832 }
5833 }
5834
Willy Tarreaud98cf932009-12-27 22:54:55 +01005835 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005836 /* we may have some pending data starting at req->buf->p */
5837 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005838 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005839 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5840
Willy Tarreaubed410e2014-04-22 08:19:34 +02005841 msg->next = 0;
5842 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5843
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005844 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005845 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005846 if (!(s->flags & SF_ERR_MASK))
5847 s->flags |= SF_ERR_CLICL;
5848 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005849 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005851 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005852 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 }
5854
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005855 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005856 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005857 if (objt_server(s->target))
5858 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859
5860 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005861 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005862
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005863 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005864 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005865 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005866
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005867 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005868 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005869 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005870 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005871 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005872
Willy Tarreau5c620922011-05-11 19:56:11 +02005873 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005874 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005875 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005876 * modes are already handled by the stream sock layer. We must not do
5877 * this in content-length mode because it could present the MSG_MORE
5878 * flag with the last block of forwarded data, which would cause an
5879 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005880 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005881 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005882 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005883
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005884 return 0;
5885
Willy Tarreaud98cf932009-12-27 22:54:55 +01005886 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005887 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005888 if (sess->listener->counters)
5889 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005890
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005891 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005892 /* we may have some pending data starting at req->buf->p */
5893 b_adv(req->buf, msg->next);
5894 msg->next = 0;
5895
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005896 txn->req.msg_state = HTTP_MSG_ERROR;
5897 if (txn->status) {
5898 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005899 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005900 } else {
5901 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005902 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005903 }
5904 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005905 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005906
Willy Tarreaue7dff022015-04-03 01:14:29 +02005907 if (!(s->flags & SF_ERR_MASK))
5908 s->flags |= SF_ERR_PRXCOND;
5909 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005910 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005911 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005912 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005913 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005914 }
5915 return 0;
5916
5917 aborted_xfer:
5918 txn->req.msg_state = HTTP_MSG_ERROR;
5919 if (txn->status) {
5920 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005921 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005922 } else {
5923 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005924 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005925 }
5926 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005927 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005928
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005929 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005930 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005931 if (objt_server(s->target))
5932 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005933
Willy Tarreaue7dff022015-04-03 01:14:29 +02005934 if (!(s->flags & SF_ERR_MASK))
5935 s->flags |= SF_ERR_SRVCL;
5936 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005937 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005938 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005939 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005940 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005941 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 return 0;
5943}
5944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5946 * processing can continue on next analysers, or zero if it either needs more
5947 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005948 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 * when it has nothing left to do, and may remove any analyser when it wants to
5950 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005951 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005952int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005953{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005954 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005955 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005957 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005958 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005960 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005961
Willy Tarreau87b09662015-04-03 00:22:06 +02005962 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005963 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005965 rep,
5966 rep->rex, rep->wex,
5967 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005968 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005969 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971 /*
5972 * Now parse the partial (or complete) lines.
5973 * We will check the response syntax, and also join multi-line
5974 * headers. An index of all the lines will be elaborated while
5975 * parsing.
5976 *
5977 * For the parsing, we use a 28 states FSM.
5978 *
5979 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005980 * rep->buf->p = beginning of response
5981 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5982 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005983 * msg->eol = end of current header or line (LF or CRLF)
5984 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 */
5986
Willy Tarreau628c40c2014-04-24 19:11:26 +02005987 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005988 /* There's a protected area at the end of the buffer for rewriting
5989 * purposes. We don't want to start to parse the request if the
5990 * protected area is affected, because we may have to move processed
5991 * data later, which is much more complicated.
5992 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005993 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005994 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005995 /* some data has still not left the buffer, wake us once that's done */
5996 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5997 goto abort_response;
5998 channel_dont_close(rep);
5999 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006000 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006001 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006002 }
6003
Willy Tarreau379357a2013-06-08 12:55:46 +02006004 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6005 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6006 buffer_slow_realign(rep->buf);
6007
Willy Tarreau9b28e032012-10-12 23:49:43 +02006008 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006009 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006010 }
6011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012 /* 1: we might have to print this header in debug mode */
6013 if (unlikely((global.mode & MODE_DEBUG) &&
6014 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006015 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006017
Willy Tarreau9b28e032012-10-12 23:49:43 +02006018 sol = rep->buf->p;
6019 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 sol += hdr_idx_first_pos(&txn->hdr_idx);
6023 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006024
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 while (cur_idx) {
6026 eol = sol + txn->hdr_idx.v[cur_idx].len;
6027 debug_hdr("srvhdr", s, sol, eol);
6028 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6029 cur_idx = txn->hdr_idx.v[cur_idx].next;
6030 }
6031 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 /*
6034 * Now we quickly check if we have found a full valid response.
6035 * If not so, we check the FD and buffer states before leaving.
6036 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006037 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 * responses are checked first.
6039 *
6040 * Depending on whether the client is still there or not, we
6041 * may send an error response back or not. Note that normally
6042 * we should only check for HTTP status there, and check I/O
6043 * errors somewhere else.
6044 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045
Willy Tarreau655dce92009-11-08 13:10:58 +01006046 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047 /* Invalid response */
6048 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6049 /* we detected a parsing error. We want to archive this response
6050 * in the dedicated proxy area for later troubleshooting.
6051 */
6052 hdr_response_bad:
6053 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006054 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006056 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006057 if (objt_server(s->target)) {
6058 objt_server(s->target)->counters.failed_resp++;
6059 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006060 }
Willy Tarreau64648412010-03-05 10:41:54 +01006061 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006062 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006063 rep->analysers = 0;
6064 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006065 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006066 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006067 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068
Willy Tarreaue7dff022015-04-03 01:14:29 +02006069 if (!(s->flags & SF_ERR_MASK))
6070 s->flags |= SF_ERR_PRXCOND;
6071 if (!(s->flags & SF_FINST_MASK))
6072 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073
6074 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006075 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006076
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006078 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006079 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006080 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081 goto hdr_response_bad;
6082 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006085 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006087 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006088 else if (txn->flags & TX_NOT_FIRST)
6089 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006090
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006091 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006092 if (objt_server(s->target)) {
6093 objt_server(s->target)->counters.failed_resp++;
6094 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006095 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006096
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006097 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 rep->analysers = 0;
6099 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006100 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006101 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006102 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006103
Willy Tarreaue7dff022015-04-03 01:14:29 +02006104 if (!(s->flags & SF_ERR_MASK))
6105 s->flags |= SF_ERR_SRVCL;
6106 if (!(s->flags & SF_FINST_MASK))
6107 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006108 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006110
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006111 /* read timeout : return a 504 to the client. */
6112 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006113 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006114 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006115 else if (txn->flags & TX_NOT_FIRST)
6116 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006117
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006118 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006119 if (objt_server(s->target)) {
6120 objt_server(s->target)->counters.failed_resp++;
6121 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006122 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006123
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006124 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 rep->analysers = 0;
6126 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006127 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006128 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006129 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006130
Willy Tarreaue7dff022015-04-03 01:14:29 +02006131 if (!(s->flags & SF_ERR_MASK))
6132 s->flags |= SF_ERR_SRVTO;
6133 if (!(s->flags & SF_FINST_MASK))
6134 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006135 return 0;
6136 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006137
Willy Tarreauf003d372012-11-26 13:35:37 +01006138 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006139 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006140 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006141 s->be->be_counters.cli_aborts++;
6142 if (objt_server(s->target))
6143 objt_server(s->target)->counters.cli_aborts++;
6144
6145 rep->analysers = 0;
6146 channel_auto_close(rep);
6147
6148 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006149 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006150 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006151
Willy Tarreaue7dff022015-04-03 01:14:29 +02006152 if (!(s->flags & SF_ERR_MASK))
6153 s->flags |= SF_ERR_CLICL;
6154 if (!(s->flags & SF_FINST_MASK))
6155 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006156
Willy Tarreau87b09662015-04-03 00:22:06 +02006157 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006158 return 0;
6159 }
6160
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006161 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006162 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006163 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006164 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006165 else if (txn->flags & TX_NOT_FIRST)
6166 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006167
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006168 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006169 if (objt_server(s->target)) {
6170 objt_server(s->target)->counters.failed_resp++;
6171 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006172 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006173
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006174 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006175 rep->analysers = 0;
6176 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006177 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006178 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006179 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006180
Willy Tarreaue7dff022015-04-03 01:14:29 +02006181 if (!(s->flags & SF_ERR_MASK))
6182 s->flags |= SF_ERR_SRVCL;
6183 if (!(s->flags & SF_FINST_MASK))
6184 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006185 return 0;
6186 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006187
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006189 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006190 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006191 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006192 else if (txn->flags & TX_NOT_FIRST)
6193 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006194
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006195 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006196 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006197 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006198
Willy Tarreaue7dff022015-04-03 01:14:29 +02006199 if (!(s->flags & SF_ERR_MASK))
6200 s->flags |= SF_ERR_CLICL;
6201 if (!(s->flags & SF_FINST_MASK))
6202 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006203
Willy Tarreau87b09662015-04-03 00:22:06 +02006204 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006205 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006206 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006207
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006208 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006209 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210 return 0;
6211 }
6212
6213 /* More interesting part now : we know that we have a complete
6214 * response which at least looks like HTTP. We have an indicator
6215 * of each header's length, so we can parse them quickly.
6216 */
6217
6218 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006219 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221 /*
6222 * 1: get the status code
6223 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006224 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006225 if (n < 1 || n > 5)
6226 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006227 /* when the client triggers a 4xx from the server, it's most often due
6228 * to a missing object or permission. These events should be tracked
6229 * because if they happen often, it may indicate a brute force or a
6230 * vulnerability scan.
6231 */
6232 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006233 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006234
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006235 if (objt_server(s->target))
6236 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237
Willy Tarreau91852eb2015-05-01 13:26:00 +02006238 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6239 * exactly one digit "." one digit. This check may be disabled using
6240 * option accept-invalid-http-response.
6241 */
6242 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6243 if (msg->sl.st.v_l != 8) {
6244 msg->err_pos = 0;
6245 goto hdr_response_bad;
6246 }
6247
6248 if (rep->buf->p[4] != '/' ||
6249 !isdigit((unsigned char)rep->buf->p[5]) ||
6250 rep->buf->p[6] != '.' ||
6251 !isdigit((unsigned char)rep->buf->p[7])) {
6252 msg->err_pos = 4;
6253 goto hdr_response_bad;
6254 }
6255 }
6256
Willy Tarreau5b154472009-12-21 20:11:07 +01006257 /* check if the response is HTTP/1.1 or above */
6258 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006259 ((rep->buf->p[5] > '1') ||
6260 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006261 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006262
6263 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006264 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 +01006265
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006266 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006267 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006268
Willy Tarreau9b28e032012-10-12 23:49:43 +02006269 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006270
Willy Tarreau39650402010-03-15 19:44:39 +01006271 /* Adjust server's health based on status code. Note: status codes 501
6272 * and 505 are triggered on demand by client request, so we must not
6273 * count them as server failures.
6274 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006275 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006276 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006277 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006278 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006279 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006280 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006282 /*
6283 * 2: check for cacheability.
6284 */
6285
6286 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006287 case 100:
6288 /*
6289 * We may be facing a 100-continue response, in which case this
6290 * is not the right response, and we're waiting for the next one.
6291 * Let's allow this response to go to the client and wait for the
6292 * next one.
6293 */
6294 hdr_idx_init(&txn->hdr_idx);
6295 msg->next -= channel_forward(rep, msg->next);
6296 msg->msg_state = HTTP_MSG_RPBEFORE;
6297 txn->status = 0;
6298 s->logs.t_data = -1; /* was not a response yet */
6299 goto next_one;
6300
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006301 case 200:
6302 case 203:
6303 case 206:
6304 case 300:
6305 case 301:
6306 case 410:
6307 /* RFC2616 @13.4:
6308 * "A response received with a status code of
6309 * 200, 203, 206, 300, 301 or 410 MAY be stored
6310 * by a cache (...) unless a cache-control
6311 * directive prohibits caching."
6312 *
6313 * RFC2616 @9.5: POST method :
6314 * "Responses to this method are not cacheable,
6315 * unless the response includes appropriate
6316 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006317 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006318 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006319 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006320 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6321 break;
6322 default:
6323 break;
6324 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006326 /*
6327 * 3: we may need to capture headers
6328 */
6329 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006330 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006331 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006332 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006333
Willy Tarreau557f1992015-05-01 10:05:17 +02006334 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6335 * by RFC7230#3.3.3 :
6336 *
6337 * The length of a message body is determined by one of the following
6338 * (in order of precedence):
6339 *
6340 * 1. Any response to a HEAD request and any response with a 1xx
6341 * (Informational), 204 (No Content), or 304 (Not Modified) status
6342 * code is always terminated by the first empty line after the
6343 * header fields, regardless of the header fields present in the
6344 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006345 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006346 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6347 * the connection will become a tunnel immediately after the empty
6348 * line that concludes the header fields. A client MUST ignore any
6349 * Content-Length or Transfer-Encoding header fields received in
6350 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006351 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006352 * 3. If a Transfer-Encoding header field is present and the chunked
6353 * transfer coding (Section 4.1) is the final encoding, the message
6354 * body length is determined by reading and decoding the chunked
6355 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006357 * If a Transfer-Encoding header field is present in a response and
6358 * the chunked transfer coding is not the final encoding, the
6359 * message body length is determined by reading the connection until
6360 * it is closed by the server. If a Transfer-Encoding header field
6361 * is present in a request and the chunked transfer coding is not
6362 * the final encoding, the message body length cannot be determined
6363 * reliably; the server MUST respond with the 400 (Bad Request)
6364 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006365 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006366 * If a message is received with both a Transfer-Encoding and a
6367 * Content-Length header field, the Transfer-Encoding overrides the
6368 * Content-Length. Such a message might indicate an attempt to
6369 * perform request smuggling (Section 9.5) or response splitting
6370 * (Section 9.4) and ought to be handled as an error. A sender MUST
6371 * remove the received Content-Length field prior to forwarding such
6372 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006373 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006374 * 4. If a message is received without Transfer-Encoding and with
6375 * either multiple Content-Length header fields having differing
6376 * field-values or a single Content-Length header field having an
6377 * invalid value, then the message framing is invalid and the
6378 * recipient MUST treat it as an unrecoverable error. If this is a
6379 * request message, the server MUST respond with a 400 (Bad Request)
6380 * status code and then close the connection. If this is a response
6381 * message received by a proxy, the proxy MUST close the connection
6382 * to the server, discard the received response, and send a 502 (Bad
6383 * Gateway) response to the client. If this is a response message
6384 * received by a user agent, the user agent MUST close the
6385 * connection to the server and discard the received response.
6386 *
6387 * 5. If a valid Content-Length header field is present without
6388 * Transfer-Encoding, its decimal value defines the expected message
6389 * body length in octets. If the sender closes the connection or
6390 * the recipient times out before the indicated number of octets are
6391 * received, the recipient MUST consider the message to be
6392 * incomplete and close the connection.
6393 *
6394 * 6. If this is a request message and none of the above are true, then
6395 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006396 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006397 * 7. Otherwise, this is a response message without a declared message
6398 * body length, so the message body length is determined by the
6399 * number of octets received prior to the server closing the
6400 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006401 */
6402
6403 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006404 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006405 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006406 * FIXME: should we parse anyway and return an error on chunked encoding ?
6407 */
6408 if (txn->meth == HTTP_METH_HEAD ||
6409 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006410 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006411 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006412 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006413 goto skip_content_length;
6414 }
6415
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006416 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006417 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006418 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006419 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006420 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6421 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006422 /* bad transfer-encoding (chunked followed by something else) */
6423 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006424 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006425 break;
6426 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427 }
6428
Willy Tarreau1c913912015-04-30 10:57:51 +02006429 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006431 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006432 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6434 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006435 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006436 signed long long cl;
6437
Willy Tarreauad14f752011-09-02 20:33:27 +02006438 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006439 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006440 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006441 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442
Willy Tarreauad14f752011-09-02 20:33:27 +02006443 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006444 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006445 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006446 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006447
Willy Tarreauad14f752011-09-02 20:33:27 +02006448 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006449 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006450 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006451 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006452
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006453 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006454 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006456 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006457
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006458 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006459 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006460 }
6461
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006462 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006463 select_compression_response_header(s, rep->buf);
6464
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006466 /* Now we have to check if we need to modify the Connection header.
6467 * This is more difficult on the response than it is on the request,
6468 * because we can have two different HTTP versions and we don't know
6469 * how the client will interprete a response. For instance, let's say
6470 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6471 * HTTP/1.1 response without any header. Maybe it will bound itself to
6472 * HTTP/1.0 because it only knows about it, and will consider the lack
6473 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6474 * the lack of header as a keep-alive. Thus we will use two flags
6475 * indicating how a request MAY be understood by the client. In case
6476 * of multiple possibilities, we'll fix the header to be explicit. If
6477 * ambiguous cases such as both close and keepalive are seen, then we
6478 * will fall back to explicit close. Note that we won't take risks with
6479 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006480 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006481 */
6482
Willy Tarreaudc008c52010-02-01 16:20:08 +01006483 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6484 txn->status == 101)) {
6485 /* Either we've established an explicit tunnel, or we're
6486 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006487 * to understand the next protocols. We have to switch to tunnel
6488 * mode, so that we transfer the request and responses then let
6489 * this protocol pass unmodified. When we later implement specific
6490 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006491 * header which contains information about that protocol for
6492 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006493 */
6494 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6495 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006496 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6497 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006498 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006500 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006501
Willy Tarreau70dffda2014-01-30 03:07:23 +01006502 /* this situation happens when combining pretend-keepalive with httpclose. */
6503 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006504 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006505 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006506 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6507
Willy Tarreau60466522010-01-18 19:08:45 +01006508 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006509 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006510 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6511 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006512
Willy Tarreau60466522010-01-18 19:08:45 +01006513 /* now adjust header transformations depending on current state */
6514 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6515 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6516 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006517 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006518 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006519 }
Willy Tarreau60466522010-01-18 19:08:45 +01006520 else { /* SCL / KAL */
6521 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006522 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006523 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006524 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006525
Willy Tarreau60466522010-01-18 19:08:45 +01006526 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006527 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006528
Willy Tarreau60466522010-01-18 19:08:45 +01006529 /* Some keep-alive responses are converted to Server-close if
6530 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006531 */
Willy Tarreau60466522010-01-18 19:08:45 +01006532 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6533 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006534 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006536 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006537 }
6538
Willy Tarreau7959a552013-09-23 16:44:27 +02006539 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006540 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006541
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006542 /* end of job, return OK */
6543 rep->analysers &= ~an_bit;
6544 rep->analyse_exp = TICK_ETERNITY;
6545 channel_auto_close(rep);
6546 return 1;
6547
6548 abort_keep_alive:
6549 /* A keep-alive request to the server failed on a network error.
6550 * The client is required to retry. We need to close without returning
6551 * any other information so that the client retries.
6552 */
6553 txn->status = 0;
6554 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006555 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006556 channel_auto_close(rep);
6557 s->logs.logwait = 0;
6558 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006559 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006560 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006561 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006562 return 0;
6563}
6564
6565/* This function performs all the processing enabled for the current response.
6566 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006567 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006568 * other functions. It works like process_request (see indications above).
6569 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006570int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006571{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006572 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006573 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006574 struct http_msg *msg = &txn->rsp;
6575 struct proxy *cur_proxy;
6576 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006577 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006578
Willy Tarreau87b09662015-04-03 00:22:06 +02006579 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006580 now_ms, __FUNCTION__,
6581 s,
6582 rep,
6583 rep->rex, rep->wex,
6584 rep->flags,
6585 rep->buf->i,
6586 rep->analysers);
6587
6588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6589 return 0;
6590
Willy Tarreau70730dd2014-04-24 18:06:27 +02006591 /* The stats applet needs to adjust the Connection header but we don't
6592 * apply any filter there.
6593 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006594 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6595 rep->analysers &= ~an_bit;
6596 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006597 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006598 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006599
Willy Tarreau58975672014-04-24 21:13:57 +02006600 /*
6601 * We will have to evaluate the filters.
6602 * As opposed to version 1.2, now they will be evaluated in the
6603 * filters order and not in the header order. This means that
6604 * each filter has to be validated among all headers.
6605 *
6606 * Filters are tried with ->be first, then with ->fe if it is
6607 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006608 *
6609 * Maybe we are in resume condiion. In this case I choose the
6610 * "struct proxy" which contains the rule list matching the resume
6611 * pointer. If none of theses "struct proxy" match, I initialise
6612 * the process with the first one.
6613 *
6614 * In fact, I check only correspondance betwwen the current list
6615 * pointer and the ->fe rule list. If it doesn't match, I initialize
6616 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006617 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006618 if (s->current_rule_list == &sess->fe->http_res_rules)
6619 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006620 else
6621 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006622 while (1) {
6623 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006626 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006627 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006628
Willy Tarreau51d861a2015-05-22 17:30:48 +02006629 if (ret == HTTP_RULE_RES_BADREQ)
6630 goto return_srv_prx_502;
6631
6632 if (ret == HTTP_RULE_RES_DONE) {
6633 rep->analysers &= ~an_bit;
6634 rep->analyse_exp = TICK_ETERNITY;
6635 return 1;
6636 }
6637 }
6638
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006639 /* we need to be called again. */
6640 if (ret == HTTP_RULE_RES_YIELD) {
6641 channel_dont_close(rep);
6642 return 0;
6643 }
6644
Willy Tarreau58975672014-04-24 21:13:57 +02006645 /* try headers filters */
6646 if (rule_set->rsp_exp != NULL) {
6647 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6648 return_bad_resp:
6649 if (objt_server(s->target)) {
6650 objt_server(s->target)->counters.failed_resp++;
6651 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006652 }
Willy Tarreau58975672014-04-24 21:13:57 +02006653 s->be->be_counters.failed_resp++;
6654 return_srv_prx_502:
6655 rep->analysers = 0;
6656 txn->status = 502;
6657 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006658 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006659 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006660 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006661 if (!(s->flags & SF_ERR_MASK))
6662 s->flags |= SF_ERR_PRXCOND;
6663 if (!(s->flags & SF_FINST_MASK))
6664 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006665 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006666 }
Willy Tarreau58975672014-04-24 21:13:57 +02006667 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006668
Willy Tarreau58975672014-04-24 21:13:57 +02006669 /* has the response been denied ? */
6670 if (txn->flags & TX_SVDENY) {
6671 if (objt_server(s->target))
6672 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006673
Willy Tarreau58975672014-04-24 21:13:57 +02006674 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006675 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006676 if (sess->listener->counters)
6677 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006678
Willy Tarreau58975672014-04-24 21:13:57 +02006679 goto return_srv_prx_502;
6680 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 /* add response headers from the rule sets in the same order */
6683 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006684 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006685 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006687 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006688 ret = acl_pass(ret);
6689 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6690 ret = !ret;
6691 if (!ret)
6692 continue;
6693 }
6694 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6695 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006696 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006699 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006700 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006701 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006702 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006703
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006704 /* After this point, this anayzer can't return yield, so we can
6705 * remove the bit corresponding to this analyzer from the list.
6706 *
6707 * Note that the intermediate returns and goto found previously
6708 * reset the analyzers.
6709 */
6710 rep->analysers &= ~an_bit;
6711 rep->analyse_exp = TICK_ETERNITY;
6712
Willy Tarreau58975672014-04-24 21:13:57 +02006713 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006714 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006715 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 /*
6718 * Now check for a server cookie.
6719 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006720 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006721 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 /*
6724 * Check for cache-control or pragma headers if required.
6725 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006726 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 +02006727 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006728
Willy Tarreau58975672014-04-24 21:13:57 +02006729 /*
6730 * Add server cookie in the response if needed
6731 */
6732 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6733 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006734 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006735 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6736 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6737 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6738 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6739 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006740 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006741 /* the server is known, it's not the one the client requested, or the
6742 * cookie's last seen date needs to be refreshed. We have to
6743 * insert a set-cookie here, except if we want to insert only on POST
6744 * requests and this one isn't. Note that servers which don't have cookies
6745 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 */
Willy Tarreau58975672014-04-24 21:13:57 +02006747 if (!objt_server(s->target)->cookie) {
6748 chunk_printf(&trash,
6749 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6750 s->be->cookie_name);
6751 }
6752 else {
6753 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754
Willy Tarreau58975672014-04-24 21:13:57 +02006755 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6756 /* emit last_date, which is mandatory */
6757 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6758 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6759 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006760
Willy Tarreau58975672014-04-24 21:13:57 +02006761 if (s->be->cookie_maxlife) {
6762 /* emit first_date, which is either the original one or
6763 * the current date.
6764 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006765 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006766 s30tob64(txn->cookie_first_date ?
6767 txn->cookie_first_date >> 2 :
6768 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006769 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006770 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006771 }
Willy Tarreau58975672014-04-24 21:13:57 +02006772 chunk_appendf(&trash, "; path=/");
6773 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006774
Willy Tarreau58975672014-04-24 21:13:57 +02006775 if (s->be->cookie_domain)
6776 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006777
Willy Tarreau58975672014-04-24 21:13:57 +02006778 if (s->be->ck_opts & PR_CK_HTTPONLY)
6779 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006780
Willy Tarreau58975672014-04-24 21:13:57 +02006781 if (s->be->ck_opts & PR_CK_SECURE)
6782 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006783
Willy Tarreau58975672014-04-24 21:13:57 +02006784 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6785 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006786
Willy Tarreau58975672014-04-24 21:13:57 +02006787 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006788 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006789 /* the server did not change, only the date was updated */
6790 txn->flags |= TX_SCK_UPDATED;
6791 else
6792 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 /* Here, we will tell an eventual cache on the client side that we don't
6795 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6796 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6797 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006798 */
Willy Tarreau58975672014-04-24 21:13:57 +02006799 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006800
Willy Tarreau58975672014-04-24 21:13:57 +02006801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006802
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6804 "Cache-control: private", 22) < 0))
6805 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006806 }
Willy Tarreau58975672014-04-24 21:13:57 +02006807 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
Willy Tarreau58975672014-04-24 21:13:57 +02006809 /*
6810 * Check if result will be cacheable with a cookie.
6811 * We'll block the response if security checks have caught
6812 * nasty things such as a cacheable cookie.
6813 */
6814 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6815 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6816 (s->be->options & PR_O_CHK_CACHE)) {
6817 /* we're in presence of a cacheable response containing
6818 * a set-cookie header. We'll block it as requested by
6819 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820 */
Willy Tarreau58975672014-04-24 21:13:57 +02006821 if (objt_server(s->target))
6822 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006823
Willy Tarreau58975672014-04-24 21:13:57 +02006824 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006825 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006826 if (sess->listener->counters)
6827 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828
Willy Tarreau58975672014-04-24 21:13:57 +02006829 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6830 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6831 send_log(s->be, LOG_ALERT,
6832 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6833 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6834 goto return_srv_prx_502;
6835 }
Willy Tarreau03945942009-12-22 16:50:27 +01006836
Willy Tarreau70730dd2014-04-24 18:06:27 +02006837 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006838 /*
6839 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6840 * If an "Upgrade" token is found, the header is left untouched in order
6841 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006842 * if anything but "Upgrade" is present in the Connection header. We don't
6843 * want to touch any 101 response either since it's switching to another
6844 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006845 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006846 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006847 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006848 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006849 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6850 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006851
Willy Tarreau58975672014-04-24 21:13:57 +02006852 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6853 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6854 /* we want a keep-alive response here. Keep-alive header
6855 * required if either side is not 1.1.
6856 */
6857 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6858 want_flags |= TX_CON_KAL_SET;
6859 }
6860 else {
6861 /* we want a close response here. Close header required if
6862 * the server is 1.1, regardless of the client.
6863 */
6864 if (msg->flags & HTTP_MSGF_VER_11)
6865 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006866 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867
Willy Tarreau58975672014-04-24 21:13:57 +02006868 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6869 http_change_connection_header(txn, msg, want_flags);
6870 }
6871
6872 skip_header_mangling:
6873 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6874 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6875 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876
Willy Tarreau58975672014-04-24 21:13:57 +02006877 /* if the user wants to log as soon as possible, without counting
6878 * bytes from the server, then this is the right moment. We have
6879 * to temporarily assign bytes_out to log what we currently have.
6880 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006881 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006882 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6883 s->logs.bytes_out = txn->rsp.eoh;
6884 s->do_log(s);
6885 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006886 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006887 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006888}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreaud98cf932009-12-27 22:54:55 +01006890/* This function is an analyser which forwards response body (including chunk
6891 * sizes if any). It is called as soon as we must forward, even if we forward
6892 * zero byte. The only situation where it must not be called is when we're in
6893 * tunnel mode and we want to forward till the close. It's used both to forward
6894 * remaining data and to resync after end of body. It expects the msg_state to
6895 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006896 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006897 *
6898 * It is capable of compressing response data both in content-length mode and
6899 * in chunked mode. The state machines follows different flows depending on
6900 * whether content-length and chunked modes are used, since there are no
6901 * trailers in content-length :
6902 *
6903 * chk-mode cl-mode
6904 * ,----- BODY -----.
6905 * / \
6906 * V size > 0 V chk-mode
6907 * .--> SIZE -------------> DATA -------------> CRLF
6908 * | | size == 0 | last byte |
6909 * | v final crlf v inspected |
6910 * | TRAILERS -----------> DONE |
6911 * | |
6912 * `----------------------------------------------'
6913 *
6914 * Compression only happens in the DATA state, and must be flushed in final
6915 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6916 * is performed at once on final states for all bytes parsed, or when leaving
6917 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006918 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006919int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006920{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006921 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006922 struct http_txn *txn = s->txn;
6923 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006924 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006925 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006926 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006927
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006928 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6929 return 0;
6930
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006931 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006932 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006933 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006934 /* Output closed while we were sending data. We must abort and
6935 * wake the other side up.
6936 */
6937 msg->msg_state = HTTP_MSG_ERROR;
6938 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006939 return 1;
6940 }
6941
Willy Tarreau4fe41902010-06-07 22:27:41 +02006942 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006943 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006944
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006945 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006946 /* we have msg->sov which points to the first byte of message
6947 * body, and res->buf.p still points to the beginning of the
6948 * message. We forward the headers now, as we don't need them
6949 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006950 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006951 b_adv(res->buf, msg->sov);
6952 msg->next -= msg->sov;
6953 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006954
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006955 /* The previous analysers guarantee that the state is somewhere
6956 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6957 * msg->next are always correct.
6958 */
6959 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6960 if (msg->flags & HTTP_MSGF_TE_CHNK)
6961 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6962 else
6963 msg->msg_state = HTTP_MSG_DATA;
6964 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006965 }
6966
Willy Tarreauefdf0942014-04-24 20:08:57 +02006967 if (res->to_forward) {
6968 /* We can't process the buffer's contents yet */
6969 res->flags |= CF_WAKE_WRITE;
6970 goto missing_data;
6971 }
6972
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006973 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6974 /* We need a compression buffer in the DATA state to put the
6975 * output of compressed data, and in CRLF state to let the
6976 * TRAILERS state finish the job of removing the trailing CRLF.
6977 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006978 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006979 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006980 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006981 goto aborted_xfer; /* no memory */
6982 }
6983
6984 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006985 if (ret < 0) {
6986 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006987 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006988 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006989 compressing = 1;
6990 }
6991
Willy Tarreaud98cf932009-12-27 22:54:55 +01006992 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006993 switch (msg->msg_state - HTTP_MSG_DATA) {
6994 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006995 /* we may have some pending data starting at res->buf->p */
6996 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006997 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006998 if (ret < 0)
6999 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007000
Willy Tarreaud5a67832014-04-21 10:54:27 +02007001 if (msg->chunk_len) {
7002 /* input empty or output full */
7003 if (res->buf->i > msg->next)
7004 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007005 goto missing_data;
7006 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007007 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007008 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007009 if (msg->chunk_len > res->buf->i - msg->next) {
7010 /* output full */
7011 res->flags |= CF_WAKE_WRITE;
7012 goto missing_data;
7013 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007014 msg->next += msg->chunk_len;
7015 msg->chunk_len = 0;
7016 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007017
7018 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007019 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007020 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007021 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007022 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007023 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007024 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007025 /* fall through for HTTP_MSG_CHUNK_CRLF */
7026
7027 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7028 /* we want the CRLF after the data */
7029
7030 ret = http_skip_chunk_crlf(msg);
7031 if (ret == 0)
7032 goto missing_data;
7033 else if (ret < 0) {
7034 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007035 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007036 goto return_bad_res;
7037 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007038 /* we're in MSG_CHUNK_SIZE now, fall through */
7039
7040 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007041 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007042 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007043 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007044 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007045
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007046 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007047 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007048 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007049 else if (ret < 0) {
7050 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007051 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007052 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007053 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007054 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007055 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007056
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007057 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007058 if (unlikely(compressing)) {
7059 /* we need to flush output contents before syncing FSMs */
7060 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7061 compressing = 0;
7062 }
7063
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007064 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007065 if (ret == 0)
7066 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007067 else if (ret < 0) {
7068 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007069 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007070 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007071 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007072 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007073
7074 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007075 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007076 if (unlikely(compressing)) {
7077 /* we need to flush output contents before syncing FSMs */
7078 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7079 compressing = 0;
7080 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007081
Willy Tarreauc623c172014-04-18 09:53:50 +02007082 /* we may have some pending data starting at res->buf->p
7083 * such as a last chunk of data or trailers.
7084 */
7085 b_adv(res->buf, msg->next);
7086 msg->next = 0;
7087
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007088 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007089 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007090 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7091 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007092 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007093
Willy Tarreau610ecce2010-01-04 21:15:02 +01007094 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007095 /* some state changes occurred, maybe the analyser
7096 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007097 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007098 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007099 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007100 /* response errors are most likely due to
7101 * the client aborting the transfer.
7102 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007103 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007104 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007105 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007106 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007107 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007108 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007109 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007110 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007111 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007112 }
7113 }
7114
Willy Tarreaud98cf932009-12-27 22:54:55 +01007115 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007116 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007117 if (unlikely(compressing)) {
7118 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007119 compressing = 0;
7120 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007121
Willy Tarreauc623c172014-04-18 09:53:50 +02007122 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7123 b_adv(res->buf, msg->next);
7124 msg->next = 0;
7125 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7126 }
7127
Willy Tarreauf003d372012-11-26 13:35:37 +01007128 if (res->flags & CF_SHUTW)
7129 goto aborted_xfer;
7130
7131 /* stop waiting for data if the input is closed before the end. If the
7132 * client side was already closed, it means that the client has aborted,
7133 * so we don't want to count this as a server abort. Otherwise it's a
7134 * server abort.
7135 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007136 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007137 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007138 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007139 if (!(s->flags & SF_ERR_MASK))
7140 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007141 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007142 if (objt_server(s->target))
7143 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007144 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007145 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007146
Willy Tarreau40dba092010-03-04 18:14:51 +01007147 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007148 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007149 goto return_bad_res;
7150
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007151 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007152 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007153 * Similarly, with keep-alive on the client side, we don't want to forward a
7154 * close.
7155 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007156 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007157 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7158 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007159 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007160
Willy Tarreau5c620922011-05-11 19:56:11 +02007161 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007162 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007163 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007164 * modes are already handled by the stream sock layer. We must not do
7165 * this in content-length mode because it could present the MSG_MORE
7166 * flag with the last block of forwarded data, which would cause an
7167 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007168 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007169 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007170 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007171
Willy Tarreau87b09662015-04-03 00:22:06 +02007172 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007173 return 0;
7174
Willy Tarreau40dba092010-03-04 18:14:51 +01007175 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007176 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007177 if (objt_server(s->target))
7178 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007179
7180 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007181 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007182 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007183 compressing = 0;
7184 }
7185
Willy Tarreauc623c172014-04-18 09:53:50 +02007186 /* we may have some pending data starting at res->buf->p */
7187 if (s->comp_algo == NULL) {
7188 b_adv(res->buf, msg->next);
7189 msg->next = 0;
7190 }
7191
Willy Tarreaud98cf932009-12-27 22:54:55 +01007192 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007193 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007194 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007195 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007196 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007197 if (objt_server(s->target))
7198 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007199
Willy Tarreaue7dff022015-04-03 01:14:29 +02007200 if (!(s->flags & SF_ERR_MASK))
7201 s->flags |= SF_ERR_PRXCOND;
7202 if (!(s->flags & SF_FINST_MASK))
7203 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007204 return 0;
7205
7206 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007207 if (unlikely(compressing)) {
7208 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7209 compressing = 0;
7210 }
7211
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007212 txn->rsp.msg_state = HTTP_MSG_ERROR;
7213 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007214 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007215 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007216 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007217
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007218 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007219 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007220 if (objt_server(s->target))
7221 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007222
Willy Tarreaue7dff022015-04-03 01:14:29 +02007223 if (!(s->flags & SF_ERR_MASK))
7224 s->flags |= SF_ERR_CLICL;
7225 if (!(s->flags & SF_FINST_MASK))
7226 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007227 return 0;
7228}
7229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007230/* Iterate the same filter through all request headers.
7231 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232 * Since it can manage the switch to another backend, it updates the per-proxy
7233 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007235int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007236{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237 char *cur_ptr, *cur_end, *cur_next;
7238 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007239 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007241 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 last_hdr = 0;
7244
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 old_idx = 0;
7247
7248 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007249 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007251 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 (exp->action == ACT_ALLOW ||
7253 exp->action == ACT_DENY ||
7254 exp->action == ACT_TARPIT))
7255 return 0;
7256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007257 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 if (!cur_idx)
7259 break;
7260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007261 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 cur_ptr = cur_next;
7263 cur_end = cur_ptr + cur_hdr->len;
7264 cur_next = cur_end + cur_hdr->cr + 1;
7265
7266 /* Now we have one header between cur_ptr and cur_end,
7267 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268 */
7269
Willy Tarreau15a53a42015-01-21 13:39:42 +01007270 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 last_hdr = 1;
7275 break;
7276
7277 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 break;
7281
7282 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 break;
7286
7287 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007288 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7289 if (trash.len < 0)
7290 return -1;
7291
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007292 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 /* FIXME: if the user adds a newline in the replacement, the
7294 * index will not be recalculated for now, and the new line
7295 * will not be counted as a new header.
7296 */
7297
7298 cur_end += delta;
7299 cur_next += delta;
7300 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007301 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 break;
7303
7304 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007305 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 cur_next += delta;
7307
Willy Tarreaufa355d42009-11-29 18:12:29 +01007308 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007309 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7310 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 cur_hdr->len = 0;
7312 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007313 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 }
7318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 /* keep the link from this header to next one in case of later
7320 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 old_idx = cur_idx;
7323 }
7324 return 0;
7325}
7326
7327
7328/* Apply the filter to the request line.
7329 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7330 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 * Since it can manage the switch to another backend, it updates the per-proxy
7332 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007334int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 char *cur_ptr, *cur_end;
7337 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007338 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007339 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340
Willy Tarreau3d300592007-03-18 18:34:41 +01007341 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007343 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007344 (exp->action == ACT_ALLOW ||
7345 exp->action == ACT_DENY ||
7346 exp->action == ACT_TARPIT))
7347 return 0;
7348 else if (exp->action == ACT_REMOVE)
7349 return 0;
7350
7351 done = 0;
7352
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007354 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355
7356 /* Now we have the request line between cur_ptr and cur_end */
7357
Willy Tarreau15a53a42015-01-21 13:39:42 +01007358 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007359 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 done = 1;
7363 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 done = 1;
7368 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 done = 1;
7373 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007376 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7377 if (trash.len < 0)
7378 return -1;
7379
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007380 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 /* FIXME: if the user adds a newline in the replacement, the
7382 * index will not be recalculated for now, and the new line
7383 * will not be counted as a new header.
7384 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007385
Willy Tarreaufa355d42009-11-29 18:12:29 +01007386 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007388 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 HTTP_MSG_RQMETH,
7390 cur_ptr, cur_end + 1,
7391 NULL, NULL);
7392 if (unlikely(!cur_end))
7393 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 /* we have a full request and we know that we have either a CR
7396 * or an LF at <ptr>.
7397 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007398 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7399 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 /* there is no point trying this regex on headers */
7401 return 1;
7402 }
7403 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 return done;
7405}
Willy Tarreau97de6242006-12-27 17:18:38 +01007406
Willy Tarreau58f10d72006-12-04 02:26:12 +01007407
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007410 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 * unparsable request. Since it can manage the switch to another backend, it
7413 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007415int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416{
Willy Tarreau192252e2015-04-04 01:47:55 +02007417 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007418 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007419 struct hdr_exp *exp;
7420
7421 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 /*
7425 * The interleaving of transformations and verdicts
7426 * makes it difficult to decide to continue or stop
7427 * the evaluation.
7428 */
7429
Willy Tarreau6c123b12010-01-28 20:22:06 +01007430 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7431 break;
7432
Willy Tarreau3d300592007-03-18 18:34:41 +01007433 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007435 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007437
7438 /* if this filter had a condition, evaluate it now and skip to
7439 * next filter if the condition does not match.
7440 */
7441 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007442 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007443 ret = acl_pass(ret);
7444 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7445 ret = !ret;
7446
7447 if (!ret)
7448 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007449 }
7450
7451 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007452 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 if (unlikely(ret < 0))
7454 return -1;
7455
7456 if (likely(ret == 0)) {
7457 /* The filter did not match the request, it can be
7458 * iterated through all headers.
7459 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007460 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7461 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465}
7466
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468/* Find the end of a cookie value contained between <s> and <e>. It works the
7469 * same way as with headers above except that the semi-colon also ends a token.
7470 * See RFC2965 for more information. Note that it requires a valid header to
7471 * return a valid result.
7472 */
7473char *find_cookie_value_end(char *s, const char *e)
7474{
7475 int quoted, qdpair;
7476
7477 quoted = qdpair = 0;
7478 for (; s < e; s++) {
7479 if (qdpair) qdpair = 0;
7480 else if (quoted) {
7481 if (*s == '\\') qdpair = 1;
7482 else if (*s == '"') quoted = 0;
7483 }
7484 else if (*s == '"') quoted = 1;
7485 else if (*s == ',' || *s == ';') return s;
7486 }
7487 return s;
7488}
7489
7490/* Delete a value in a header between delimiters <from> and <next> in buffer
7491 * <buf>. The number of characters displaced is returned, and the pointer to
7492 * the first delimiter is updated if required. The function tries as much as
7493 * possible to respect the following principles :
7494 * - replace <from> delimiter by the <next> one unless <from> points to a
7495 * colon, in which case <next> is simply removed
7496 * - set exactly one space character after the new first delimiter, unless
7497 * there are not enough characters in the block being moved to do so.
7498 * - remove unneeded spaces before the previous delimiter and after the new
7499 * one.
7500 *
7501 * It is the caller's responsibility to ensure that :
7502 * - <from> points to a valid delimiter or the colon ;
7503 * - <next> points to a valid delimiter or the final CR/LF ;
7504 * - there are non-space chars before <from> ;
7505 * - there is a CR/LF at or after <next>.
7506 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007507int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508{
7509 char *prev = *from;
7510
7511 if (*prev == ':') {
7512 /* We're removing the first value, preserve the colon and add a
7513 * space if possible.
7514 */
7515 if (!http_is_crlf[(unsigned char)*next])
7516 next++;
7517 prev++;
7518 if (prev < next)
7519 *prev++ = ' ';
7520
7521 while (http_is_spht[(unsigned char)*next])
7522 next++;
7523 } else {
7524 /* Remove useless spaces before the old delimiter. */
7525 while (http_is_spht[(unsigned char)*(prev-1)])
7526 prev--;
7527 *from = prev;
7528
7529 /* copy the delimiter and if possible a space if we're
7530 * not at the end of the line.
7531 */
7532 if (!http_is_crlf[(unsigned char)*next]) {
7533 *prev++ = *next++;
7534 if (prev + 1 < next)
7535 *prev++ = ' ';
7536 while (http_is_spht[(unsigned char)*next])
7537 next++;
7538 }
7539 }
7540 return buffer_replace2(buf, prev, next, NULL, 0);
7541}
7542
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007543/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007544 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 * desirable to call it only when needed. This code is quite complex because
7546 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7547 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007549void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007550{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007551 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007552 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007554 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7556 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007559 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007560 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007562 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007564 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007566 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 hdr_beg = hdr_next;
7568 hdr_end = hdr_beg + cur_hdr->len;
7569 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 /* We have one full header between hdr_beg and hdr_end, and the
7572 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573 * "Cookie:" headers.
7574 */
7575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007577 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578 old_idx = cur_idx;
7579 continue;
7580 }
7581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 del_from = NULL; /* nothing to be deleted */
7583 preserve_hdr = 0; /* assume we may kill the whole header */
7584
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585 /* Now look for cookies. Conforming to RFC2109, we have to support
7586 * attributes whose name begin with a '$', and associate them with
7587 * the right cookie, if we want to delete this cookie.
7588 * So there are 3 cases for each cookie read :
7589 * 1) it's a special attribute, beginning with a '$' : ignore it.
7590 * 2) it's a server id cookie that we *MAY* want to delete : save
7591 * some pointers on it (last semi-colon, beginning of cookie...)
7592 * 3) it's an application cookie : we *MAY* have to delete a previous
7593 * "special" cookie.
7594 * At the end of loop, if a "special" cookie remains, we may have to
7595 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 * *MUST* delete it.
7597 *
7598 * Note: RFC2965 is unclear about the processing of spaces around
7599 * the equal sign in the ATTR=VALUE form. A careful inspection of
7600 * the RFC explicitly allows spaces before it, and not within the
7601 * tokens (attrs or values). An inspection of RFC2109 allows that
7602 * too but section 10.1.3 lets one think that spaces may be allowed
7603 * after the equal sign too, resulting in some (rare) buggy
7604 * implementations trying to do that. So let's do what servers do.
7605 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7606 * allowed quoted strings in values, with any possible character
7607 * after a backslash, including control chars and delimitors, which
7608 * causes parsing to become ambiguous. Browsers also allow spaces
7609 * within values even without quotes.
7610 *
7611 * We have to keep multiple pointers in order to support cookie
7612 * removal at the beginning, middle or end of header without
7613 * corrupting the header. All of these headers are valid :
7614 *
7615 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7616 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7617 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7618 * | | | | | | | | |
7619 * | | | | | | | | hdr_end <--+
7620 * | | | | | | | +--> next
7621 * | | | | | | +----> val_end
7622 * | | | | | +-----------> val_beg
7623 * | | | | +--------------> equal
7624 * | | | +----------------> att_end
7625 * | | +---------------------> att_beg
7626 * | +--------------------------> prev
7627 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 */
7629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7631 /* Iterate through all cookies on this line */
7632
7633 /* find att_beg */
7634 att_beg = prev + 1;
7635 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7636 att_beg++;
7637
7638 /* find att_end : this is the first character after the last non
7639 * space before the equal. It may be equal to hdr_end.
7640 */
7641 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 while (equal < hdr_end) {
7644 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 if (http_is_spht[(unsigned char)*equal++])
7647 continue;
7648 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 }
7650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7652 * is between <att_beg> and <equal>, both may be identical.
7653 */
7654
7655 /* look for end of cookie if there is an equal sign */
7656 if (equal < hdr_end && *equal == '=') {
7657 /* look for the beginning of the value */
7658 val_beg = equal + 1;
7659 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7660 val_beg++;
7661
7662 /* find the end of the value, respecting quotes */
7663 next = find_cookie_value_end(val_beg, hdr_end);
7664
7665 /* make val_end point to the first white space or delimitor after the value */
7666 val_end = next;
7667 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7668 val_end--;
7669 } else {
7670 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007671 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 /* We have nothing to do with attributes beginning with '$'. However,
7674 * they will automatically be removed if a header before them is removed,
7675 * since they're supposed to be linked together.
7676 */
7677 if (*att_beg == '$')
7678 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 /* Ignore cookies with no equal sign */
7681 if (equal == next) {
7682 /* This is not our cookie, so we must preserve it. But if we already
7683 * scheduled another cookie for removal, we cannot remove the
7684 * complete header, but we can remove the previous block itself.
7685 */
7686 preserve_hdr = 1;
7687 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 val_end += delta;
7690 next += delta;
7691 hdr_end += delta;
7692 hdr_next += delta;
7693 cur_hdr->len += delta;
7694 http_msg_move_end(&txn->req, delta);
7695 prev = del_from;
7696 del_from = NULL;
7697 }
7698 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 /* if there are spaces around the equal sign, we need to
7702 * strip them otherwise we'll get trouble for cookie captures,
7703 * or even for rewrites. Since this happens extremely rarely,
7704 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7707 int stripped_before = 0;
7708 int stripped_after = 0;
7709
7710 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007711 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 equal += stripped_before;
7713 val_beg += stripped_before;
7714 }
7715
7716 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 val_beg += stripped_after;
7719 stripped_before += stripped_after;
7720 }
7721
7722 val_end += stripped_before;
7723 next += stripped_before;
7724 hdr_end += stripped_before;
7725 hdr_next += stripped_before;
7726 cur_hdr->len += stripped_before;
7727 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 /* First, let's see if we want to capture this cookie. We check
7732 * that we don't already have a client side cookie, because we
7733 * can only capture one. Also as an optimisation, we ignore
7734 * cookies shorter than the declared name.
7735 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007736 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7737 (val_end - att_beg >= sess->fe->capture_namelen) &&
7738 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 int log_len = val_end - att_beg;
7740
7741 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7742 Alert("HTTP logging : out of memory.\n");
7743 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007744 if (log_len > sess->fe->capture_len)
7745 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 memcpy(txn->cli_cookie, att_beg, log_len);
7747 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Willy Tarreaubca99692010-10-06 19:25:55 +02007751 /* Persistence cookies in passive, rewrite or insert mode have the
7752 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007754 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007756 * For cookies in prefix mode, the form is :
7757 *
7758 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007760 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7761 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7762 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7766 * have the server ID between val_beg and delim, and the original cookie between
7767 * delim+1 and val_end. Otherwise, delim==val_end :
7768 *
7769 * Cookie: NAME=SRV; # in all but prefix modes
7770 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7771 * | || || | |+-> next
7772 * | || || | +--> val_end
7773 * | || || +---------> delim
7774 * | || |+------------> val_beg
7775 * | || +-------------> att_end = equal
7776 * | |+-----------------> att_beg
7777 * | +------------------> prev
7778 * +-------------------------> hdr_beg
7779 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007781 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 for (delim = val_beg; delim < val_end; delim++)
7783 if (*delim == COOKIE_DELIM)
7784 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 } else {
7786 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007788 /* Now check if the cookie contains a date field, which would
7789 * appear after a vertical bar ('|') just after the server name
7790 * and before the delimiter.
7791 */
7792 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7793 if (vbar1) {
7794 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007795 * right is the last seen date. It is a base64 encoded
7796 * 30-bit value representing the UNIX date since the
7797 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007799 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007800 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007801 if (val_end - vbar1 >= 5) {
7802 val = b64tos30(vbar1);
7803 if (val > 0)
7804 txn->cookie_last_date = val << 2;
7805 }
7806 /* look for a second vertical bar */
7807 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7808 if (vbar1 && (val_end - vbar1 > 5)) {
7809 val = b64tos30(vbar1 + 1);
7810 if (val > 0)
7811 txn->cookie_first_date = val << 2;
7812 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 }
7814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007815
Willy Tarreauf64d1412010-10-07 20:06:11 +02007816 /* if the cookie has an expiration date and the proxy wants to check
7817 * it, then we do that now. We first check if the cookie is too old,
7818 * then only if it has expired. We detect strict overflow because the
7819 * time resolution here is not great (4 seconds). Cookies with dates
7820 * in the future are ignored if their offset is beyond one day. This
7821 * allows an admin to fix timezone issues without expiring everyone
7822 * and at the same time avoids keeping unwanted side effects for too
7823 * long.
7824 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007825 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7826 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007827 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007828 txn->flags &= ~TX_CK_MASK;
7829 txn->flags |= TX_CK_OLD;
7830 delim = val_beg; // let's pretend we have not found the cookie
7831 txn->cookie_first_date = 0;
7832 txn->cookie_last_date = 0;
7833 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007834 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7835 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007836 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007837 txn->flags &= ~TX_CK_MASK;
7838 txn->flags |= TX_CK_EXPIRED;
7839 delim = val_beg; // let's pretend we have not found the cookie
7840 txn->cookie_first_date = 0;
7841 txn->cookie_last_date = 0;
7842 }
7843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007844 /* Here, we'll look for the first running server which supports the cookie.
7845 * This allows to share a same cookie between several servers, for example
7846 * to dedicate backup servers to specific servers only.
7847 * However, to prevent clients from sticking to cookie-less backup server
7848 * when they have incidentely learned an empty cookie, we simply ignore
7849 * empty cookies and mark them as invalid.
7850 * The same behaviour is applied when persistence must be ignored.
7851 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007852 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007854
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007855 while (srv) {
7856 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7857 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007858 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007859 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007860 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 /* we found the server and we can use it */
7862 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007863 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007864 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007866 break;
7867 } else {
7868 /* we found a server, but it's down,
7869 * mark it as such and go on in case
7870 * another one is available.
7871 */
7872 txn->flags &= ~TX_CK_MASK;
7873 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 srv = srv->next;
7877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007878
Willy Tarreauf64d1412010-10-07 20:06:11 +02007879 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007880 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007882 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007883 txn->flags |= TX_CK_UNUSED;
7884 else
7885 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007887
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 /* depending on the cookie mode, we may have to either :
7889 * - delete the complete cookie if we're in insert+indirect mode, so that
7890 * the server never sees it ;
7891 * - remove the server id from the cookie value, and tag the cookie as an
7892 * application cookie so that it does not get accidentely removed later,
7893 * if we're in cookie prefix mode
7894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007895 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007896 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 val_end += delta;
7900 next += delta;
7901 hdr_end += delta;
7902 hdr_next += delta;
7903 cur_hdr->len += delta;
7904 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 del_from = NULL;
7907 preserve_hdr = 1; /* we want to keep this cookie */
7908 }
7909 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007910 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 del_from = prev;
7912 }
7913 } else {
7914 /* This is not our cookie, so we must preserve it. But if we already
7915 * scheduled another cookie for removal, we cannot remove the
7916 * complete header, but we can remove the previous block itself.
7917 */
7918 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007921 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007922 if (att_beg >= del_from)
7923 att_beg += delta;
7924 if (att_end >= del_from)
7925 att_end += delta;
7926 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 val_end += delta;
7928 next += delta;
7929 hdr_end += delta;
7930 hdr_next += delta;
7931 cur_hdr->len += delta;
7932 http_msg_move_end(&txn->req, delta);
7933 prev = del_from;
7934 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007938 /* continue with next cookie on this header line */
7939 att_beg = next;
7940 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 /* There are no more cookies on this line.
7943 * We may still have one (or several) marked for deletion at the
7944 * end of the line. We must do this now in two ways :
7945 * - if some cookies must be preserved, we only delete from the
7946 * mark to the end of line ;
7947 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 cur_hdr->len += delta;
7955 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007956 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957
7958 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007959 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7960 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007962 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007965 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 }
7967
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971}
7972
7973
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974/* Iterate the same filter through all response headers contained in <rtr>.
7975 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7976 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007977int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 char *cur_ptr, *cur_end, *cur_next;
7980 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007981 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007983 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984
7985 last_hdr = 0;
7986
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 old_idx = 0;
7989
7990 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007991 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007993 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 (exp->action == ACT_ALLOW ||
7995 exp->action == ACT_DENY))
7996 return 0;
7997
7998 cur_idx = txn->hdr_idx.v[old_idx].next;
7999 if (!cur_idx)
8000 break;
8001
8002 cur_hdr = &txn->hdr_idx.v[cur_idx];
8003 cur_ptr = cur_next;
8004 cur_end = cur_ptr + cur_hdr->len;
8005 cur_next = cur_end + cur_hdr->cr + 1;
8006
8007 /* Now we have one header between cur_ptr and cur_end,
8008 * and the next header starts at cur_next.
8009 */
8010
Willy Tarreau15a53a42015-01-21 13:39:42 +01008011 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 switch (exp->action) {
8013 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008014 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 last_hdr = 1;
8016 break;
8017
8018 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 last_hdr = 1;
8021 break;
8022
8023 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008024 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8025 if (trash.len < 0)
8026 return -1;
8027
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008028 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 /* FIXME: if the user adds a newline in the replacement, the
8030 * index will not be recalculated for now, and the new line
8031 * will not be counted as a new header.
8032 */
8033
8034 cur_end += delta;
8035 cur_next += delta;
8036 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008037 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 break;
8039
8040 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008041 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 cur_next += delta;
8043
Willy Tarreaufa355d42009-11-29 18:12:29 +01008044 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8046 txn->hdr_idx.used--;
8047 cur_hdr->len = 0;
8048 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008049 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 break;
8051
8052 }
8053 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
8055 /* keep the link from this header to next one in case of later
8056 * removal of next header.
8057 */
8058 old_idx = cur_idx;
8059 }
8060 return 0;
8061}
8062
8063
8064/* Apply the filter to the status line in the response buffer <rtr>.
8065 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8066 * or -1 if a replacement resulted in an invalid status line.
8067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008068int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 char *cur_ptr, *cur_end;
8071 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008072 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008073 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074
8075
Willy Tarreau3d300592007-03-18 18:34:41 +01008076 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008078 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 (exp->action == ACT_ALLOW ||
8080 exp->action == ACT_DENY))
8081 return 0;
8082 else if (exp->action == ACT_REMOVE)
8083 return 0;
8084
8085 done = 0;
8086
Willy Tarreau9b28e032012-10-12 23:49:43 +02008087 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008088 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089
8090 /* Now we have the status line between cur_ptr and cur_end */
8091
Willy Tarreau15a53a42015-01-21 13:39:42 +01008092 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 switch (exp->action) {
8094 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008095 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 done = 1;
8097 break;
8098
8099 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008100 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 done = 1;
8102 break;
8103
8104 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008105 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8106 if (trash.len < 0)
8107 return -1;
8108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008109 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 /* FIXME: if the user adds a newline in the replacement, the
8111 * index will not be recalculated for now, and the new line
8112 * will not be counted as a new header.
8113 */
8114
Willy Tarreaufa355d42009-11-29 18:12:29 +01008115 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008117 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008118 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 cur_ptr, cur_end + 1,
8120 NULL, NULL);
8121 if (unlikely(!cur_end))
8122 return -1;
8123
8124 /* we have a full respnse and we know that we have either a CR
8125 * or an LF at <ptr>.
8126 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008128 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 /* there is no point trying this regex on headers */
8130 return 1;
8131 }
8132 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 return done;
8134}
8135
8136
8137
8138/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008139 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8141 * unparsable response.
8142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008143int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144{
Willy Tarreau192252e2015-04-04 01:47:55 +02008145 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008146 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008147 struct hdr_exp *exp;
8148
8149 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 int ret;
8151
8152 /*
8153 * The interleaving of transformations and verdicts
8154 * makes it difficult to decide to continue or stop
8155 * the evaluation.
8156 */
8157
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008158 if (txn->flags & TX_SVDENY)
8159 break;
8160
Willy Tarreau3d300592007-03-18 18:34:41 +01008161 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8163 exp->action == ACT_PASS)) {
8164 exp = exp->next;
8165 continue;
8166 }
8167
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008168 /* if this filter had a condition, evaluate it now and skip to
8169 * next filter if the condition does not match.
8170 */
8171 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008172 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008173 ret = acl_pass(ret);
8174 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8175 ret = !ret;
8176 if (!ret)
8177 continue;
8178 }
8179
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008181 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182 if (unlikely(ret < 0))
8183 return -1;
8184
8185 if (likely(ret == 0)) {
8186 /* The filter did not match the response, it can be
8187 * iterated through all headers.
8188 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008189 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8190 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 }
8193 return 0;
8194}
8195
8196
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008198 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 * desirable to call it only when needed. This function is also used when we
8200 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008202void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008204 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008205 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008206 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 char *hdr_beg, *hdr_end, *hdr_next;
8210 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 /* Iterate through the headers.
8213 * we start with the start line.
8214 */
8215 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008216 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217
8218 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8219 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008220 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
8222 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 hdr_beg = hdr_next;
8224 hdr_end = hdr_beg + cur_hdr->len;
8225 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 /* We have one full header between hdr_beg and hdr_end, and the
8228 * next header starts at hdr_next. We're only interested in
8229 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 */
8231
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 is_cookie2 = 0;
8233 prev = hdr_beg + 10;
8234 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008235 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8237 if (!val) {
8238 old_idx = cur_idx;
8239 continue;
8240 }
8241 is_cookie2 = 1;
8242 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 }
8244
Willy Tarreau24581ba2010-08-31 22:39:35 +02008245 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8246 * <prev> points to the colon.
8247 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008248 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8251 * check-cache is enabled) and we are not interested in checking
8252 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008253 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008254 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 return;
8256
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* OK so now we know we have to process this response cookie.
8258 * The format of the Set-Cookie header is slightly different
8259 * from the format of the Cookie header in that it does not
8260 * support the comma as a cookie delimiter (thus the header
8261 * cannot be folded) because the Expires attribute described in
8262 * the original Netscape's spec may contain an unquoted date
8263 * with a comma inside. We have to live with this because
8264 * many browsers don't support Max-Age and some browsers don't
8265 * support quoted strings. However the Set-Cookie2 header is
8266 * clean.
8267 *
8268 * We have to keep multiple pointers in order to support cookie
8269 * removal at the beginning, middle or end of header without
8270 * corrupting the header (in case of set-cookie2). A special
8271 * pointer, <scav> points to the beginning of the set-cookie-av
8272 * fields after the first semi-colon. The <next> pointer points
8273 * either to the end of line (set-cookie) or next unquoted comma
8274 * (set-cookie2). All of these headers are valid :
8275 *
8276 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8277 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8278 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8279 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8280 * | | | | | | | | | |
8281 * | | | | | | | | +-> next hdr_end <--+
8282 * | | | | | | | +------------> scav
8283 * | | | | | | +--------------> val_end
8284 * | | | | | +--------------------> val_beg
8285 * | | | | +----------------------> equal
8286 * | | | +------------------------> att_end
8287 * | | +----------------------------> att_beg
8288 * | +------------------------------> prev
8289 * +-----------------------------------------> hdr_beg
8290 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008291
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 for (; prev < hdr_end; prev = next) {
8293 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* find att_beg */
8296 att_beg = prev + 1;
8297 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8298 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299
Willy Tarreau24581ba2010-08-31 22:39:35 +02008300 /* find att_end : this is the first character after the last non
8301 * space before the equal. It may be equal to hdr_end.
8302 */
8303 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 while (equal < hdr_end) {
8306 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8307 break;
8308 if (http_is_spht[(unsigned char)*equal++])
8309 continue;
8310 att_end = equal;
8311 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8314 * is between <att_beg> and <equal>, both may be identical.
8315 */
8316
8317 /* look for end of cookie if there is an equal sign */
8318 if (equal < hdr_end && *equal == '=') {
8319 /* look for the beginning of the value */
8320 val_beg = equal + 1;
8321 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8322 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 /* find the end of the value, respecting quotes */
8325 next = find_cookie_value_end(val_beg, hdr_end);
8326
8327 /* make val_end point to the first white space or delimitor after the value */
8328 val_end = next;
8329 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8330 val_end--;
8331 } else {
8332 /* <equal> points to next comma, semi-colon or EOL */
8333 val_beg = val_end = next = equal;
8334 }
8335
8336 if (next < hdr_end) {
8337 /* Set-Cookie2 supports multiple cookies, and <next> points to
8338 * a colon or semi-colon before the end. So skip all attr-value
8339 * pairs and look for the next comma. For Set-Cookie, since
8340 * commas are permitted in values, skip to the end.
8341 */
8342 if (is_cookie2)
8343 next = find_hdr_value_end(next, hdr_end);
8344 else
8345 next = hdr_end;
8346 }
8347
8348 /* Now everything is as on the diagram above */
8349
8350 /* Ignore cookies with no equal sign */
8351 if (equal == val_end)
8352 continue;
8353
8354 /* If there are spaces around the equal sign, we need to
8355 * strip them otherwise we'll get trouble for cookie captures,
8356 * or even for rewrites. Since this happens extremely rarely,
8357 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008359 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8360 int stripped_before = 0;
8361 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 equal += stripped_before;
8366 val_beg += stripped_before;
8367 }
8368
8369 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008370 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 val_beg += stripped_after;
8372 stripped_before += stripped_after;
8373 }
8374
8375 val_end += stripped_before;
8376 next += stripped_before;
8377 hdr_end += stripped_before;
8378 hdr_next += stripped_before;
8379 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008380 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 }
8382
8383 /* First, let's see if we want to capture this cookie. We check
8384 * that we don't already have a server side cookie, because we
8385 * can only capture one. Also as an optimisation, we ignore
8386 * cookies shorter than the declared name.
8387 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008388 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008389 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008390 (val_end - att_beg >= sess->fe->capture_namelen) &&
8391 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008392 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008393 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008394 Alert("HTTP logging : out of memory.\n");
8395 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008396 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008397 if (log_len > sess->fe->capture_len)
8398 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008399 memcpy(txn->srv_cookie, att_beg, log_len);
8400 txn->srv_cookie[log_len] = 0;
8401 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 }
8403
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008404 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008406 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008407 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8408 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008409 /* assume passive cookie by default */
8410 txn->flags &= ~TX_SCK_MASK;
8411 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412
8413 /* If the cookie is in insert mode on a known server, we'll delete
8414 * this occurrence because we'll insert another one later.
8415 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008416 * a direct access.
8417 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008418 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008419 /* The "preserve" flag was set, we don't want to touch the
8420 * server's cookie.
8421 */
8422 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008424 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 /* this cookie must be deleted */
8426 if (*prev == ':' && next == hdr_end) {
8427 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008428 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8430 txn->hdr_idx.used--;
8431 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008432 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008433 hdr_next += delta;
8434 http_msg_move_end(&txn->rsp, delta);
8435 /* note: while both invalid now, <next> and <hdr_end>
8436 * are still equal, so the for() will stop as expected.
8437 */
8438 } else {
8439 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 next = prev;
8442 hdr_end += delta;
8443 hdr_next += delta;
8444 cur_hdr->len += delta;
8445 http_msg_move_end(&txn->rsp, delta);
8446 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008447 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008448 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008449 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008453 * with this server since we know it.
8454 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008455 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 next += delta;
8457 hdr_end += delta;
8458 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008460 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008461
Willy Tarreauf1348312010-10-07 15:54:11 +02008462 txn->flags &= ~TX_SCK_MASK;
8463 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008464 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008467 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008469 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008470 next += delta;
8471 hdr_end += delta;
8472 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008474 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475
Willy Tarreau827aee92011-03-10 16:55:02 +01008476 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008477 txn->flags &= ~TX_SCK_MASK;
8478 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 }
8480 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 /* that's done for this cookie, check the next one on the same
8482 * line when next != hdr_end (only if is_cookie2).
8483 */
8484 }
8485 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488}
8489
8490
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008492 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008494void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008496 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 char *p1, *p2;
8498
8499 char *cur_ptr, *cur_end, *cur_next;
8500 int cur_idx;
8501
Willy Tarreau5df51872007-11-25 16:20:08 +01008502 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 return;
8504
8505 /* Iterate through the headers.
8506 * we start with the start line.
8507 */
8508 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008509 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510
8511 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8512 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008513 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514
8515 cur_hdr = &txn->hdr_idx.v[cur_idx];
8516 cur_ptr = cur_next;
8517 cur_end = cur_ptr + cur_hdr->len;
8518 cur_next = cur_end + cur_hdr->cr + 1;
8519
8520 /* We have one full header between cur_ptr and cur_end, and the
8521 * next header starts at cur_next. We're only interested in
8522 * "Cookie:" headers.
8523 */
8524
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008525 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8526 if (val) {
8527 if ((cur_end - (cur_ptr + val) >= 8) &&
8528 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8529 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8530 return;
8531 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532 }
8533
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008534 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8535 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536 continue;
8537
8538 /* OK, right now we know we have a cache-control header at cur_ptr */
8539
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008540 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 if (p1 >= cur_end) /* no more info */
8543 continue;
8544
8545 /* p1 is at the beginning of the value */
8546 p2 = p1;
8547
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008548 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008549 p2++;
8550
8551 /* we have a complete value between p1 and p2 */
8552 if (p2 < cur_end && *p2 == '=') {
8553 /* we have something of the form no-cache="set-cookie" */
8554 if ((cur_end - p1 >= 21) &&
8555 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8556 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008557 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558 continue;
8559 }
8560
8561 /* OK, so we know that either p2 points to the end of string or to a comma */
8562 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008563 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8565 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8566 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008567 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 return;
8569 }
8570
8571 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008572 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008573 continue;
8574 }
8575 }
8576}
8577
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578
Willy Tarreaub2513902006-12-17 14:52:38 +01008579/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008580 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008581 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008582 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008583 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008584 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008585 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008586 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008587 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008588int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008589{
8590 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008591 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008592 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008593
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008594 if (!uri_auth)
8595 return 0;
8596
Cyril Bonté70be45d2010-10-12 00:14:35 +02008597 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 return 0;
8599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008600 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008601 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008602 return 0;
8603
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008604 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008605 return 0;
8606
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 return 1;
8608}
8609
Willy Tarreau4076a152009-04-02 15:18:36 +02008610/*
8611 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008612 * By default it tries to report the error position as msg->err_pos. However if
8613 * this one is not set, it will then report msg->next, which is the last known
8614 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008615 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008616 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008617void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008618 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008619 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008620{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008621 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008622 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008623 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008624
Willy Tarreau9b28e032012-10-12 23:49:43 +02008625 es->len = MIN(chn->buf->i, sizeof(es->buf));
8626 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008627 len1 = MIN(len1, es->len);
8628 len2 = es->len - len1; /* remaining data if buffer wraps */
8629
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008631 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008632 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008633
Willy Tarreau4076a152009-04-02 15:18:36 +02008634 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008635 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008636 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008637 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008638
Willy Tarreau4076a152009-04-02 15:18:36 +02008639 es->when = date; // user-visible date
8640 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008641 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008642 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008643 if (objt_conn(sess->origin))
8644 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008645 else
8646 memset(&es->src, 0, sizeof(es->src));
8647
Willy Tarreau078272e2010-12-12 12:46:33 +01008648 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008649 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008650 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008651 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008652 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008653 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008654 es->b_out = chn->buf->o;
8655 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008656 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008657 es->m_clen = msg->chunk_len;
8658 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008659}
Willy Tarreaub2513902006-12-17 14:52:38 +01008660
Willy Tarreau294c4732011-12-16 21:35:50 +01008661/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8662 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8663 * performed over the whole headers. Otherwise it must contain a valid header
8664 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8665 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8666 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8667 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008668 * -1. The value fetch stops at commas, so this function is suited for use with
8669 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008670 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008671 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008672unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008673 struct hdr_idx *idx, int occ,
8674 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008675{
Willy Tarreau294c4732011-12-16 21:35:50 +01008676 struct hdr_ctx local_ctx;
8677 char *ptr_hist[MAX_HDR_HISTORY];
8678 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008679 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008681
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 if (!ctx) {
8683 local_ctx.idx = 0;
8684 ctx = &local_ctx;
8685 }
8686
Willy Tarreaubce70882009-09-07 11:51:47 +02008687 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008688 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008689 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008690 occ--;
8691 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 *vptr = ctx->line + ctx->val;
8693 *vlen = ctx->vlen;
8694 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008695 }
8696 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008697 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008698 }
8699
8700 /* negative occurrence, we scan all the list then walk back */
8701 if (-occ > MAX_HDR_HISTORY)
8702 return 0;
8703
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008705 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8707 len_hist[hist_ptr] = ctx->vlen;
8708 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 hist_ptr = 0;
8710 found++;
8711 }
8712 if (-occ > found)
8713 return 0;
8714 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008715 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8716 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8717 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008719 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 if (hist_ptr >= MAX_HDR_HISTORY)
8721 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 *vptr = ptr_hist[hist_ptr];
8723 *vlen = len_hist[hist_ptr];
8724 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008725}
8726
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008727/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8728 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8729 * performed over the whole headers. Otherwise it must contain a valid header
8730 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8731 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8732 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8733 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8734 * -1. This function differs from http_get_hdr() in that it only returns full
8735 * line header values and does not stop at commas.
8736 * The return value is 0 if nothing was found, or non-zero otherwise.
8737 */
8738unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8739 struct hdr_idx *idx, int occ,
8740 struct hdr_ctx *ctx, char **vptr, int *vlen)
8741{
8742 struct hdr_ctx local_ctx;
8743 char *ptr_hist[MAX_HDR_HISTORY];
8744 int len_hist[MAX_HDR_HISTORY];
8745 unsigned int hist_ptr;
8746 int found;
8747
8748 if (!ctx) {
8749 local_ctx.idx = 0;
8750 ctx = &local_ctx;
8751 }
8752
8753 if (occ >= 0) {
8754 /* search from the beginning */
8755 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8756 occ--;
8757 if (occ <= 0) {
8758 *vptr = ctx->line + ctx->val;
8759 *vlen = ctx->vlen;
8760 return 1;
8761 }
8762 }
8763 return 0;
8764 }
8765
8766 /* negative occurrence, we scan all the list then walk back */
8767 if (-occ > MAX_HDR_HISTORY)
8768 return 0;
8769
8770 found = hist_ptr = 0;
8771 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8772 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8773 len_hist[hist_ptr] = ctx->vlen;
8774 if (++hist_ptr >= MAX_HDR_HISTORY)
8775 hist_ptr = 0;
8776 found++;
8777 }
8778 if (-occ > found)
8779 return 0;
8780 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8781 * find occurrence -occ, so we have to check [hist_ptr+occ].
8782 */
8783 hist_ptr += occ;
8784 if (hist_ptr >= MAX_HDR_HISTORY)
8785 hist_ptr -= MAX_HDR_HISTORY;
8786 *vptr = ptr_hist[hist_ptr];
8787 *vlen = len_hist[hist_ptr];
8788 return 1;
8789}
8790
Willy Tarreaubaaee002006-06-26 02:48:02 +02008791/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008792 * Print a debug line with a header. Always stop at the first CR or LF char,
8793 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8794 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008795 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008796void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008797{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008798 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008799 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008800
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008801 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008802 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008803 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008804 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008805
8806 for (max = 0; start + max < end; max++)
8807 if (start[max] == '\r' || start[max] == '\n')
8808 break;
8809
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008810 UBOUND(max, trash.size - trash.len - 3);
8811 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8812 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008813 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008814}
8815
Willy Tarreaueee5b512015-04-03 23:46:31 +02008816
8817/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8818 * The hdr_idx is allocated as well. In case of allocation failure, everything
8819 * allocated is freed and NULL is returned. Otherwise the new transaction is
8820 * assigned to the stream and returned.
8821 */
8822struct http_txn *http_alloc_txn(struct stream *s)
8823{
8824 struct http_txn *txn = s->txn;
8825
8826 if (txn)
8827 return txn;
8828
8829 txn = pool_alloc2(pool2_http_txn);
8830 if (!txn)
8831 return txn;
8832
8833 txn->hdr_idx.size = global.tune.max_http_hdr;
8834 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8835 if (!txn->hdr_idx.v) {
8836 pool_free2(pool2_http_txn, txn);
8837 return NULL;
8838 }
8839
8840 s->txn = txn;
8841 return txn;
8842}
8843
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008845 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846 * the required fields are properly allocated and that we only need to (re)init
8847 * them. This should be used before processing any new request.
8848 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008849void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008850{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008851 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008852 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008853
8854 txn->flags = 0;
8855 txn->status = -1;
8856
Willy Tarreauf64d1412010-10-07 20:06:11 +02008857 txn->cookie_first_date = 0;
8858 txn->cookie_last_date = 0;
8859
Willy Tarreaueee5b512015-04-03 23:46:31 +02008860 txn->srv_cookie = NULL;
8861 txn->cli_cookie = NULL;
8862 txn->uri = NULL;
8863
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008864 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008865 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008866 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008867 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008868 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008869 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008870 txn->req.chunk_len = 0LL;
8871 txn->req.body_len = 0LL;
8872 txn->rsp.chunk_len = 0LL;
8873 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008874 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8875 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008876 txn->req.chn = &s->req;
8877 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008878
8879 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880
8881 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8882 if (fe->options2 & PR_O2_REQBUG_OK)
8883 txn->req.err_pos = -1; /* let buggy requests pass */
8884
Willy Tarreau0937bc42009-12-22 15:03:09 +01008885 if (txn->hdr_idx.v)
8886 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008887
8888 vars_init(&s->vars_txn, SCOPE_TXN);
8889 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008890}
8891
8892/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008893void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008894{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008895 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008896 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897
Willy Tarreau75195602014-03-11 15:48:55 +01008898 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008899 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008900 s->comp_algo->end(&s->comp_ctx);
8901 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008902 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008903
Willy Tarreau0937bc42009-12-22 15:03:09 +01008904 /* these ones will have been dynamically allocated */
8905 pool_free2(pool2_requri, txn->uri);
8906 pool_free2(pool2_capture, txn->cli_cookie);
8907 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008908 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008909
William Lallemanda73203e2012-03-12 12:48:57 +01008910 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008911 txn->uri = NULL;
8912 txn->srv_cookie = NULL;
8913 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008914
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008915 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008916 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008917 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008918 pool_free2(h->pool, s->req_cap[h->index]);
8919 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008920 }
8921
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008922 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008923 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008924 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008925 pool_free2(h->pool, s->res_cap[h->index]);
8926 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008927 }
8928
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008929 vars_prune(&s->vars_txn, s);
8930 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008931}
8932
8933/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008934void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935{
8936 http_end_txn(s);
8937 http_init_txn(s);
8938
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008939 /* reinitialise the current rule list pointer to NULL. We are sure that
8940 * any rulelist match the NULL pointer.
8941 */
8942 s->current_rule_list = NULL;
8943
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008944 s->be = strm_fe(s);
8945 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008946 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008947 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008948 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008949 /* re-init store persistence */
8950 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008951 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008952
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008954
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008955 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956
Willy Tarreau739cfba2010-01-25 23:11:14 +01008957 /* We must trim any excess data from the response buffer, because we
8958 * may have blocked an invalid response from a server that we don't
8959 * want to accidentely forward once we disable the analysers, nor do
8960 * we want those data to come along with next response. A typical
8961 * example of such data would be from a buggy server responding to
8962 * a HEAD with some data, or sending more than the advertised
8963 * content-length.
8964 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008965 if (unlikely(s->res.buf->i))
8966 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008967
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008968 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008969 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008970
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008971 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008972 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008974 s->req.rex = TICK_ETERNITY;
8975 s->req.wex = TICK_ETERNITY;
8976 s->req.analyse_exp = TICK_ETERNITY;
8977 s->res.rex = TICK_ETERNITY;
8978 s->res.wex = TICK_ETERNITY;
8979 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008980}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008981
Sasha Pachev218f0642014-06-16 12:05:59 -06008982void free_http_res_rules(struct list *r)
8983{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008984 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008985
8986 list_for_each_entry_safe(pr, tr, r, list) {
8987 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008988 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008989 free(pr);
8990 }
8991}
8992
8993void free_http_req_rules(struct list *r)
8994{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008995 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008996
8997 list_for_each_entry_safe(pr, tr, r, list) {
8998 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008999 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009000 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009001
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009002 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009003 free(pr);
9004 }
9005}
9006
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009007/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009008struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009009{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009010 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009011 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009012 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009013 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009014
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009015 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009016 if (!rule) {
9017 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009018 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009019 }
9020
CJ Ess108b1dd2015-04-07 12:03:37 -04009021 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009022 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009023 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009024 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009025 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009026 int code;
9027 int hc;
9028
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009029 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009030 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009031 if (strcmp(args[cur_arg], "deny_status") == 0) {
9032 cur_arg++;
9033 if (!args[cur_arg]) {
9034 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9035 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9036 goto out_err;
9037 }
9038
9039 code = atol(args[cur_arg]);
9040 cur_arg++;
9041 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9042 if (http_err_codes[hc] == code) {
9043 rule->deny_status = hc;
9044 break;
9045 }
9046 }
9047
9048 if (hc >= HTTP_ERR_SIZE) {
9049 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9050 file, linenum, code);
9051 }
9052 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009053 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009054 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009055 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009056 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009057 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009058 cur_arg = 1;
9059
9060 while(*args[cur_arg]) {
9061 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009062 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009063 cur_arg+=2;
9064 continue;
9065 } else
9066 break;
9067 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009068 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009069 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009070 cur_arg = 1;
9071
9072 if (!*args[cur_arg] ||
9073 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9074 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9075 file, linenum, args[0]);
9076 goto out_err;
9077 }
9078 rule->arg.nice = atoi(args[cur_arg]);
9079 if (rule->arg.nice < -1024)
9080 rule->arg.nice = -1024;
9081 else if (rule->arg.nice > 1024)
9082 rule->arg.nice = 1024;
9083 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009084 } else if (!strcmp(args[0], "set-tos")) {
9085#ifdef IP_TOS
9086 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009087 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009088 cur_arg = 1;
9089
9090 if (!*args[cur_arg] ||
9091 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9092 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9093 file, linenum, args[0]);
9094 goto out_err;
9095 }
9096
9097 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9098 if (err && *err != '\0') {
9099 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9100 file, linenum, err, args[0]);
9101 goto out_err;
9102 }
9103 cur_arg++;
9104#else
9105 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9106 goto out_err;
9107#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009108 } else if (!strcmp(args[0], "set-mark")) {
9109#ifdef SO_MARK
9110 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009112 cur_arg = 1;
9113
9114 if (!*args[cur_arg] ||
9115 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9116 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9117 file, linenum, args[0]);
9118 goto out_err;
9119 }
9120
9121 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9122 if (err && *err != '\0') {
9123 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9124 file, linenum, err, args[0]);
9125 goto out_err;
9126 }
9127 cur_arg++;
9128 global.last_checks |= LSTCHK_NETADM;
9129#else
9130 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9131 goto out_err;
9132#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009133 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009134 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009135 cur_arg = 1;
9136
9137 if (!*args[cur_arg] ||
9138 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9139 bad_log_level:
9140 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9141 file, linenum, args[0]);
9142 goto out_err;
9143 }
9144 if (strcmp(args[cur_arg], "silent") == 0)
9145 rule->arg.loglevel = -1;
9146 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9147 goto bad_log_level;
9148 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009149 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009150 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009151 cur_arg = 1;
9152
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009153 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9154 (*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 +01009155 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9156 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009157 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009158 }
9159
9160 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9161 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9162 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009163
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009164 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009165 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009166 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9167 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009168 free(proxy->conf.lfs_file);
9169 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9170 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009171 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009172 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009173 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009174 cur_arg = 1;
9175
9176 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009177 (*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 -06009178 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9179 file, linenum, args[0]);
9180 goto out_err;
9181 }
9182
9183 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9184 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9185 LIST_INIT(&rule->arg.hdr_add.fmt);
9186
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009187 error = NULL;
9188 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9189 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9190 args[cur_arg + 1], error);
9191 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009192 goto out_err;
9193 }
9194
9195 proxy->conf.args.ctx = ARGC_HRQ;
9196 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9197 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9198 file, linenum);
9199
9200 free(proxy->conf.lfs_file);
9201 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9202 proxy->conf.lfs_line = proxy->conf.args.line;
9203 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009204 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009205 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009206 cur_arg = 1;
9207
9208 if (!*args[cur_arg] ||
9209 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9210 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9211 file, linenum, args[0]);
9212 goto out_err;
9213 }
9214
9215 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9216 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9217
9218 proxy->conf.args.ctx = ARGC_HRQ;
9219 free(proxy->conf.lfs_file);
9220 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9221 proxy->conf.lfs_line = proxy->conf.args.line;
9222 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009223 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9224 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009225 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009226 struct sample_expr *expr;
9227 unsigned int where;
9228 char *err = NULL;
9229
9230 cur_arg = 1;
9231 proxy->conf.args.ctx = ARGC_TRK;
9232
9233 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9234 if (!expr) {
9235 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9236 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9237 free(err);
9238 goto out_err;
9239 }
9240
9241 where = 0;
9242 if (proxy->cap & PR_CAP_FE)
9243 where |= SMP_VAL_FE_HRQ_HDR;
9244 if (proxy->cap & PR_CAP_BE)
9245 where |= SMP_VAL_BE_HRQ_HDR;
9246
9247 if (!(expr->fetch->val & where)) {
9248 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9249 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9250 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9251 args[cur_arg-1], sample_src_names(expr->fetch->use));
9252 free(expr);
9253 goto out_err;
9254 }
9255
9256 if (strcmp(args[cur_arg], "table") == 0) {
9257 cur_arg++;
9258 if (!args[cur_arg]) {
9259 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9260 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9261 free(expr);
9262 goto out_err;
9263 }
9264 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009265 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009266 cur_arg++;
9267 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009268 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009269 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009270 } else if (strcmp(args[0], "redirect") == 0) {
9271 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009272 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009273
Willy Tarreaube4653b2015-05-28 15:26:58 +02009274 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009275 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9276 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9277 goto out_err;
9278 }
9279
9280 /* this redirect rule might already contain a parsed condition which
9281 * we'll pass to the http-request rule.
9282 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009283 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009284 rule->arg.redir = redir;
9285 rule->cond = redir->cond;
9286 redir->cond = NULL;
9287 cur_arg = 2;
9288 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009289 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9290 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009291 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009292 /*
9293 * '+ 8' for 'add-acl('
9294 * '- 9' for 'add-acl(' + trailing ')'
9295 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009296 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009297
9298 cur_arg = 1;
9299
9300 if (!*args[cur_arg] ||
9301 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9302 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9303 file, linenum, args[0]);
9304 goto out_err;
9305 }
9306
9307 LIST_INIT(&rule->arg.map.key);
9308 proxy->conf.args.ctx = ARGC_HRQ;
9309 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9310 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9311 file, linenum);
9312 free(proxy->conf.lfs_file);
9313 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9314 proxy->conf.lfs_line = proxy->conf.args.line;
9315 cur_arg += 1;
9316 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9317 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009318 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009319 /*
9320 * '+ 8' for 'del-acl('
9321 * '- 9' for 'del-acl(' + trailing ')'
9322 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009323 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009324
9325 cur_arg = 1;
9326
9327 if (!*args[cur_arg] ||
9328 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9329 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9330 file, linenum, args[0]);
9331 goto out_err;
9332 }
9333
9334 LIST_INIT(&rule->arg.map.key);
9335 proxy->conf.args.ctx = ARGC_HRQ;
9336 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9337 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9338 file, linenum);
9339 free(proxy->conf.lfs_file);
9340 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9341 proxy->conf.lfs_line = proxy->conf.args.line;
9342 cur_arg += 1;
9343 } else if (strncmp(args[0], "del-map", 7) == 0) {
9344 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009345 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009346 /*
9347 * '+ 8' for 'del-map('
9348 * '- 9' for 'del-map(' + trailing ')'
9349 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009350 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009351
9352 cur_arg = 1;
9353
9354 if (!*args[cur_arg] ||
9355 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9356 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9357 file, linenum, args[0]);
9358 goto out_err;
9359 }
9360
9361 LIST_INIT(&rule->arg.map.key);
9362 proxy->conf.args.ctx = ARGC_HRQ;
9363 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9364 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9365 file, linenum);
9366 free(proxy->conf.lfs_file);
9367 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9368 proxy->conf.lfs_line = proxy->conf.args.line;
9369 cur_arg += 1;
9370 } else if (strncmp(args[0], "set-map", 7) == 0) {
9371 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009372 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009373 /*
9374 * '+ 8' for 'set-map('
9375 * '- 9' for 'set-map(' + trailing ')'
9376 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009377 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009378
9379 cur_arg = 1;
9380
9381 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9382 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9383 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9384 file, linenum, args[0]);
9385 goto out_err;
9386 }
9387
9388 LIST_INIT(&rule->arg.map.key);
9389 LIST_INIT(&rule->arg.map.value);
9390 proxy->conf.args.ctx = ARGC_HRQ;
9391
9392 /* key pattern */
9393 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9394 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9395 file, linenum);
9396
9397 /* value pattern */
9398 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9399 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9400 file, linenum);
9401 free(proxy->conf.lfs_file);
9402 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9403 proxy->conf.lfs_line = proxy->conf.args.line;
9404
9405 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009406 } else if (strncmp(args[0], "set-src", 7) == 0) {
9407 struct sample_expr *expr;
9408 unsigned int where;
9409 char *err = NULL;
9410
9411 cur_arg = 1;
9412 proxy->conf.args.ctx = ARGC_HRQ;
9413
9414 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9415 if (!expr) {
9416 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9417 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9418 free(err);
9419 goto out_err;
9420 }
9421
9422 where = 0;
9423 if (proxy->cap & PR_CAP_FE)
9424 where |= SMP_VAL_FE_HRQ_HDR;
9425 if (proxy->cap & PR_CAP_BE)
9426 where |= SMP_VAL_BE_HRQ_HDR;
9427
9428 if (!(expr->fetch->val & where)) {
9429 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9430 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9431 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9432 args[cur_arg-1], sample_src_names(expr->fetch->use));
9433 free(expr);
9434 goto out_err;
9435 }
9436
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009437 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009438 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009439 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9440 char *errmsg = NULL;
9441 cur_arg = 1;
9442 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009443 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009444 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009445 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009446 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9447 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9448 free(errmsg);
9449 goto out_err;
9450 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009451 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009452 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', 'set-var', 'set-src', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009453 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009454 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009455 }
9456
9457 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9458 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009459 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009460
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009461 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9462 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9463 file, linenum, args[0], errmsg);
9464 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009465 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009466 }
9467 rule->cond = cond;
9468 }
9469 else if (*args[cur_arg]) {
9470 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9471 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9472 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009473 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009474 }
9475
9476 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009477 out_err:
9478 free(rule);
9479 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009480}
9481
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009482/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009483struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009484{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009485 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009486 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009487 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009488 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009489
9490 rule = calloc(1, sizeof(*rule));
9491 if (!rule) {
9492 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9493 goto out_err;
9494 }
9495
9496 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009497 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009498 cur_arg = 1;
9499 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009500 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009501 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009502 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009503 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009504 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 (integer value).\n",
9509 file, linenum, args[0]);
9510 goto out_err;
9511 }
9512 rule->arg.nice = atoi(args[cur_arg]);
9513 if (rule->arg.nice < -1024)
9514 rule->arg.nice = -1024;
9515 else if (rule->arg.nice > 1024)
9516 rule->arg.nice = 1024;
9517 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009518 } else if (!strcmp(args[0], "set-tos")) {
9519#ifdef IP_TOS
9520 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009521 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009522 cur_arg = 1;
9523
9524 if (!*args[cur_arg] ||
9525 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9526 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9527 file, linenum, args[0]);
9528 goto out_err;
9529 }
9530
9531 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9532 if (err && *err != '\0') {
9533 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9534 file, linenum, err, args[0]);
9535 goto out_err;
9536 }
9537 cur_arg++;
9538#else
9539 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9540 goto out_err;
9541#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009542 } else if (!strcmp(args[0], "set-mark")) {
9543#ifdef SO_MARK
9544 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009545 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009546 cur_arg = 1;
9547
9548 if (!*args[cur_arg] ||
9549 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9550 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9551 file, linenum, args[0]);
9552 goto out_err;
9553 }
9554
9555 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9556 if (err && *err != '\0') {
9557 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9558 file, linenum, err, args[0]);
9559 goto out_err;
9560 }
9561 cur_arg++;
9562 global.last_checks |= LSTCHK_NETADM;
9563#else
9564 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9565 goto out_err;
9566#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009567 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009568 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009569 cur_arg = 1;
9570
9571 if (!*args[cur_arg] ||
9572 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9573 bad_log_level:
9574 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9575 file, linenum, args[0]);
9576 goto out_err;
9577 }
9578 if (strcmp(args[cur_arg], "silent") == 0)
9579 rule->arg.loglevel = -1;
9580 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9581 goto bad_log_level;
9582 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009583 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009584 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009585 cur_arg = 1;
9586
9587 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9588 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9589 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9590 file, linenum, args[0]);
9591 goto out_err;
9592 }
9593
9594 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9595 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9596 LIST_INIT(&rule->arg.hdr_add.fmt);
9597
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009598 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009599 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009600 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9601 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009602 free(proxy->conf.lfs_file);
9603 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9604 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009605 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009606 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009607 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009608 cur_arg = 1;
9609
9610 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009611 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9612 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009613 file, linenum, args[0]);
9614 goto out_err;
9615 }
9616
9617 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9618 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9619 LIST_INIT(&rule->arg.hdr_add.fmt);
9620
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009621 error = NULL;
9622 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9623 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9624 args[cur_arg + 1], error);
9625 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009626 goto out_err;
9627 }
9628
9629 proxy->conf.args.ctx = ARGC_HRQ;
9630 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9631 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9632 file, linenum);
9633
9634 free(proxy->conf.lfs_file);
9635 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9636 proxy->conf.lfs_line = proxy->conf.args.line;
9637 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009638 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009639 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009640 cur_arg = 1;
9641
9642 if (!*args[cur_arg] ||
9643 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9644 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9645 file, linenum, args[0]);
9646 goto out_err;
9647 }
9648
9649 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9650 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9651
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009652 proxy->conf.args.ctx = ARGC_HRS;
9653 free(proxy->conf.lfs_file);
9654 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9655 proxy->conf.lfs_line = proxy->conf.args.line;
9656 cur_arg += 1;
9657 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9658 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009659 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009660 /*
9661 * '+ 8' for 'add-acl('
9662 * '- 9' for 'add-acl(' + trailing ')'
9663 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009664 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009665
9666 cur_arg = 1;
9667
9668 if (!*args[cur_arg] ||
9669 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9670 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9671 file, linenum, args[0]);
9672 goto out_err;
9673 }
9674
9675 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009676 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009677 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9678 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9679 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009680 free(proxy->conf.lfs_file);
9681 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9682 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009683
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009684 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009685 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9686 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009687 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688 /*
9689 * '+ 8' for 'del-acl('
9690 * '- 9' for 'del-acl(' + trailing ')'
9691 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009692 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009693
9694 cur_arg = 1;
9695
9696 if (!*args[cur_arg] ||
9697 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9698 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9699 file, linenum, args[0]);
9700 goto out_err;
9701 }
9702
9703 LIST_INIT(&rule->arg.map.key);
9704 proxy->conf.args.ctx = ARGC_HRS;
9705 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9706 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9707 file, linenum);
9708 free(proxy->conf.lfs_file);
9709 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9710 proxy->conf.lfs_line = proxy->conf.args.line;
9711 cur_arg += 1;
9712 } else if (strncmp(args[0], "del-map", 7) == 0) {
9713 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009714 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009715 /*
9716 * '+ 8' for 'del-map('
9717 * '- 9' for 'del-map(' + trailing ')'
9718 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009719 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009720
9721 cur_arg = 1;
9722
9723 if (!*args[cur_arg] ||
9724 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9725 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9726 file, linenum, args[0]);
9727 goto out_err;
9728 }
9729
9730 LIST_INIT(&rule->arg.map.key);
9731 proxy->conf.args.ctx = ARGC_HRS;
9732 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9733 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9734 file, linenum);
9735 free(proxy->conf.lfs_file);
9736 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9737 proxy->conf.lfs_line = proxy->conf.args.line;
9738 cur_arg += 1;
9739 } else if (strncmp(args[0], "set-map", 7) == 0) {
9740 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009741 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009742 /*
9743 * '+ 8' for 'set-map('
9744 * '- 9' for 'set-map(' + trailing ')'
9745 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009746 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009747
9748 cur_arg = 1;
9749
9750 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9751 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9752 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9753 file, linenum, args[0]);
9754 goto out_err;
9755 }
9756
9757 LIST_INIT(&rule->arg.map.key);
9758 LIST_INIT(&rule->arg.map.value);
9759
9760 proxy->conf.args.ctx = ARGC_HRS;
9761
9762 /* key pattern */
9763 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9764 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9765 file, linenum);
9766
9767 /* value pattern */
9768 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9769 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9770 file, linenum);
9771
9772 free(proxy->conf.lfs_file);
9773 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9774 proxy->conf.lfs_line = proxy->conf.args.line;
9775
9776 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009777 } else if (strcmp(args[0], "redirect") == 0) {
9778 struct redirect_rule *redir;
9779 char *errmsg = NULL;
9780
9781 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9782 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9783 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9784 goto out_err;
9785 }
9786
9787 /* this redirect rule might already contain a parsed condition which
9788 * we'll pass to the http-request rule.
9789 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009790 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009791 rule->arg.redir = redir;
9792 rule->cond = redir->cond;
9793 redir->cond = NULL;
9794 cur_arg = 2;
9795 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009796 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9797 char *errmsg = NULL;
9798 cur_arg = 1;
9799 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009800 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009801 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009802 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009803 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9804 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9805 free(errmsg);
9806 goto out_err;
9807 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009808 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009809 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', 'set-var' but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009810 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9811 goto out_err;
9812 }
9813
9814 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9815 struct acl_cond *cond;
9816 char *errmsg = NULL;
9817
9818 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9819 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9820 file, linenum, args[0], errmsg);
9821 free(errmsg);
9822 goto out_err;
9823 }
9824 rule->cond = cond;
9825 }
9826 else if (*args[cur_arg]) {
9827 Alert("parsing [%s:%d]: 'http-response %s' expects"
9828 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9829 file, linenum, args[0], args[cur_arg]);
9830 goto out_err;
9831 }
9832
9833 return rule;
9834 out_err:
9835 free(rule);
9836 return NULL;
9837}
9838
Willy Tarreau4baae242012-12-27 12:00:31 +01009839/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009840 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009841 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9842 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009843 */
9844struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009845 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009846{
9847 struct redirect_rule *rule;
9848 int cur_arg;
9849 int type = REDIRECT_TYPE_NONE;
9850 int code = 302;
9851 const char *destination = NULL;
9852 const char *cookie = NULL;
9853 int cookie_set = 0;
9854 unsigned int flags = REDIRECT_FLAG_NONE;
9855 struct acl_cond *cond = NULL;
9856
9857 cur_arg = 0;
9858 while (*(args[cur_arg])) {
9859 if (strcmp(args[cur_arg], "location") == 0) {
9860 if (!*args[cur_arg + 1])
9861 goto missing_arg;
9862
9863 type = REDIRECT_TYPE_LOCATION;
9864 cur_arg++;
9865 destination = args[cur_arg];
9866 }
9867 else if (strcmp(args[cur_arg], "prefix") == 0) {
9868 if (!*args[cur_arg + 1])
9869 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009870 type = REDIRECT_TYPE_PREFIX;
9871 cur_arg++;
9872 destination = args[cur_arg];
9873 }
9874 else if (strcmp(args[cur_arg], "scheme") == 0) {
9875 if (!*args[cur_arg + 1])
9876 goto missing_arg;
9877
9878 type = REDIRECT_TYPE_SCHEME;
9879 cur_arg++;
9880 destination = args[cur_arg];
9881 }
9882 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9883 if (!*args[cur_arg + 1])
9884 goto missing_arg;
9885
9886 cur_arg++;
9887 cookie = args[cur_arg];
9888 cookie_set = 1;
9889 }
9890 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9891 if (!*args[cur_arg + 1])
9892 goto missing_arg;
9893
9894 cur_arg++;
9895 cookie = args[cur_arg];
9896 cookie_set = 0;
9897 }
9898 else if (strcmp(args[cur_arg], "code") == 0) {
9899 if (!*args[cur_arg + 1])
9900 goto missing_arg;
9901
9902 cur_arg++;
9903 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009904 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009905 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009906 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009907 args[cur_arg - 1], args[cur_arg]);
9908 return NULL;
9909 }
9910 }
9911 else if (!strcmp(args[cur_arg],"drop-query")) {
9912 flags |= REDIRECT_FLAG_DROP_QS;
9913 }
9914 else if (!strcmp(args[cur_arg],"append-slash")) {
9915 flags |= REDIRECT_FLAG_APPEND_SLASH;
9916 }
9917 else if (strcmp(args[cur_arg], "if") == 0 ||
9918 strcmp(args[cur_arg], "unless") == 0) {
9919 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9920 if (!cond) {
9921 memprintf(errmsg, "error in condition: %s", *errmsg);
9922 return NULL;
9923 }
9924 break;
9925 }
9926 else {
9927 memprintf(errmsg,
9928 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9929 args[cur_arg]);
9930 return NULL;
9931 }
9932 cur_arg++;
9933 }
9934
9935 if (type == REDIRECT_TYPE_NONE) {
9936 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9937 return NULL;
9938 }
9939
Willy Tarreaube4653b2015-05-28 15:26:58 +02009940 if (dir && type != REDIRECT_TYPE_LOCATION) {
9941 memprintf(errmsg, "response only supports redirect type 'location'");
9942 return NULL;
9943 }
9944
Willy Tarreau4baae242012-12-27 12:00:31 +01009945 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9946 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009947 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009948
9949 if (!use_fmt) {
9950 /* old-style static redirect rule */
9951 rule->rdr_str = strdup(destination);
9952 rule->rdr_len = strlen(destination);
9953 }
9954 else {
9955 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009956
9957 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9958 * if prefix == "/", we don't want to add anything, otherwise it
9959 * makes it hard for the user to configure a self-redirection.
9960 */
Godbachd9722032014-12-18 15:44:58 +08009961 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009962 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009963 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009964 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9965 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009966 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009967 free(curproxy->conf.lfs_file);
9968 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9969 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009970 }
9971 }
9972
Willy Tarreau4baae242012-12-27 12:00:31 +01009973 if (cookie) {
9974 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9975 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9976 */
9977 rule->cookie_len = strlen(cookie);
9978 if (cookie_set) {
9979 rule->cookie_str = malloc(rule->cookie_len + 10);
9980 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9981 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9982 rule->cookie_len += 9;
9983 } else {
9984 rule->cookie_str = malloc(rule->cookie_len + 21);
9985 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9986 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9987 rule->cookie_len += 20;
9988 }
9989 }
9990 rule->type = type;
9991 rule->code = code;
9992 rule->flags = flags;
9993 LIST_INIT(&rule->list);
9994 return rule;
9995
9996 missing_arg:
9997 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9998 return NULL;
9999}
10000
Willy Tarreau8797c062007-05-07 00:55:35 +020010001/************************************************************************/
10002/* The code below is dedicated to ACL parsing and matching */
10003/************************************************************************/
10004
10005
Willy Tarreau14174bc2012-04-16 14:34:04 +020010006/* This function ensures that the prerequisites for an L7 fetch are ready,
10007 * which means that a request or response is ready. If some data is missing,
10008 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010009 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10010 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010011 *
10012 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010013 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10014 * decide whether or not an HTTP message is present ;
10015 * 0 if the requested data cannot be fetched or if it is certain that
10016 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010017 * 1 if an HTTP message is ready
10018 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010019int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010020 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010021{
Willy Tarreau192252e2015-04-04 01:47:55 +020010022 struct http_txn *txn;
10023 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010024
Willy Tarreaueee5b512015-04-03 23:46:31 +020010025 /* Note: this function may only be used from places where
10026 * http_init_txn() has already been done, and implies that <s>,
10027 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10028 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010029 */
10030
Willy Tarreau192252e2015-04-04 01:47:55 +020010031 if (!s)
10032 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010033 if (!s->txn) {
10034 if (unlikely(!http_alloc_txn(s)))
10035 return 0; /* not enough memory */
10036 http_init_txn(s);
10037 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010038 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010039 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010040
10041 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010042 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010043
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010044 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010045 /* If the buffer does not leave enough free space at the end,
10046 * we must first realign it.
10047 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010048 if (s->req.buf->p > s->req.buf->data &&
10049 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10050 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010051
Willy Tarreau14174bc2012-04-16 14:34:04 +020010052 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010053 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010054 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010055
10056 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010057 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010058 http_msg_analyzer(msg, &txn->hdr_idx);
10059
10060 /* Still no valid request ? */
10061 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010062 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010063 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010064 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010065 }
10066 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010067 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010068 return 0;
10069 }
10070
10071 /* OK we just got a valid HTTP request. We have some minor
10072 * preparation to perform so that further checks can rely
10073 * on HTTP tests.
10074 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010075
10076 /* If the request was parsed but was too large, we must absolutely
10077 * return an error so that it is not processed. At the moment this
10078 * cannot happen, but if the parsers are to change in the future,
10079 * we want this check to be maintained.
10080 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010081 if (unlikely(s->req.buf->i + s->req.buf->p >
10082 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010083 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010084 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010085 return 1;
10086 }
10087
Willy Tarreau9b28e032012-10-12 23:49:43 +020010088 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010089 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010090 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010091
Willy Tarreau506d0502013-07-06 13:29:24 +020010092 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10093 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010094 }
10095
Willy Tarreau506d0502013-07-06 13:29:24 +020010096 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010097 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010098 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010099
10100 /* otherwise everything's ready for the request */
10101 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010102 else {
10103 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010104 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10105 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010107 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010108 }
10109
10110 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010111 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010112 return 1;
10113}
Willy Tarreau8797c062007-05-07 00:55:35 +020010114
Willy Tarreau8797c062007-05-07 00:55:35 +020010115/* 1. Check on METHOD
10116 * We use the pre-parsed method if it is known, and store its number as an
10117 * integer. If it is unknown, we use the pointer and the length.
10118 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010119static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010120{
10121 int len, meth;
10122
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010123 len = strlen(text);
10124 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010125
10126 pattern->val.i = meth;
10127 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010128 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010129 pattern->len = len;
10130 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010131 else {
10132 pattern->ptr.str = NULL;
10133 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010134 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010135 return 1;
10136}
10137
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010138/* This function fetches the method of current HTTP request and stores
10139 * it in the global pattern struct as a chunk. There are two possibilities :
10140 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10141 * in <len> and <ptr> is NULL ;
10142 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10143 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010144 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010145 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010146static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010147smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010148{
10149 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010150 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010151
Willy Tarreau24e32d82012-04-23 23:55:44 +020010152 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010153
Willy Tarreau8797c062007-05-07 00:55:35 +020010154 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010155 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010156 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010157 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010158 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10159 /* ensure the indexes are not affected */
10160 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010161 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010162 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10163 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010164 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010165 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010166 return 1;
10167}
10168
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010169/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010170static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010171{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010172 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010173 struct pattern_list *lst;
10174 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010175
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010176 list_for_each_entry(lst, &expr->patterns, list) {
10177 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010178
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010179 /* well-known method */
10180 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010181 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010182 return pattern;
10183 else
10184 continue;
10185 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010186
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010187 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010188 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010189 continue;
10190
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010191 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010192 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10193 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010194 return pattern;
10195 }
10196 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010197}
10198
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010199static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010200smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010201{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010202 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010203 char *ptr;
10204 int len;
10205
Willy Tarreauc0239e02012-04-16 14:42:55 +020010206 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010207
Willy Tarreau8797c062007-05-07 00:55:35 +020010208 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010209 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210
10211 while ((len-- > 0) && (*ptr++ != '/'));
10212 if (len <= 0)
10213 return 0;
10214
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010215 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010216 smp->data.u.str.str = ptr;
10217 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010218
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010219 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010220 return 1;
10221}
10222
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010223static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010224smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010225{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010226 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010227 char *ptr;
10228 int len;
10229
Willy Tarreauc0239e02012-04-16 14:42:55 +020010230 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010231
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010232 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010233 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10234 return 0;
10235
Willy Tarreau8797c062007-05-07 00:55:35 +020010236 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010237 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010238
10239 while ((len-- > 0) && (*ptr++ != '/'));
10240 if (len <= 0)
10241 return 0;
10242
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010243 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010244 smp->data.u.str.str = ptr;
10245 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010246
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010247 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010248 return 1;
10249}
10250
10251/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010252static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010253smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010254{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010255 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010256 char *ptr;
10257 int len;
10258
Willy Tarreauc0239e02012-04-16 14:42:55 +020010259 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010260
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010261 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010262 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10263 return 0;
10264
Willy Tarreau8797c062007-05-07 00:55:35 +020010265 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010266 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010268 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010269 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010270 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010271 return 1;
10272}
10273
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010274/* returns the longest available part of the body. This requires that the body
10275 * has been waited for using http-buffer-request.
10276 */
10277static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010278smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010279{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010280 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010281 struct http_msg *msg;
10282 unsigned long len;
10283 unsigned long block1;
10284 char *body;
10285 struct chunk *temp;
10286
10287 CHECK_HTTP_MESSAGE_FIRST();
10288
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010290 msg = &txn->req;
10291 else
10292 msg = &txn->rsp;
10293
10294 len = http_body_bytes(msg);
10295 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10296
10297 block1 = len;
10298 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10299 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10300
10301 if (block1 == len) {
10302 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010303 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010304 smp->data.u.str.str = body;
10305 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010306 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10307 }
10308 else {
10309 /* buffer is wrapped, we need to defragment it */
10310 temp = get_trash_chunk();
10311 memcpy(temp->str, body, block1);
10312 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010313 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010314 smp->data.u.str.str = temp->str;
10315 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010316 smp->flags = SMP_F_VOL_TEST;
10317 }
10318 return 1;
10319}
10320
10321
10322/* returns the available length of the body. This requires that the body
10323 * has been waited for using http-buffer-request.
10324 */
10325static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010326smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010327{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010328 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010329 struct http_msg *msg;
10330
10331 CHECK_HTTP_MESSAGE_FIRST();
10332
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010333 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010334 msg = &txn->req;
10335 else
10336 msg = &txn->rsp;
10337
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010338 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010339 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010340
10341 smp->flags = SMP_F_VOL_TEST;
10342 return 1;
10343}
10344
10345
10346/* returns the advertised length of the body, or the advertised size of the
10347 * chunks available in the buffer. This requires that the body has been waited
10348 * for using http-buffer-request.
10349 */
10350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010352{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010353 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010354 struct http_msg *msg;
10355
10356 CHECK_HTTP_MESSAGE_FIRST();
10357
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010358 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010359 msg = &txn->req;
10360 else
10361 msg = &txn->rsp;
10362
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010363 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010364 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010365
10366 smp->flags = SMP_F_VOL_TEST;
10367 return 1;
10368}
10369
10370
Willy Tarreau8797c062007-05-07 00:55:35 +020010371/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010372static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010373smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010374{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010375 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010376
Willy Tarreauc0239e02012-04-16 14:42:55 +020010377 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010379 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010380 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010381 smp->data.u.str.len = txn->req.sl.rq.u_l;
10382 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010383 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010384 return 1;
10385}
10386
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010387static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010388smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010389{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010390 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010391 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010392
Willy Tarreauc0239e02012-04-16 14:42:55 +020010393 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010394
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010395 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010396 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 +020010397 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010398 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010400 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010401 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010402 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010403 return 1;
10404}
10405
10406static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010407smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010408{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010409 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010410 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010411
Willy Tarreauc0239e02012-04-16 14:42:55 +020010412 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010413
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010414 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010415 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 +020010416 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10417 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010419 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010420 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010421 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010422 return 1;
10423}
10424
Willy Tarreau185b5c42012-04-26 15:11:51 +020010425/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10426 * Accepts an optional argument of type string containing the header field name,
10427 * and an optional argument of type signed or unsigned integer to request an
10428 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010429 * headers are considered from the first one. It does not stop on commas and
10430 * returns full lines instead (useful for User-Agent or Date for example).
10431 */
10432static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010433smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010434{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010435 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010436 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010437 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010438 int occ = 0;
10439 const char *name_str = NULL;
10440 int name_len = 0;
10441
10442 if (!ctx) {
10443 /* first call */
10444 ctx = &static_hdr_ctx;
10445 ctx->idx = 0;
10446 smp->ctx.a[0] = ctx;
10447 }
10448
10449 if (args) {
10450 if (args[0].type != ARGT_STR)
10451 return 0;
10452 name_str = args[0].data.str.str;
10453 name_len = args[0].data.str.len;
10454
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010455 if (args[1].type == ARGT_SINT)
10456 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010457 }
10458
10459 CHECK_HTTP_MESSAGE_FIRST();
10460
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010461 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010462 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010463
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010464 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10465 /* search for header from the beginning */
10466 ctx->idx = 0;
10467
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010468 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010469 /* no explicit occurrence and single fetch => last header by default */
10470 occ = -1;
10471
10472 if (!occ)
10473 /* prepare to report multiple occurrences for ACL fetches */
10474 smp->flags |= SMP_F_NOT_LAST;
10475
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010476 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010477 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010478 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010479 return 1;
10480
10481 smp->flags &= ~SMP_F_NOT_LAST;
10482 return 0;
10483}
10484
10485/* 6. Check on HTTP header count. The number of occurrences is returned.
10486 * Accepts exactly 1 argument of type string. It does not stop on commas and
10487 * returns full lines instead (useful for User-Agent or Date for example).
10488 */
10489static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010490smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010491{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010492 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010493 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010494 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010495 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010496 const char *name = NULL;
10497 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010498
Willy Tarreau601a4d12015-04-01 19:16:09 +020010499 if (args && args->type == ARGT_STR) {
10500 name = args->data.str.str;
10501 len = args->data.str.len;
10502 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010503
10504 CHECK_HTTP_MESSAGE_FIRST();
10505
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010506 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010507 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010508
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010509 ctx.idx = 0;
10510 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010511 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010512 cnt++;
10513
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010514 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010515 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010516 smp->flags = SMP_F_VOL_HDR;
10517 return 1;
10518}
10519
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010520static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010521smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010522{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010523 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010524 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010525 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010526 struct chunk *temp;
10527 char del = ',';
10528
10529 if (args && args->type == ARGT_STR)
10530 del = *args[0].data.str.str;
10531
10532 CHECK_HTTP_MESSAGE_FIRST();
10533
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010534 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010535 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010536
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010537 temp = get_trash_chunk();
10538
10539 ctx.idx = 0;
10540 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10541 if (temp->len)
10542 temp->str[temp->len++] = del;
10543 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10544 temp->len += ctx.del;
10545 }
10546
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010547 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010548 smp->data.u.str.str = temp->str;
10549 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010550 smp->flags = SMP_F_VOL_HDR;
10551 return 1;
10552}
10553
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010554/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10555 * Accepts an optional argument of type string containing the header field name,
10556 * and an optional argument of type signed or unsigned integer to request an
10557 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010558 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010559 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010560static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010561smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010562{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010563 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010564 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010565 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010566 int occ = 0;
10567 const char *name_str = NULL;
10568 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010569
Willy Tarreaua890d072013-04-02 12:01:06 +020010570 if (!ctx) {
10571 /* first call */
10572 ctx = &static_hdr_ctx;
10573 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010574 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010575 }
10576
Willy Tarreau185b5c42012-04-26 15:11:51 +020010577 if (args) {
10578 if (args[0].type != ARGT_STR)
10579 return 0;
10580 name_str = args[0].data.str.str;
10581 name_len = args[0].data.str.len;
10582
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010583 if (args[1].type == ARGT_SINT)
10584 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010585 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010586
Willy Tarreaue333ec92012-04-16 16:26:40 +020010587 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010588
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010589 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010590 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010591
Willy Tarreau185b5c42012-04-26 15:11:51 +020010592 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010593 /* search for header from the beginning */
10594 ctx->idx = 0;
10595
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010596 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010597 /* no explicit occurrence and single fetch => last header by default */
10598 occ = -1;
10599
10600 if (!occ)
10601 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010602 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010603
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010604 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010605 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010606 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010608
Willy Tarreau37406352012-04-23 16:16:37 +020010609 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010610 return 0;
10611}
10612
Willy Tarreauc11416f2007-06-17 16:58:38 +020010613/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010614 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010615 */
10616static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010617smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010618{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010619 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010620 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010621 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010622 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010623 const char *name = NULL;
10624 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010625
Willy Tarreau601a4d12015-04-01 19:16:09 +020010626 if (args && args->type == ARGT_STR) {
10627 name = args->data.str.str;
10628 len = args->data.str.len;
10629 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010630
Willy Tarreaue333ec92012-04-16 16:26:40 +020010631 CHECK_HTTP_MESSAGE_FIRST();
10632
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010633 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010634 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010635
Willy Tarreau33a7e692007-06-10 19:45:56 +020010636 ctx.idx = 0;
10637 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010638 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010639 cnt++;
10640
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010641 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010642 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010643 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010644 return 1;
10645}
10646
Willy Tarreau185b5c42012-04-26 15:11:51 +020010647/* Fetch an HTTP header's integer value. The integer value is returned. It
10648 * takes a mandatory argument of type string and an optional one of type int
10649 * to designate a specific occurrence. It returns an unsigned integer, which
10650 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010651 */
10652static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010653smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010654{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010655 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010656
Willy Tarreauf853c462012-04-23 18:53:56 +020010657 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010658 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010659 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010660 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010661
Willy Tarreaud53e2422012-04-16 17:21:11 +020010662 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010663}
10664
Cyril Bonté69fa9922012-10-25 00:01:06 +020010665/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10666 * and an optional one of type int to designate a specific occurrence.
10667 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010668 */
10669static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010670smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010671{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010672 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010673
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010674 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010675 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010676 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010677 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010678 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010679 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010680 if (smp->data.u.str.len < temp->size - 1) {
10681 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10682 temp->str[smp->data.u.str.len] = '\0';
10683 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010684 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010685 break;
10686 }
10687 }
10688 }
10689
Willy Tarreaud53e2422012-04-16 17:21:11 +020010690 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010691 if (!(smp->flags & SMP_F_NOT_LAST))
10692 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010693 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010694 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010695}
10696
Willy Tarreau737b0c12007-06-10 21:28:46 +020010697/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10698 * the first '/' after the possible hostname, and ends before the possible '?'.
10699 */
10700static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010701smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010702{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010703 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010704 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010705
Willy Tarreauc0239e02012-04-16 14:42:55 +020010706 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010707
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010708 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010709 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010710 ptr = http_get_path(txn);
10711 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010712 return 0;
10713
10714 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010715 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010716 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010717
10718 while (ptr < end && *ptr != '?')
10719 ptr++;
10720
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010721 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010722 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010723 return 1;
10724}
10725
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010726/* This produces a concatenation of the first occurrence of the Host header
10727 * followed by the path component if it begins with a slash ('/'). This means
10728 * that '*' will not be added, resulting in exactly the first Host entry.
10729 * If no Host header is found, then the path is returned as-is. The returned
10730 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010731 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010732 */
10733static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010734smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010735{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010736 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010737 char *ptr, *end, *beg;
10738 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010739 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010740
10741 CHECK_HTTP_MESSAGE_FIRST();
10742
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010743 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010744 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010745 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010746 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010747
10748 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010749 temp = get_trash_chunk();
10750 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010751 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010752 smp->data.u.str.str = temp->str;
10753 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010754
10755 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010756 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010757 beg = http_get_path(txn);
10758 if (!beg)
10759 beg = end;
10760
10761 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10762
10763 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010764 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10765 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010766 }
10767
10768 smp->flags = SMP_F_VOL_1ST;
10769 return 1;
10770}
10771
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010772/* This produces a 32-bit hash of the concatenation of the first occurrence of
10773 * the Host header followed by the path component if it begins with a slash ('/').
10774 * This means that '*' will not be added, resulting in exactly the first Host
10775 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010776 * is hashed using the path hash followed by a full avalanche hash and provides a
10777 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010778 * high-traffic sites without having to store whole paths.
10779 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010780int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010781smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010782{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010783 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010784 struct hdr_ctx ctx;
10785 unsigned int hash = 0;
10786 char *ptr, *beg, *end;
10787 int len;
10788
10789 CHECK_HTTP_MESSAGE_FIRST();
10790
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010791 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010792 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010793 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010794 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10795 ptr = ctx.line + ctx.val;
10796 len = ctx.vlen;
10797 while (len--)
10798 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10799 }
10800
10801 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010802 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010803 beg = http_get_path(txn);
10804 if (!beg)
10805 beg = end;
10806
10807 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10808
10809 if (beg < ptr && *beg == '/') {
10810 while (beg < ptr)
10811 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10812 }
10813 hash = full_hash(hash);
10814
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010815 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010816 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010817 smp->flags = SMP_F_VOL_1ST;
10818 return 1;
10819}
10820
Willy Tarreau4a550602012-12-09 14:53:32 +010010821/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010822 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10823 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10824 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010825 * that in environments where IPv6 is insignificant, truncating the output to
10826 * 8 bytes would still work.
10827 */
10828static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010829smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010830{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010831 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010832 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010833
10834 if (!cli_conn)
10835 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010836
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010837 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010838 return 0;
10839
Willy Tarreau47ca5452012-12-23 20:22:19 +010010840 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010841 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010842 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010843
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010844 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010845 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010846 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010847 temp->len += 4;
10848 break;
10849 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010850 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010851 temp->len += 16;
10852 break;
10853 default:
10854 return 0;
10855 }
10856
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010857 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010858 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010859 return 1;
10860}
10861
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010862/* Extracts the query string, which comes after the question mark '?'. If no
10863 * question mark is found, nothing is returned. Otherwise it returns a sample
10864 * of type string carrying the whole query string.
10865 */
10866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010867smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010868{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010869 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010870 char *ptr, *end;
10871
10872 CHECK_HTTP_MESSAGE_FIRST();
10873
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010874 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010875 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10876 end = ptr + txn->req.sl.rq.u_l;
10877
10878 /* look up the '?' */
10879 do {
10880 if (ptr == end)
10881 return 0;
10882 } while (*ptr++ != '?');
10883
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010884 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010885 smp->data.u.str.str = ptr;
10886 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010887 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10888 return 1;
10889}
10890
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010892smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010893{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010894 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10895 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10896 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010897
Willy Tarreau24e32d82012-04-23 23:55:44 +020010898 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010899
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010900 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010901 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010902 return 1;
10903}
10904
Willy Tarreau7f18e522010-10-22 20:04:13 +020010905/* return a valid test if the current request is the first one on the connection */
10906static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010907smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010908{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010909 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010910 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010911 return 1;
10912}
10913
Willy Tarreau34db1082012-04-19 17:16:54 +020010914/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010915static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010916smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010917{
10918
Willy Tarreau24e32d82012-04-23 23:55:44 +020010919 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010920 return 0;
10921
Willy Tarreauc0239e02012-04-16 14:42:55 +020010922 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010924 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010925 return 0;
10926
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010927 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010928 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010929 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010930 return 1;
10931}
Willy Tarreau8797c062007-05-07 00:55:35 +020010932
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010933/* Accepts exactly 1 argument of type userlist */
10934static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010935smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010936{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010937 if (!args || args->type != ARGT_USR)
10938 return 0;
10939
10940 CHECK_HTTP_MESSAGE_FIRST();
10941
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010942 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010943 return 0;
10944
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010945 /* if the user does not belong to the userlist or has a wrong password,
10946 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010947 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010948 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010949 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10950 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010951 return 0;
10952
10953 /* pat_match_auth() will need the user list */
10954 smp->ctx.a[0] = args->data.usr;
10955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010956 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010957 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010958 smp->data.u.str.str = smp->strm->txn->auth.user;
10959 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010960
10961 return 1;
10962}
10963
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010964/* Try to find the next occurrence of a cookie name in a cookie header value.
10965 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10966 * the cookie value is returned into *value and *value_l, and the function
10967 * returns a pointer to the next pointer to search from if the value was found.
10968 * Otherwise if the cookie was not found, NULL is returned and neither value
10969 * nor value_l are touched. The input <hdr> string should first point to the
10970 * header's value, and the <hdr_end> pointer must point to the first character
10971 * not part of the value. <list> must be non-zero if value may represent a list
10972 * of values (cookie headers). This makes it faster to abort parsing when no
10973 * list is expected.
10974 */
10975static char *
10976extract_cookie_value(char *hdr, const char *hdr_end,
10977 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010978 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010979{
10980 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10981 char *next;
10982
10983 /* we search at least a cookie name followed by an equal, and more
10984 * generally something like this :
10985 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10986 */
10987 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10988 /* Iterate through all cookies on this line */
10989
10990 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10991 att_beg++;
10992
10993 /* find att_end : this is the first character after the last non
10994 * space before the equal. It may be equal to hdr_end.
10995 */
10996 equal = att_end = att_beg;
10997
10998 while (equal < hdr_end) {
10999 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11000 break;
11001 if (http_is_spht[(unsigned char)*equal++])
11002 continue;
11003 att_end = equal;
11004 }
11005
11006 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11007 * is between <att_beg> and <equal>, both may be identical.
11008 */
11009
11010 /* look for end of cookie if there is an equal sign */
11011 if (equal < hdr_end && *equal == '=') {
11012 /* look for the beginning of the value */
11013 val_beg = equal + 1;
11014 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11015 val_beg++;
11016
11017 /* find the end of the value, respecting quotes */
11018 next = find_cookie_value_end(val_beg, hdr_end);
11019
11020 /* make val_end point to the first white space or delimitor after the value */
11021 val_end = next;
11022 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11023 val_end--;
11024 } else {
11025 val_beg = val_end = next = equal;
11026 }
11027
11028 /* We have nothing to do with attributes beginning with '$'. However,
11029 * they will automatically be removed if a header before them is removed,
11030 * since they're supposed to be linked together.
11031 */
11032 if (*att_beg == '$')
11033 continue;
11034
11035 /* Ignore cookies with no equal sign */
11036 if (equal == next)
11037 continue;
11038
11039 /* Now we have the cookie name between att_beg and att_end, and
11040 * its value between val_beg and val_end.
11041 */
11042
11043 if (att_end - att_beg == cookie_name_l &&
11044 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11045 /* let's return this value and indicate where to go on from */
11046 *value = val_beg;
11047 *value_l = val_end - val_beg;
11048 return next + 1;
11049 }
11050
11051 /* Set-Cookie headers only have the name in the first attr=value part */
11052 if (!list)
11053 break;
11054 }
11055
11056 return NULL;
11057}
11058
William Lallemanda43ba4e2014-01-28 18:14:25 +010011059/* Fetch a captured HTTP request header. The index is the position of
11060 * the "capture" option in the configuration file
11061 */
11062static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011063smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011064{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011065 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011066 int idx;
11067
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011068 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011069 return 0;
11070
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011071 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011072
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011073 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011074 return 0;
11075
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011076 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011077 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011078 smp->data.u.str.str = smp->strm->req_cap[idx];
11079 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011080
11081 return 1;
11082}
11083
11084/* Fetch a captured HTTP response header. The index is the position of
11085 * the "capture" option in the configuration file
11086 */
11087static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011088smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011089{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011090 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011091 int idx;
11092
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011093 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011094 return 0;
11095
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011096 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011097
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011098 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011099 return 0;
11100
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011101 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011102 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011103 smp->data.u.str.str = smp->strm->res_cap[idx];
11104 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011105
11106 return 1;
11107}
11108
William Lallemand65ad6e12014-01-31 15:08:02 +010011109/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11110static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011111smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011112{
11113 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011114 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011115 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011116
Willy Tarreau15e91e12015-04-04 00:52:09 +020011117 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011118 return 0;
11119
William Lallemand96a77852014-02-05 00:30:02 +010011120 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011121
William Lallemand96a77852014-02-05 00:30:02 +010011122 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11123 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011124
William Lallemand96a77852014-02-05 00:30:02 +010011125 temp = get_trash_chunk();
11126 temp->str = txn->uri;
11127 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011128 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011129 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011130 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011131
11132 return 1;
11133
11134}
11135
11136/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011138smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011139{
11140 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011141 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011142 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011143
Willy Tarreau15e91e12015-04-04 00:52:09 +020011144 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011145 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011146
William Lallemand65ad6e12014-01-31 15:08:02 +010011147 ptr = txn->uri;
11148
11149 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11150 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011151
William Lallemand65ad6e12014-01-31 15:08:02 +010011152 if (!*ptr)
11153 return 0;
11154
11155 ptr++; /* skip the space */
11156
11157 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011158 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011159 if (!ptr)
11160 return 0;
11161 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11162 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011163
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011164 smp->data.u.str = *temp;
11165 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011166 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011167 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011168
11169 return 1;
11170}
11171
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011172/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11173 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11174 */
11175static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011176smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011177{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011178 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011179
Willy Tarreau15e91e12015-04-04 00:52:09 +020011180 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011181 return 0;
11182
11183 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011184 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011185 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011186 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011187
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011188 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011189 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011190 smp->flags = SMP_F_CONST;
11191 return 1;
11192
11193}
11194
11195/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11196 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11197 */
11198static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011199smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011200{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011201 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011202
Willy Tarreau15e91e12015-04-04 00:52:09 +020011203 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011204 return 0;
11205
11206 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011207 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011208 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011209 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011210
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011211 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011212 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011213 smp->flags = SMP_F_CONST;
11214 return 1;
11215
11216}
11217
William Lallemand65ad6e12014-01-31 15:08:02 +010011218
Willy Tarreaue333ec92012-04-16 16:26:40 +020011219/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011220 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011221 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011222 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011223 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011224 * Accepts exactly 1 argument of type string. If the input options indicate
11225 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011226 * The returned sample is of type CSTR. Can be used to parse cookies in other
11227 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011228 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011229int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011230{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011231 struct http_txn *txn;
11232 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011233 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011234 const struct http_msg *msg;
11235 const char *hdr_name;
11236 int hdr_name_len;
11237 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011238 int occ = 0;
11239 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011240
Willy Tarreau24e32d82012-04-23 23:55:44 +020011241 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011242 return 0;
11243
Willy Tarreaua890d072013-04-02 12:01:06 +020011244 if (!ctx) {
11245 /* first call */
11246 ctx = &static_hdr_ctx;
11247 ctx->idx = 0;
11248 smp->ctx.a[2] = ctx;
11249 }
11250
Willy Tarreaue333ec92012-04-16 16:26:40 +020011251 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011252
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011253 txn = smp->strm->txn;
11254 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011255
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011256 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011257 msg = &txn->req;
11258 hdr_name = "Cookie";
11259 hdr_name_len = 6;
11260 } else {
11261 msg = &txn->rsp;
11262 hdr_name = "Set-Cookie";
11263 hdr_name_len = 10;
11264 }
11265
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011266 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011267 /* no explicit occurrence and single fetch => last cookie by default */
11268 occ = -1;
11269
11270 /* OK so basically here, either we want only one value and it's the
11271 * last one, or we want to iterate over all of them and we fetch the
11272 * next one.
11273 */
11274
Willy Tarreau9b28e032012-10-12 23:49:43 +020011275 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011276 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011277 /* search for the header from the beginning, we must first initialize
11278 * the search parameters.
11279 */
Willy Tarreau37406352012-04-23 16:16:37 +020011280 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011281 ctx->idx = 0;
11282 }
11283
Willy Tarreau28376d62012-04-26 21:26:10 +020011284 smp->flags |= SMP_F_VOL_HDR;
11285
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011286 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011287 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11288 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011289 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11290 goto out;
11291
Willy Tarreau24e32d82012-04-23 23:55:44 +020011292 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011293 continue;
11294
Willy Tarreau37406352012-04-23 16:16:37 +020011295 smp->ctx.a[0] = ctx->line + ctx->val;
11296 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011297 }
11298
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011299 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011300 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011301 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011302 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011303 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011304 &smp->data.u.str.str,
11305 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011306 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011307 found = 1;
11308 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011309 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011310 smp->flags |= SMP_F_NOT_LAST;
11311 return 1;
11312 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011313 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011314 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011315 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011316 /* all cookie headers and values were scanned. If we're looking for the
11317 * last occurrence, we may return it now.
11318 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011319 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011320 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011321 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011322}
11323
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011325 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011326 * multiple cookies may be parsed on the same line. The returned sample is of
11327 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011328 */
11329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011330smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011331{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011332 struct http_txn *txn;
11333 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011334 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011335 const struct http_msg *msg;
11336 const char *hdr_name;
11337 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011338 int cnt;
11339 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011340 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011341
Willy Tarreau24e32d82012-04-23 23:55:44 +020011342 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011343 return 0;
11344
Willy Tarreaue333ec92012-04-16 16:26:40 +020011345 CHECK_HTTP_MESSAGE_FIRST();
11346
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011347 txn = smp->strm->txn;
11348 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011349
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011350 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011351 msg = &txn->req;
11352 hdr_name = "Cookie";
11353 hdr_name_len = 6;
11354 } else {
11355 msg = &txn->rsp;
11356 hdr_name = "Set-Cookie";
11357 hdr_name_len = 10;
11358 }
11359
Willy Tarreau9b28e032012-10-12 23:49:43 +020011360 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011361 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011362 ctx.idx = 0;
11363 cnt = 0;
11364
11365 while (1) {
11366 /* Note: val_beg == NULL every time we need to fetch a new header */
11367 if (!val_beg) {
11368 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11369 break;
11370
Willy Tarreau24e32d82012-04-23 23:55:44 +020011371 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011372 continue;
11373
11374 val_beg = ctx.line + ctx.val;
11375 val_end = val_beg + ctx.vlen;
11376 }
11377
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011378 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011379 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011380 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011381 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011382 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011383 &smp->data.u.str.str,
11384 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011385 cnt++;
11386 }
11387 }
11388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011389 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011390 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011391 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011392 return 1;
11393}
11394
Willy Tarreau51539362012-05-08 12:46:28 +020011395/* Fetch an cookie's integer value. The integer value is returned. It
11396 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11397 */
11398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011399smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011400{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011401 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011402
11403 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011404 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011405 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011406 }
11407
11408 return ret;
11409}
11410
Willy Tarreau8797c062007-05-07 00:55:35 +020011411/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011412/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011413/************************************************************************/
11414
David Cournapeau16023ee2010-12-23 20:55:41 +090011415/*
11416 * Given a path string and its length, find the position of beginning of the
11417 * query string. Returns NULL if no query string is found in the path.
11418 *
11419 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11420 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011421 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011422 */
bedis4c75cca2012-10-05 08:38:24 +020011423static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011424{
11425 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011426
bedis4c75cca2012-10-05 08:38:24 +020011427 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011428 return p ? p + 1 : NULL;
11429}
11430
bedis4c75cca2012-10-05 08:38:24 +020011431static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011432{
bedis4c75cca2012-10-05 08:38:24 +020011433 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011434}
11435
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011436/* after increasing a pointer value, it can exceed the first buffer
11437 * size. This function transform the value of <ptr> according with
11438 * the expected position. <chunks> is an array of the one or two
11439 * avalaible chunks. The first value is the start of the first chunk,
11440 * the second value if the end+1 of the first chunks. The third value
11441 * is NULL or the start of the second chunk and the fourth value is
11442 * the end+1 of the second chunk. The function returns 1 if does a
11443 * wrap, else returns 0.
11444 */
11445static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11446{
11447 if (*ptr < chunks[1])
11448 return 0;
11449 if (!chunks[2])
11450 return 0;
11451 *ptr = chunks[2] + ( *ptr - chunks[1] );
11452 return 1;
11453}
11454
David Cournapeau16023ee2010-12-23 20:55:41 +090011455/*
11456 * Given a url parameter, find the starting position of the first occurence,
11457 * or NULL if the parameter is not found.
11458 *
11459 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11460 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011461 *
11462 * Warning:this function returns a pointer that can be point to the first chunk
11463 * or the second chunk. The caller must be check the position before using the
11464 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011465 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011466static const char *
11467find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011468 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011469 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011470{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011471 const char *pos, *last, *equal;
11472 const char **bufs = chunks;
11473 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011474
David Cournapeau16023ee2010-12-23 20:55:41 +090011475
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011476 pos = bufs[0];
11477 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011478 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011479 /* Check the equal. */
11480 equal = pos + url_param_name_l;
11481 if (fix_pointer_if_wrap(chunks, &equal)) {
11482 if (equal >= chunks[3])
11483 return NULL;
11484 } else {
11485 if (equal >= chunks[1])
11486 return NULL;
11487 }
11488 if (*equal == '=') {
11489 if (pos + url_param_name_l > last) {
11490 /* process wrap case, we detect a wrap. In this case, the
11491 * comparison is performed in two parts.
11492 */
11493
11494 /* This is the end, we dont have any other chunk. */
11495 if (bufs != chunks || !bufs[2])
11496 return NULL;
11497
11498 /* Compute the length of each part of the comparison. */
11499 l1 = last - pos;
11500 l2 = url_param_name_l - l1;
11501
11502 /* The second buffer is too short to contain the compared string. */
11503 if (bufs[2] + l2 > bufs[3])
11504 return NULL;
11505
11506 if (memcmp(pos, url_param_name, l1) == 0 &&
11507 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11508 return pos;
11509
11510 /* Perform wrapping and jump the string who fail the comparison. */
11511 bufs += 2;
11512 pos = bufs[0] + l2;
11513 last = bufs[1];
11514
11515 } else {
11516 /* process a simple comparison. */
11517 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11518 return pos; }
11519 pos += url_param_name_l + 1;
11520 if (fix_pointer_if_wrap(chunks, &pos))
11521 last = bufs[2];
11522 }
11523 }
11524
11525 while (1) {
11526 /* Look for the next delimiter. */
11527 while (pos <= last && !is_param_delimiter(*pos, delim))
11528 pos++;
11529 if (pos < last)
11530 break;
11531 /* process buffer wrapping. */
11532 if (bufs != chunks || !bufs[2])
11533 return NULL;
11534 bufs += 2;
11535 pos = bufs[0];
11536 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011537 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011538 pos++;
11539 }
11540 return NULL;
11541}
11542
11543/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011544 * Given a url parameter name and a query string, returns its value and size
11545 * into *value and *value_l respectively, and returns non-zero. An empty
11546 * url_param_name matches the first available parameter. If the parameter is
11547 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011548 */
11549static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011550find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011551 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011552 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011553{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011554 const char *arg_start, *qs_end;
11555 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011556
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 arg_start = chunks[0];
11558 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011559 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011560 /* Looks for an argument name. */
11561 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011562 url_param_name, url_param_name_l,
11563 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011564 /* Check for wrapping. */
11565 if (arg_start > qs_end)
11566 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011567 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011568 if (!arg_start)
11569 return 0;
11570
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011571 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011572 while (1) {
11573 /* looks for the first argument. */
11574 value_start = memchr(arg_start, '=', qs_end - arg_start);
11575 if (!value_start) {
11576
11577 /* Check for wrapping. */
11578 if (arg_start >= chunks[0] &&
11579 arg_start <= chunks[1] &&
11580 chunks[2]) {
11581 arg_start = chunks[2];
11582 qs_end = chunks[3];
11583 continue;
11584 }
11585 return 0;
11586 }
11587 break;
11588 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011589 value_start++;
11590 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011591 else {
11592 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011593 value_start = arg_start + url_param_name_l + 1;
11594
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011595 /* Check for pointer wrapping. */
11596 if (fix_pointer_if_wrap(chunks, &value_start)) {
11597 /* Update the end pointer. */
11598 qs_end = chunks[3];
11599
11600 /* Check for overflow. */
11601 if (value_start > qs_end)
11602 return 0;
11603 }
11604 }
11605
David Cournapeau16023ee2010-12-23 20:55:41 +090011606 value_end = value_start;
11607
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608 while (1) {
11609 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11610 value_end++;
11611 if (value_end < qs_end)
11612 break;
11613 /* process buffer wrapping. */
11614 if (value_end >= chunks[0] &&
11615 value_end <= chunks[1] &&
11616 chunks[2]) {
11617 value_end = chunks[2];
11618 qs_end = chunks[3];
11619 continue;
11620 }
11621 break;
11622 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011623
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011624 *vstart = value_start;
11625 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011626 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011627}
11628
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011629/* This scans a URL-encoded query string. It takes an optionally wrapping
11630 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11631 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11632 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011633 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011634static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011635smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011636{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011637 const char *vstart, *vend;
11638 struct chunk *temp;
11639 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011640
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011641 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011642 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011643 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011644 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011645 return 0;
11646
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011647 /* Create sample. If the value is contiguous, return the pointer as CONST,
11648 * if the value is wrapped, copy-it in a buffer.
11649 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011650 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011651 if (chunks[2] &&
11652 vstart >= chunks[0] && vstart <= chunks[1] &&
11653 vend >= chunks[2] && vend <= chunks[3]) {
11654 /* Wrapped case. */
11655 temp = get_trash_chunk();
11656 memcpy(temp->str, vstart, chunks[1] - vstart);
11657 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011658 smp->data.u.str.str = temp->str;
11659 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011660 } else {
11661 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011662 smp->data.u.str.str = (char *)vstart;
11663 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011664 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11665 }
11666
11667 /* Update context, check wrapping. */
11668 chunks[0] = vend;
11669 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11670 chunks[1] = chunks[3];
11671 chunks[2] = NULL;
11672 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011673
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011674 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011675 smp->flags |= SMP_F_NOT_LAST;
11676
David Cournapeau16023ee2010-12-23 20:55:41 +090011677 return 1;
11678}
11679
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011680/* This function iterates over each parameter of the query string. It uses
11681 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011682 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11683 * An optional parameter name is passed in args[0], otherwise any parameter is
11684 * considered. It supports an optional delimiter argument for the beginning of
11685 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011686 */
11687static int
11688smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11689{
11690 struct http_msg *msg;
11691 char delim = '?';
11692 const char *name;
11693 int name_len;
11694
Dragan Dosen26f77e52015-05-25 10:02:11 +020011695 if (!args ||
11696 (args[0].type && args[0].type != ARGT_STR) ||
11697 (args[1].type && args[1].type != ARGT_STR))
11698 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011699
Dragan Dosen26f77e52015-05-25 10:02:11 +020011700 name = "";
11701 name_len = 0;
11702 if (args->type == ARGT_STR) {
11703 name = args->data.str.str;
11704 name_len = args->data.str.len;
11705 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011706
Dragan Dosen26f77e52015-05-25 10:02:11 +020011707 if (args[1].type)
11708 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011709
Dragan Dosen26f77e52015-05-25 10:02:11 +020011710 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011711 CHECK_HTTP_MESSAGE_FIRST();
11712
11713 msg = &smp->strm->txn->req;
11714
11715 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11716 msg->sl.rq.u_l, delim);
11717 if (!smp->ctx.a[0])
11718 return 0;
11719
11720 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011721
11722 /* Assume that the context is filled with NULL pointer
11723 * before the first call.
11724 * smp->ctx.a[2] = NULL;
11725 * smp->ctx.a[3] = NULL;
11726 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011727 }
11728
11729 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11730}
11731
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011732/* This function iterates over each parameter of the body. This requires
11733 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011734 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11735 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11736 * optional second part if the body wraps at the end of the buffer. An optional
11737 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011738 */
11739static int
11740smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11741{
11742 struct http_txn *txn = smp->strm->txn;
11743 struct http_msg *msg;
11744 unsigned long len;
11745 unsigned long block1;
11746 char *body;
11747 const char *name;
11748 int name_len;
11749
11750 if (!args || (args[0].type && args[0].type != ARGT_STR))
11751 return 0;
11752
11753 name = "";
11754 name_len = 0;
11755 if (args[0].type == ARGT_STR) {
11756 name = args[0].data.str.str;
11757 name_len = args[0].data.str.len;
11758 }
11759
11760 if (!smp->ctx.a[0]) { // first call, find the query string
11761 CHECK_HTTP_MESSAGE_FIRST();
11762
11763 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11764 msg = &txn->req;
11765 else
11766 msg = &txn->rsp;
11767
11768 len = http_body_bytes(msg);
11769 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11770
11771 block1 = len;
11772 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11773 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11774
11775 if (block1 == len) {
11776 /* buffer is not wrapped (or empty) */
11777 smp->ctx.a[0] = body;
11778 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011779
11780 /* Assume that the context is filled with NULL pointer
11781 * before the first call.
11782 * smp->ctx.a[2] = NULL;
11783 * smp->ctx.a[3] = NULL;
11784 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011785 }
11786 else {
11787 /* buffer is wrapped, we need to defragment it */
11788 smp->ctx.a[0] = body;
11789 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790 smp->ctx.a[2] = msg->chn->buf->data;
11791 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011792 }
11793 }
11794 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11795}
11796
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011797/* Return the signed integer value for the specified url parameter (see url_param
11798 * above).
11799 */
11800static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011801smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011802{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011803 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011804
11805 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011806 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011807 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011808 }
11809
11810 return ret;
11811}
11812
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011813/* This produces a 32-bit hash of the concatenation of the first occurrence of
11814 * the Host header followed by the path component if it begins with a slash ('/').
11815 * This means that '*' will not be added, resulting in exactly the first Host
11816 * entry. If no Host header is found, then the path is used. The resulting value
11817 * is hashed using the url hash followed by a full avalanche hash and provides a
11818 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11819 * high-traffic sites without having to store whole paths.
11820 * this differs from the base32 functions in that it includes the url parameters
11821 * as well as the path
11822 */
11823static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011824smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011825{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011826 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011827 struct hdr_ctx ctx;
11828 unsigned int hash = 0;
11829 char *ptr, *beg, *end;
11830 int len;
11831
11832 CHECK_HTTP_MESSAGE_FIRST();
11833
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011834 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011835 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011836 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011837 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11838 ptr = ctx.line + ctx.val;
11839 len = ctx.vlen;
11840 while (len--)
11841 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11842 }
11843
11844 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011845 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 +000011846 beg = http_get_path(txn);
11847 if (!beg)
11848 beg = end;
11849
11850 for (ptr = beg; ptr < end ; ptr++);
11851
11852 if (beg < ptr && *beg == '/') {
11853 while (beg < ptr)
11854 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11855 }
11856 hash = full_hash(hash);
11857
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011858 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011859 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011860 smp->flags = SMP_F_VOL_1ST;
11861 return 1;
11862}
11863
11864/* This concatenates the source address with the 32-bit hash of the Host and
11865 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11866 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11867 * on the source address length. The URL hash is stored before the address so
11868 * that in environments where IPv6 is insignificant, truncating the output to
11869 * 8 bytes would still work.
11870 */
11871static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011872smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011873{
11874 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011875 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011876 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011877
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011878 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011879 return 0;
11880
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011881 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011882 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011883
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011885 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11886 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011887
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011888 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011889 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011890 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011891 temp->len += 4;
11892 break;
11893 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011894 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011895 temp->len += 16;
11896 break;
11897 default:
11898 return 0;
11899 }
11900
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011901 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011902 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011903 return 1;
11904}
11905
Willy Tarreau185b5c42012-04-26 15:11:51 +020011906/* This function is used to validate the arguments passed to any "hdr" fetch
11907 * keyword. These keywords support an optional positive or negative occurrence
11908 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11909 * is assumed that the types are already the correct ones. Returns 0 on error,
11910 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11911 * error message in case of error, that the caller is responsible for freeing.
11912 * The initial location must either be freeable or NULL.
11913 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011914int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011915{
11916 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011917 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011918 return 0;
11919 }
11920 return 1;
11921}
11922
Willy Tarreau276fae92013-07-25 14:36:01 +020011923/* takes an UINT value on input supposed to represent the time since EPOCH,
11924 * adds an optional offset found in args[0] and emits a string representing
11925 * the date in RFC-1123/5322 format.
11926 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011927static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011928{
11929 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11930 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11931 struct chunk *temp;
11932 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011933 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011934 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011935
11936 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011937 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011938 curr_date += args[0].data.sint;
11939
11940 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011941 if (!tm)
11942 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011943
11944 temp = get_trash_chunk();
11945 temp->len = snprintf(temp->str, temp->size - temp->len,
11946 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11947 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11948 tm->tm_hour, tm->tm_min, tm->tm_sec);
11949
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011950 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011951 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011952 return 1;
11953}
11954
Thierry FOURNIERad903512014-04-11 17:51:01 +020011955/* Match language range with language tag. RFC2616 14.4:
11956 *
11957 * A language-range matches a language-tag if it exactly equals
11958 * the tag, or if it exactly equals a prefix of the tag such
11959 * that the first tag character following the prefix is "-".
11960 *
11961 * Return 1 if the strings match, else return 0.
11962 */
11963static inline int language_range_match(const char *range, int range_len,
11964 const char *tag, int tag_len)
11965{
11966 const char *end = range + range_len;
11967 const char *tend = tag + tag_len;
11968 while (range < end) {
11969 if (*range == '-' && tag == tend)
11970 return 1;
11971 if (*range != *tag || tag == tend)
11972 return 0;
11973 range++;
11974 tag++;
11975 }
11976 /* Return true only if the last char of the tag is matched. */
11977 return tag == tend;
11978}
11979
11980/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011981static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011982{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011983 const char *al = smp->data.u.str.str;
11984 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011985 const char *token;
11986 int toklen;
11987 int qvalue;
11988 const char *str;
11989 const char *w;
11990 int best_q = 0;
11991
11992 /* Set the constant to the sample, because the output of the
11993 * function will be peek in the constant configuration string.
11994 */
11995 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011996 smp->data.u.str.size = 0;
11997 smp->data.u.str.str = "";
11998 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011999
12000 /* Parse the accept language */
12001 while (1) {
12002
12003 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012004 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012005 al++;
12006 if (al >= end)
12007 break;
12008
12009 /* Start of the fisrt word. */
12010 token = al;
12011
12012 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012013 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012014 al++;
12015 if (al == token)
12016 goto expect_comma;
12017
12018 /* Length of the token. */
12019 toklen = al - token;
12020 qvalue = 1000;
12021
12022 /* Check if the token exists in the list. If the token not exists,
12023 * jump to the next token.
12024 */
12025 str = args[0].data.str.str;
12026 w = str;
12027 while (1) {
12028 if (*str == ';' || *str == '\0') {
12029 if (language_range_match(token, toklen, w, str-w))
12030 goto look_for_q;
12031 if (*str == '\0')
12032 goto expect_comma;
12033 w = str + 1;
12034 }
12035 str++;
12036 }
12037 goto expect_comma;
12038
12039look_for_q:
12040
12041 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012042 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012043 al++;
12044 if (al >= end)
12045 goto process_value;
12046
12047 /* If ',' is found, process the result */
12048 if (*al == ',')
12049 goto process_value;
12050
12051 /* If the character is different from ';', look
12052 * for the end of the header part in best effort.
12053 */
12054 if (*al != ';')
12055 goto expect_comma;
12056
12057 /* Assumes that the char is ';', now expect "q=". */
12058 al++;
12059
12060 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012061 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012062 al++;
12063 if (al >= end)
12064 goto process_value;
12065
12066 /* Expect 'q'. If no 'q', continue in best effort */
12067 if (*al != 'q')
12068 goto process_value;
12069 al++;
12070
12071 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012072 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012073 al++;
12074 if (al >= end)
12075 goto process_value;
12076
12077 /* Expect '='. If no '=', continue in best effort */
12078 if (*al != '=')
12079 goto process_value;
12080 al++;
12081
12082 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012083 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012084 al++;
12085 if (al >= end)
12086 goto process_value;
12087
12088 /* Parse the q value. */
12089 qvalue = parse_qvalue(al, &al);
12090
12091process_value:
12092
12093 /* If the new q value is the best q value, then store the associated
12094 * language in the response. If qvalue is the biggest value (1000),
12095 * break the process.
12096 */
12097 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012098 smp->data.u.str.str = (char *)w;
12099 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012100 if (qvalue >= 1000)
12101 break;
12102 best_q = qvalue;
12103 }
12104
12105expect_comma:
12106
12107 /* Expect comma or end. If the end is detected, quit the loop. */
12108 while (al < end && *al != ',')
12109 al++;
12110 if (al >= end)
12111 break;
12112
12113 /* Comma is found, jump it and restart the analyzer. */
12114 al++;
12115 }
12116
12117 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012118 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12119 smp->data.u.str.str = args[1].data.str.str;
12120 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012121 }
12122
12123 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012124 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012125}
12126
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012127/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012128static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012129{
12130 /* If the constant flag is set or if not size is avalaible at
12131 * the end of the buffer, copy the string in other buffer
12132 * before decoding.
12133 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012134 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012135 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12137 smp->data.u.str.str = str->str;
12138 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012139 smp->flags &= ~SMP_F_CONST;
12140 }
12141
12142 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012143 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12144 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012145 return 1;
12146}
12147
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012148static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12149{
12150 struct proxy *fe = strm_fe(smp->strm);
12151 int idx, i;
12152 struct cap_hdr *hdr;
12153 int len;
12154
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012155 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012156 return 0;
12157
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012158 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012159
12160 /* Check the availibity of the capture id. */
12161 if (idx > fe->nb_req_cap - 1)
12162 return 0;
12163
12164 /* Look for the original configuration. */
12165 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12166 hdr != NULL && i != idx ;
12167 i--, hdr = hdr->next);
12168 if (!hdr)
12169 return 0;
12170
12171 /* check for the memory allocation */
12172 if (smp->strm->req_cap[hdr->index] == NULL)
12173 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12174 if (smp->strm->req_cap[hdr->index] == NULL)
12175 return 0;
12176
12177 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012178 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012179 if (len > hdr->len)
12180 len = hdr->len;
12181
12182 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012183 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012184 smp->strm->req_cap[idx][len] = '\0';
12185
12186 return 1;
12187}
12188
12189static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12190{
12191 struct proxy *fe = strm_fe(smp->strm);
12192 int idx, i;
12193 struct cap_hdr *hdr;
12194 int len;
12195
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012196 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012197 return 0;
12198
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012199 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012200
12201 /* Check the availibity of the capture id. */
12202 if (idx > fe->nb_rsp_cap - 1)
12203 return 0;
12204
12205 /* Look for the original configuration. */
12206 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12207 hdr != NULL && i != idx ;
12208 i--, hdr = hdr->next);
12209 if (!hdr)
12210 return 0;
12211
12212 /* check for the memory allocation */
12213 if (smp->strm->res_cap[hdr->index] == NULL)
12214 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12215 if (smp->strm->res_cap[hdr->index] == NULL)
12216 return 0;
12217
12218 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012219 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012220 if (len > hdr->len)
12221 len = hdr->len;
12222
12223 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012224 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012225 smp->strm->res_cap[idx][len] = '\0';
12226
12227 return 1;
12228}
12229
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012230/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012231 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012232 * the relevant part of the request line accordingly. Then it updates various
12233 * pointers to the next elements which were moved, and the total buffer length.
12234 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012235 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12236 * error, though this can be revisited when this code is finally exploited.
12237 *
12238 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12239 * query string and 3 to replace uri.
12240 *
12241 * In query string case, the mark question '?' must be set at the start of the
12242 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012243 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012244int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012245 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012246{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012247 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012248 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012249 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012250 int delta;
12251
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012252 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012253 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012254 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012255 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12256
12257 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012258 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012259 txn->req.sl.rq.m_l += delta;
12260 txn->req.sl.rq.u += delta;
12261 txn->req.sl.rq.v += delta;
12262 break;
12263
12264 case 1: // path
12265 cur_ptr = http_get_path(txn);
12266 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012267 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268
12269 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012270 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012271 cur_end++;
12272
12273 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012274 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012275 txn->req.sl.rq.u_l += delta;
12276 txn->req.sl.rq.v += delta;
12277 break;
12278
12279 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012280 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012281 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012282 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12283 while (cur_ptr < cur_end && *cur_ptr != '?')
12284 cur_ptr++;
12285
12286 /* skip the question mark or indicate that we must insert it
12287 * (but only if the format string is not empty then).
12288 */
12289 if (cur_ptr < cur_end)
12290 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012291 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012292 offset = 0;
12293
12294 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012295 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012296 txn->req.sl.rq.u_l += delta;
12297 txn->req.sl.rq.v += delta;
12298 break;
12299
12300 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012301 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012302 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12303
12304 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012305 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306 txn->req.sl.rq.u_l += delta;
12307 txn->req.sl.rq.v += delta;
12308 break;
12309
12310 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012311 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012312 }
12313
12314 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012315 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012316 txn->req.sl.rq.l += delta;
12317 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012318 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012319 return 0;
12320}
12321
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012322/* This function replace the HTTP status code and the associated message. The
12323 * variable <status> contains the new status code. This function never fails.
12324 */
12325void http_set_status(unsigned int status, struct stream *s)
12326{
12327 struct http_txn *txn = s->txn;
12328 char *cur_ptr, *cur_end;
12329 int delta;
12330 char *res;
12331 int c_l;
12332 const char *msg;
12333 int msg_len;
12334
12335 chunk_reset(&trash);
12336
12337 res = ultoa_o(status, trash.str, trash.size);
12338 c_l = res - trash.str;
12339
12340 trash.str[c_l] = ' ';
12341 trash.len = c_l + 1;
12342
12343 msg = get_reason(status);
12344 msg_len = strlen(msg);
12345
12346 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12347 trash.len += msg_len;
12348
12349 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12350 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12351
12352 /* commit changes and adjust message */
12353 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12354
12355 /* adjust res line offsets and lengths */
12356 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12357 txn->rsp.sl.st.c_l = c_l;
12358 txn->rsp.sl.st.r_l = msg_len;
12359
12360 delta = trash.len - (cur_end - cur_ptr);
12361 txn->rsp.sl.st.l += delta;
12362 txn->hdr_idx.v[0].len += delta;
12363 http_msg_move_end(&txn->rsp, delta);
12364}
12365
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012366/* This function executes one of the set-{method,path,query,uri} actions. It
12367 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012368 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012369 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012370 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12371 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012372 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012373enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12374 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012375{
12376 chunk_reset(&trash);
12377
12378 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012379 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012380 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012381 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012382
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012383 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012385}
12386
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012387/* This function is just a compliant action wrapper for "set-status". */
12388enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
12389 struct session *sess, struct stream *s)
12390{
12391 http_set_status(rule->arg.status.code, s);
12392 return ACT_RET_CONT;
12393}
12394
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012395/* parse an http-request action among :
12396 * set-method
12397 * set-path
12398 * set-query
12399 * set-uri
12400 *
12401 * All of them accept a single argument of type string representing a log-format.
12402 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12403 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012404 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012405 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012406enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12407 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012408{
12409 int cur_arg = *orig_arg;
12410
Thierry FOURNIER42148732015-09-02 17:17:33 +020012411 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012412
12413 switch (args[0][4]) {
12414 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012415 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 rule->action_ptr = http_action_set_req_line;
12417 break;
12418 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012419 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012420 rule->action_ptr = http_action_set_req_line;
12421 break;
12422 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012423 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012424 rule->action_ptr = http_action_set_req_line;
12425 break;
12426 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012427 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012428 rule->action_ptr = http_action_set_req_line;
12429 break;
12430 default:
12431 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012432 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433 }
12434
12435 if (!*args[cur_arg] ||
12436 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12437 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012438 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012439 }
12440
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012441 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012443 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012444 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12445 proxy->conf.args.file, proxy->conf.args.line);
12446
12447 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012448 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012449}
12450
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012451/* parse set-status action:
12452 * This action accepts a single argument of type int representing
12453 * an http status code. It returns ACT_RET_PRS_OK on success,
12454 * ACT_RET_PRS_ERR on error.
12455 */
12456enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12457 struct act_rule *rule, char **err)
12458{
12459 char *error;
12460
Thierry FOURNIER42148732015-09-02 17:17:33 +020012461 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012462 rule->action_ptr = action_http_set_status;
12463
12464 /* Check if an argument is available */
12465 if (!*args[*orig_arg]) {
12466 memprintf(err, "expects exactly 1 argument <status>");
12467 return ACT_RET_PRS_ERR;
12468 }
12469
12470 /* convert status code as integer */
12471 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12472 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12473 memprintf(err, "expects an integer status code between 100 and 999");
12474 return ACT_RET_PRS_ERR;
12475 }
12476
12477 (*orig_arg)++;
12478 return ACT_RET_PRS_OK;
12479}
12480
Willy Tarreaua9083d02015-05-08 15:27:59 +020012481/* This function executes the "capture" action. It executes a fetch expression,
12482 * turns the result into a string and puts it in a capture slot. It always
12483 * returns 1. If an error occurs the action is cancelled, but the rule
12484 * processing continues.
12485 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012486enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12487 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012488{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012489 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012490 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012491 char **cap = s->req_cap;
12492 int len;
12493
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012494 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012495 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012496 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012497
12498 if (cap[h->index] == NULL)
12499 cap[h->index] = pool_alloc2(h->pool);
12500
12501 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012502 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012503
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012504 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012505 if (len > h->len)
12506 len = h->len;
12507
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012508 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012509 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012510 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012511}
12512
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012513/* This function executes the "capture" action and store the result in a
12514 * capture slot if exists. It executes a fetch expression, turns the result
12515 * into a string and puts it in a capture slot. It always returns 1. If an
12516 * error occurs the action is cancelled, but the rule processing continues.
12517 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012518enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12519 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012520{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012521 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012522 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012523 char **cap = s->req_cap;
12524 struct proxy *fe = strm_fe(s);
12525 int len;
12526 int i;
12527
12528 /* Look for the original configuration. */
12529 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012530 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012531 i--, h = h->next);
12532 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012533 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012534
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012535 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012536 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012537 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012538
12539 if (cap[h->index] == NULL)
12540 cap[h->index] = pool_alloc2(h->pool);
12541
12542 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012543 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012544
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012545 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012546 if (len > h->len)
12547 len = h->len;
12548
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012549 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012550 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012551 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012552}
12553
Willy Tarreaua9083d02015-05-08 15:27:59 +020012554/* parse an "http-request capture" action. It takes a single argument which is
12555 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012556 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012557 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012558 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012559enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12560 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012561{
12562 struct sample_expr *expr;
12563 struct cap_hdr *hdr;
12564 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012565 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012566
12567 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12568 if (strcmp(args[cur_arg], "if") == 0 ||
12569 strcmp(args[cur_arg], "unless") == 0)
12570 break;
12571
12572 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012573 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012574 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012575 }
12576
Willy Tarreaua9083d02015-05-08 15:27:59 +020012577 cur_arg = *orig_arg;
12578 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12579 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012580 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012581
12582 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12583 memprintf(err,
12584 "fetch method '%s' extracts information from '%s', none of which is available here",
12585 args[cur_arg-1], sample_src_names(expr->fetch->use));
12586 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012587 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012588 }
12589
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012590 if (!args[cur_arg] || !*args[cur_arg]) {
12591 memprintf(err, "expects 'len or 'id'");
12592 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012593 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012594 }
12595
Willy Tarreaua9083d02015-05-08 15:27:59 +020012596 if (strcmp(args[cur_arg], "len") == 0) {
12597 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012598
12599 if (!(px->cap & PR_CAP_FE)) {
12600 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012601 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012602 }
12603
12604 proxy->conf.args.ctx = ARGC_CAP;
12605
Willy Tarreaua9083d02015-05-08 15:27:59 +020012606 if (!args[cur_arg]) {
12607 memprintf(err, "missing length value");
12608 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012610 }
12611 /* we copy the table name for now, it will be resolved later */
12612 len = atoi(args[cur_arg]);
12613 if (len <= 0) {
12614 memprintf(err, "length must be > 0");
12615 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012616 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012617 }
12618 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012619
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012620 if (!len) {
12621 memprintf(err, "a positive 'len' argument is mandatory");
12622 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012623 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012624 }
12625
12626 hdr = calloc(sizeof(struct cap_hdr), 1);
12627 hdr->next = px->req_cap;
12628 hdr->name = NULL; /* not a header capture */
12629 hdr->namelen = 0;
12630 hdr->len = len;
12631 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12632 hdr->index = px->nb_req_cap++;
12633
12634 px->req_cap = hdr;
12635 px->to_log |= LW_REQHDR;
12636
Thierry FOURNIER42148732015-09-02 17:17:33 +020012637 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012638 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012639 rule->arg.cap.expr = expr;
12640 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012641 }
12642
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012643 else if (strcmp(args[cur_arg], "id") == 0) {
12644 int id;
12645 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012646
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012647 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012648
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012649 if (!args[cur_arg]) {
12650 memprintf(err, "missing id value");
12651 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012652 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012653 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012654
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012655 id = strtol(args[cur_arg], &error, 10);
12656 if (*error != '\0') {
12657 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12658 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012659 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012660 }
12661 cur_arg++;
12662
12663 proxy->conf.args.ctx = ARGC_CAP;
12664
Thierry FOURNIER42148732015-09-02 17:17:33 +020012665 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012667 rule->arg.capid.expr = expr;
12668 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012669 }
12670
12671 else {
12672 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12673 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012674 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012675 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012676
12677 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012678 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012679}
12680
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012681/* This function executes the "capture" action and store the result in a
12682 * capture slot if exists. It executes a fetch expression, turns the result
12683 * into a string and puts it in a capture slot. It always returns 1. If an
12684 * error occurs the action is cancelled, but the rule processing continues.
12685 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012686enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12687 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012688{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012689 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012690 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012691 char **cap = s->res_cap;
12692 struct proxy *fe = strm_fe(s);
12693 int len;
12694 int i;
12695
12696 /* Look for the original configuration. */
12697 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012698 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012699 i--, h = h->next);
12700 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012701 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012703 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012704 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012705 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012706
12707 if (cap[h->index] == NULL)
12708 cap[h->index] = pool_alloc2(h->pool);
12709
12710 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012711 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012712
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012713 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012714 if (len > h->len)
12715 len = h->len;
12716
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012717 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012718 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012719 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012720}
12721
12722/* parse an "http-response capture" action. It takes a single argument which is
12723 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12724 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012725 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012726 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012727enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12728 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729{
12730 struct sample_expr *expr;
12731 int cur_arg;
12732 int id;
12733 char *error;
12734
12735 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12736 if (strcmp(args[cur_arg], "if") == 0 ||
12737 strcmp(args[cur_arg], "unless") == 0)
12738 break;
12739
12740 if (cur_arg < *orig_arg + 3) {
12741 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012742 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012743 }
12744
12745 cur_arg = *orig_arg;
12746 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12747 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012748 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012749
12750 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12751 memprintf(err,
12752 "fetch method '%s' extracts information from '%s', none of which is available here",
12753 args[cur_arg-1], sample_src_names(expr->fetch->use));
12754 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012755 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012756 }
12757
12758 if (!args[cur_arg] || !*args[cur_arg]) {
12759 memprintf(err, "expects 'len or 'id'");
12760 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012761 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012762 }
12763
12764 if (strcmp(args[cur_arg], "id") != 0) {
12765 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12766 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012767 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012768 }
12769
12770 cur_arg++;
12771
12772 if (!args[cur_arg]) {
12773 memprintf(err, "missing id value");
12774 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012776 }
12777
12778 id = strtol(args[cur_arg], &error, 10);
12779 if (*error != '\0') {
12780 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12781 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012782 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012783 }
12784 cur_arg++;
12785
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012786 proxy->conf.args.ctx = ARGC_CAP;
12787
Thierry FOURNIER42148732015-09-02 17:17:33 +020012788 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012789 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012790 rule->arg.capid.expr = expr;
12791 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012792
12793 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012794 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012795}
12796
William Lallemand73025dd2014-04-24 14:38:37 +020012797/*
12798 * Return the struct http_req_action_kw associated to a keyword.
12799 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012800struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012801{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012802 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012803}
12804
12805/*
12806 * Return the struct http_res_action_kw associated to a keyword.
12807 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012808struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012809{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012810 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012811}
12812
Willy Tarreau4a568972010-05-12 08:08:50 +020012813/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012814/* All supported ACL keywords must be declared here. */
12815/************************************************************************/
12816
12817/* Note: must not be declared <const> as its list will be overwritten.
12818 * Please take care of keeping this list alphabetically sorted.
12819 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012820static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012821 { "base", "base", PAT_MATCH_STR },
12822 { "base_beg", "base", PAT_MATCH_BEG },
12823 { "base_dir", "base", PAT_MATCH_DIR },
12824 { "base_dom", "base", PAT_MATCH_DOM },
12825 { "base_end", "base", PAT_MATCH_END },
12826 { "base_len", "base", PAT_MATCH_LEN },
12827 { "base_reg", "base", PAT_MATCH_REG },
12828 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012829
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012830 { "cook", "req.cook", PAT_MATCH_STR },
12831 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12832 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12833 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12834 { "cook_end", "req.cook", PAT_MATCH_END },
12835 { "cook_len", "req.cook", PAT_MATCH_LEN },
12836 { "cook_reg", "req.cook", PAT_MATCH_REG },
12837 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012838
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012839 { "hdr", "req.hdr", PAT_MATCH_STR },
12840 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12841 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12842 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12843 { "hdr_end", "req.hdr", PAT_MATCH_END },
12844 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12845 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12846 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012847
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012848 /* these two declarations uses strings with list storage (in place
12849 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12850 * and delete functions are relative to the list management. The parse
12851 * and match method are related to the corresponding fetch methods. This
12852 * is very particular ACL declaration mode.
12853 */
12854 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12855 { "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 +020012856
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012857 { "path", "path", PAT_MATCH_STR },
12858 { "path_beg", "path", PAT_MATCH_BEG },
12859 { "path_dir", "path", PAT_MATCH_DIR },
12860 { "path_dom", "path", PAT_MATCH_DOM },
12861 { "path_end", "path", PAT_MATCH_END },
12862 { "path_len", "path", PAT_MATCH_LEN },
12863 { "path_reg", "path", PAT_MATCH_REG },
12864 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012865
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012866 { "req_ver", "req.ver", PAT_MATCH_STR },
12867 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012868
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012869 { "scook", "res.cook", PAT_MATCH_STR },
12870 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12871 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12872 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12873 { "scook_end", "res.cook", PAT_MATCH_END },
12874 { "scook_len", "res.cook", PAT_MATCH_LEN },
12875 { "scook_reg", "res.cook", PAT_MATCH_REG },
12876 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012877
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012878 { "shdr", "res.hdr", PAT_MATCH_STR },
12879 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12880 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12881 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12882 { "shdr_end", "res.hdr", PAT_MATCH_END },
12883 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12884 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12885 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012886
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012887 { "url", "url", PAT_MATCH_STR },
12888 { "url_beg", "url", PAT_MATCH_BEG },
12889 { "url_dir", "url", PAT_MATCH_DIR },
12890 { "url_dom", "url", PAT_MATCH_DOM },
12891 { "url_end", "url", PAT_MATCH_END },
12892 { "url_len", "url", PAT_MATCH_LEN },
12893 { "url_reg", "url", PAT_MATCH_REG },
12894 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012895
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012896 { "urlp", "urlp", PAT_MATCH_STR },
12897 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12898 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12899 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12900 { "urlp_end", "urlp", PAT_MATCH_END },
12901 { "urlp_len", "urlp", PAT_MATCH_LEN },
12902 { "urlp_reg", "urlp", PAT_MATCH_REG },
12903 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012904
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012905 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012906}};
12907
12908/************************************************************************/
12909/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012910/************************************************************************/
12911/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012912static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012913 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012914 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012915 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12916
Willy Tarreau87b09662015-04-03 00:22:06 +020012917 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012918 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012919
12920 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012921 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12922 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12923 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012924
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012925 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12926 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012927
Willy Tarreau409bcde2013-01-08 00:31:00 +010012928 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12929 * are only here to match the ACL's name, are request-only and are used
12930 * for ACL compatibility only.
12931 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012932 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12933 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012934 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12935 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012936
12937 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12938 * only here to match the ACL's name, are request-only and are used for
12939 * ACL compatibility only.
12940 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012941 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012942 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012943 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012944 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012945
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012946 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012947 { "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 +010012948 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012949 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012950 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012951 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012952
12953 /* HTTP protocol on the request path */
12954 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012955 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012956
12957 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012958 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12959 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012960
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012961 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012962 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12963 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012964 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012965
Willy Tarreau18ed2562013-01-14 15:56:36 +010012966 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012967 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12968 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012969
Willy Tarreau18ed2562013-01-14 15:56:36 +010012970 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012971 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012972 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12973 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012974
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012975 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012976 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012977 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012978 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012979 { "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 +010012980 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012981 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012982
12983 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012984 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012985 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12986 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012987
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012989 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012990 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012991 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012992 { "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 +010012993 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012994 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012995
Willy Tarreau409bcde2013-01-08 00:31:00 +010012996 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012997 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012998 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12999 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013000 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013001
13002 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013003 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013004 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013005 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013006 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013007
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013008 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013011 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013012 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013013 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013014 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13015 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013016 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013017 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013018}};
13019
Willy Tarreau8797c062007-05-07 00:55:35 +020013020
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013021/************************************************************************/
13022/* All supported converter keywords must be declared here. */
13023/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013024/* Note: must not be declared <const> as its list will be overwritten */
13025static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013026 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013027 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013028 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13029 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013030 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013031 { NULL, NULL, 0, 0, 0 },
13032}};
13033
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013034
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013035/************************************************************************/
13036/* All supported http-request action keywords must be declared here. */
13037/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013038struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013039 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013040 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013041 { "set-method", parse_set_req_line },
13042 { "set-path", parse_set_req_line },
13043 { "set-query", parse_set_req_line },
13044 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013045 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013046 }
13047};
13048
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013049struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013050 .kw = {
13051 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013052 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013053 { NULL, NULL }
13054 }
13055};
13056
Willy Tarreau8797c062007-05-07 00:55:35 +020013057__attribute__((constructor))
13058static void __http_protocol_init(void)
13059{
13060 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013061 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013062 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013063 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013064 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013065}
13066
13067
Willy Tarreau58f10d72006-12-04 02:26:12 +010013068/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013069 * Local variables:
13070 * c-indent-level: 8
13071 * c-basic-offset: 8
13072 * End:
13073 */